1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00

Clean up Registry decorator

This commit is contained in:
Joshua Clayton 2012-04-22 16:44:03 -05:00
parent c8c1a801ed
commit 0dcd70cd14
8 changed files with 72 additions and 62 deletions

View file

@ -8,7 +8,7 @@ require "factory_girl/strategy/create"
require "factory_girl/strategy/attributes_for" require "factory_girl/strategy/attributes_for"
require "factory_girl/strategy/stub" require "factory_girl/strategy/stub"
require "factory_girl/strategy/null" require "factory_girl/strategy/null"
require 'factory_girl/strict_registry' require 'factory_girl/disallows_duplicates_registry'
require 'factory_girl/registry' require 'factory_girl/registry'
require 'factory_girl/null_factory' require 'factory_girl/null_factory'
require 'factory_girl/null_object' require 'factory_girl/null_object'
@ -36,7 +36,7 @@ require 'factory_girl/version'
module FactoryGirl module FactoryGirl
def self.factories def self.factories
@factories ||= StrictRegistry.new(Registry.new("Factory")) @factories ||= DisallowsDuplicatesRegistry.new(Registry.new("Factory"))
end end
def self.register_factory(factory) def self.register_factory(factory)
@ -51,7 +51,7 @@ module FactoryGirl
end end
def self.sequences def self.sequences
@sequences ||= StrictRegistry.new(Registry.new("Sequence")) @sequences ||= DisallowsDuplicatesRegistry.new(Registry.new("Sequence"))
end end
def self.register_sequence(sequence) def self.register_sequence(sequence)
@ -66,7 +66,7 @@ module FactoryGirl
end end
def self.traits def self.traits
@traits ||= StrictRegistry.new(Registry.new("Trait")) @traits ||= DisallowsDuplicatesRegistry.new(Registry.new("Trait"))
end end
def self.register_trait(trait) def self.register_trait(trait)

View file

@ -1,10 +1,10 @@
module FactoryGirl module FactoryGirl
class StrictRegistry class DisallowsDuplicatesRegistry
def initialize(component) def initialize(component)
@component = component @component = component
end end
delegate :each, :registered?, :clear, to: :@component delegate :clear, :each, :find, :[], :registered?, to: :@component
def register(name, item) def register(name, item)
if registered?(name) if registered?(name)
@ -13,13 +13,5 @@ module FactoryGirl
@component.register(name, item) @component.register(name, item)
end end
end end
def find(name)
if registered?(name)
@component.find(name)
else
raise ArgumentError, "#{@component.name} not registered: #{name}"
end
end
end end
end end

View file

@ -9,26 +9,30 @@ module FactoryGirl
@items = {} @items = {}
end end
def register(name, item) def clear
@items[name.to_sym] = item @items.clear
end end
def find(name)
@items[name.to_sym]
end
alias :[] :find
def each(&block) def each(&block)
@items.values.uniq.each(&block) @items.values.uniq.each(&block)
end end
def find(name)
if registered?(name)
@items[name.to_sym]
else
raise ArgumentError, "#{@name} not registered: #{name}"
end
end
alias :[] :find
def register(name, item)
@items[name.to_sym] = item
end
def registered?(name) def registered?(name)
@items.key?(name.to_sym) @items.key?(name.to_sym)
end end
def clear
@items.clear
end
end end
end end

View file

@ -19,8 +19,7 @@ module FactoryGirl
end end
def strategy_name_to_object def strategy_name_to_object
FactoryGirl.strategy_by_name(@name_or_object) || FactoryGirl.strategy_by_name(@name_or_object)
raise("unrecognized method #{@name_or_object}")
end end
end end
end end

View file

@ -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 file

@ -18,6 +18,10 @@ describe FactoryGirl::Registry do
subject[:object_name].should == registered_object subject[:object_name].should == registered_object
end 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 it "adds and returns the object registered" do
subject.register(:object_name, registered_object).should == registered_object subject.register(:object_name, registered_object).should == registered_object
end end

View file

@ -18,6 +18,6 @@ describe FactoryGirl::StrategyCalculator do
end end
it "raises when passing a bogus strategy" do 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
end end

View file

@ -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