2011-01-25 17:55:40 -05:00
|
|
|
module FactoryGirl
|
|
|
|
class Registry
|
2011-05-19 10:56:45 -04:00
|
|
|
include Enumerable
|
|
|
|
|
2011-10-15 01:54:01 -04:00
|
|
|
def initialize(name)
|
|
|
|
@name = name
|
2011-01-25 17:55:40 -05:00
|
|
|
@items = {}
|
|
|
|
end
|
|
|
|
|
2012-04-20 23:24:04 -04:00
|
|
|
def register(name, item)
|
|
|
|
if registered?(name)
|
|
|
|
raise DuplicateDefinitionError, "#{@name} already registered: #{name}"
|
|
|
|
else
|
|
|
|
@items[name.to_sym] = item
|
|
|
|
end
|
2011-01-25 17:55:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def find(name)
|
2011-10-15 01:54:01 -04:00
|
|
|
@items[name.to_sym] or raise ArgumentError.new("#{@name} not registered: #{name.to_s}")
|
2011-01-25 17:55:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def each(&block)
|
2011-06-30 18:26:06 -04:00
|
|
|
@items.values.uniq.each(&block)
|
2011-01-25 17:55:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def [](name)
|
|
|
|
find(name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def registered?(name)
|
|
|
|
@items.key?(name.to_sym)
|
|
|
|
end
|
|
|
|
|
2011-05-19 10:56:45 -04:00
|
|
|
def clear
|
|
|
|
@items.clear
|
|
|
|
end
|
2011-01-25 17:55:40 -05:00
|
|
|
end
|
|
|
|
end
|