Permalink
Browse files

Clean up Registry decorator

  • Loading branch information...
1 parent c8c1a80 commit 0dcd70cd14279c7cedce9a92d12ef79cfe0d9464 @joshuaclayton joshuaclayton committed Apr 22, 2012
View
8 lib/factory_girl.rb
@@ -8,7 +8,7 @@
require "factory_girl/strategy/attributes_for"
require "factory_girl/strategy/stub"
require "factory_girl/strategy/null"
-require 'factory_girl/strict_registry'
+require 'factory_girl/disallows_duplicates_registry'
require 'factory_girl/registry'
require 'factory_girl/null_factory'
require 'factory_girl/null_object'
@@ -36,7 +36,7 @@
module FactoryGirl
def self.factories
- @factories ||= StrictRegistry.new(Registry.new("Factory"))
+ @factories ||= DisallowsDuplicatesRegistry.new(Registry.new("Factory"))
end
def self.register_factory(factory)
@@ -51,7 +51,7 @@ def self.factory_by_name(name)
end
def self.sequences
- @sequences ||= StrictRegistry.new(Registry.new("Sequence"))
+ @sequences ||= DisallowsDuplicatesRegistry.new(Registry.new("Sequence"))
end
def self.register_sequence(sequence)
@@ -66,7 +66,7 @@ def self.sequence_by_name(name)
end
def self.traits
- @traits ||= StrictRegistry.new(Registry.new("Trait"))
+ @traits ||= DisallowsDuplicatesRegistry.new(Registry.new("Trait"))
end
def self.register_trait(trait)
View
12 lib/factory_girl/strict_registry.rb → ...ory_girl/disallows_duplicates_registry.rb
@@ -1,10 +1,10 @@
module FactoryGirl
- class StrictRegistry
+ class DisallowsDuplicatesRegistry
def initialize(component)
@component = component
end
- delegate :each, :registered?, :clear, to: :@component
+ delegate :clear, :each, :find, :[], :registered?, to: :@component
def register(name, item)
if registered?(name)
@@ -13,13 +13,5 @@ def register(name, item)
@component.register(name, item)
end
end
-
- def find(name)
- if registered?(name)
- @component.find(name)
- else
- raise ArgumentError, "#{@component.name} not registered: #{name}"
- end
- end
end
end
View
22 lib/factory_girl/registry.rb
@@ -9,26 +9,30 @@ def initialize(name)
@items = {}
end
- def register(name, item)
- @items[name.to_sym] = item
+ def clear
+ @items.clear
+ end
+
+ def each(&block)
+ @items.values.uniq.each(&block)
end
def find(name)
- @items[name.to_sym]
+ if registered?(name)
+ @items[name.to_sym]
+ else
+ raise ArgumentError, "#{@name} not registered: #{name}"
+ end
end
alias :[] :find
- def each(&block)
- @items.values.uniq.each(&block)
+ def register(name, item)
+ @items[name.to_sym] = item
end
def registered?(name)
@items.key?(name.to_sym)
end
-
- def clear
- @items.clear
- end
end
end
View
3 lib/factory_girl/strategy_calculator.rb
@@ -19,8 +19,7 @@ def strategy_is_object?
end
def strategy_name_to_object
- FactoryGirl.strategy_by_name(@name_or_object) ||
- raise("unrecognized method #{@name_or_object}")
+ FactoryGirl.strategy_by_name(@name_or_object)
end
end
end
View
44 spec/factory_girl/disallows_duplicates_registry_spec.rb
@@ -0,0 +1,44 @@
+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) }
+
+ 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
+
+ it "delegates #register to the registry when not registered" do
+ registry.stubs(registered?: false)
+ subject.register(:awesome, {})
+ registry.should have_received(:register).with(:awesome, {})
+ end
+
+ it "raises when attempting to #register a previously registered strategy" do
+ expect { subject.register(:same_name, {}) }.
+ to raise_error(FactoryGirl::DuplicateDefinitionError, "Great thing already registered: same_name")
+ end
+end
View
4 spec/factory_girl/registry_spec.rb
@@ -18,6 +18,10 @@
subject[:object_name].should == registered_object
end
+ it "raises when an object cannot be found" do
+ expect { subject.find(:object_name) }.to raise_error(ArgumentError, "Great thing not registered: object_name")
+ end
+
it "adds and returns the object registered" do
subject.register(:object_name, registered_object).should == registered_object
end
View
2 spec/factory_girl/strategy_calculator_spec.rb
@@ -18,6 +18,6 @@
end
it "raises when passing a bogus strategy" do
- expect { FactoryGirl::StrategyCalculator.new(:bogus_strategy).strategy }.to raise_error "unrecognized method bogus_strategy"
+ expect { FactoryGirl::StrategyCalculator.new(:bogus_strategy).strategy }.to raise_error(ArgumentError, /bogus_strategy/)
end
end
View
33 spec/factory_girl/strict_registry_spec.rb
@@ -1,33 +0,0 @@
-require "spec_helper"
-
-describe FactoryGirl::StrictRegistry do
- let(:registry) { stub("registry", name: "Great thing", register: true, find: true, each: true, registered?: true, clear: true) }
-
- subject { FactoryGirl::StrictRegistry.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 "raises when trying to find an unregistered object" do
- registry.stubs(registered?: false)
- expect { subject.find(:bogus) }.to raise_error(ArgumentError, "Great thing not registered: bogus")
- end
-
- it "doesn't allow a duplicate name" do
- expect { 2.times { subject.register(:same_name, {}) } }.
- to raise_error(FactoryGirl::DuplicateDefinitionError, "Great thing already registered: same_name")
- end
-end

0 comments on commit 0dcd70c

Please sign in to comment.