diff --git a/lib/factory_girl.rb b/lib/factory_girl.rb index 37def0e..4d7ed1b 100644 --- a/lib/factory_girl.rb +++ b/lib/factory_girl.rb @@ -12,7 +12,6 @@ require 'factory_girl/strategy/create' require 'factory_girl/strategy/attributes_for' require 'factory_girl/strategy/stub' require 'factory_girl/strategy/null' -require 'factory_girl/disallows_duplicates_registry' require 'factory_girl/registry' require 'factory_girl/null_factory' require 'factory_girl/null_object' @@ -39,6 +38,7 @@ require 'factory_girl/find_definitions' require 'factory_girl/reload' require 'factory_girl/decorator' require 'factory_girl/decorator/attribute_hash' +require 'factory_girl/decorator/disallows_duplicates_registry' require 'factory_girl/decorator/invocation_tracker' require 'factory_girl/decorator/invocation_ignorer' require 'factory_girl/version' diff --git a/lib/factory_girl/configuration.rb b/lib/factory_girl/configuration.rb index 41814a6..30527b3 100644 --- a/lib/factory_girl/configuration.rb +++ b/lib/factory_girl/configuration.rb @@ -6,9 +6,9 @@ module FactoryGirl attr_accessor :duplicate_attribute_assignment_from_initialize_with def initialize - @factories = DisallowsDuplicatesRegistry.new(Registry.new('Factory')) - @sequences = DisallowsDuplicatesRegistry.new(Registry.new('Sequence')) - @traits = DisallowsDuplicatesRegistry.new(Registry.new('Trait')) + @factories = Decorator::DisallowsDuplicatesRegistry.new(Registry.new('Factory')) + @sequences = Decorator::DisallowsDuplicatesRegistry.new(Registry.new('Sequence')) + @traits = Decorator::DisallowsDuplicatesRegistry.new(Registry.new('Trait')) @strategies = Registry.new('Strategy') @callback_names = Set.new @definition = Definition.new diff --git a/lib/factory_girl/decorator/disallows_duplicates_registry.rb b/lib/factory_girl/decorator/disallows_duplicates_registry.rb new file mode 100644 index 0000000..fe04290 --- /dev/null +++ b/lib/factory_girl/decorator/disallows_duplicates_registry.rb @@ -0,0 +1,13 @@ +module FactoryGirl + class Decorator + class DisallowsDuplicatesRegistry < Decorator + def register(name, item) + if registered?(name) + raise DuplicateDefinitionError, "#{@component.name} already registered: #{name}" + else + @component.register(name, item) + end + end + end + end +end diff --git a/lib/factory_girl/disallows_duplicates_registry.rb b/lib/factory_girl/disallows_duplicates_registry.rb deleted file mode 100644 index c1ef6bf..0000000 --- a/lib/factory_girl/disallows_duplicates_registry.rb +++ /dev/null @@ -1,17 +0,0 @@ -module FactoryGirl - class DisallowsDuplicatesRegistry - def initialize(component) - @component = component - end - - delegate :clear, :each, :find, :[], :registered?, to: :@component - - def register(name, item) - if registered?(name) - raise DuplicateDefinitionError, "#{@component.name} already registered: #{name}" - else - @component.register(name, item) - end - end - end -end diff --git a/spec/factory_girl/disallows_duplicates_registry_spec.rb b/spec/factory_girl/disallows_duplicates_registry_spec.rb index 7d6450e..ab20ad8 100644 --- a/spec/factory_girl/disallows_duplicates_registry_spec.rb +++ b/spec/factory_girl/disallows_duplicates_registry_spec.rb @@ -1,35 +1,9 @@ require "spec_helper" -describe FactoryGirl::DisallowsDuplicatesRegistry do - let(:registry) { stub("registry", name: "Great thing", register: true, find: true, each: true, clear: true, registered?: true, :[] => true) } +describe FactoryGirl::Decorator::DisallowsDuplicatesRegistry do + let(:registry) { stub("registry", name: 'Great thing', register: true) } - subject { FactoryGirl::DisallowsDuplicatesRegistry.new(registry) } - - it "delegates #each to the registry" do - block = -> {} - subject.each(block) - registry.should have_received(:each).with(block) - end - - it "delegates #registered? to the registry" do - subject.registered?(:great_name) - registry.should have_received(:registered?).with(:great_name) - end - - it "delegates #clear to the registry" do - subject.clear - registry.should have_received(:clear) - end - - it "delegates #find to the registry" do - subject.find(:awesome) - registry.should have_received(:find).with(:awesome) - end - - it "delegates #[] to the registry" do - subject[:awesome] - registry.should have_received(:[]).with(:awesome) - end + subject { described_class.new(registry) } it "delegates #register to the registry when not registered" do registry.stubs(registered?: false) @@ -38,6 +12,7 @@ describe FactoryGirl::DisallowsDuplicatesRegistry do end it "raises when attempting to #register a previously registered strategy" do + registry.stubs(registered?: true) expect { subject.register(:same_name, {}) }. to raise_error(FactoryGirl::DuplicateDefinitionError, "Great thing already registered: same_name") end