2011-01-25 17:55:40 -05:00
|
|
|
module FactoryGirl
|
|
|
|
class Registry
|
2011-05-19 10:56:45 -04:00
|
|
|
include Enumerable
|
|
|
|
|
2011-01-25 17:55:40 -05:00
|
|
|
def initialize
|
|
|
|
@items = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def add(item)
|
|
|
|
item.names.each { |name| add_as(name, item) }
|
|
|
|
item
|
|
|
|
end
|
|
|
|
|
|
|
|
def find(name)
|
|
|
|
@items[name.to_sym] or raise ArgumentError.new("Not registered: #{name.to_s}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def each(&block)
|
2011-05-19 10:56:45 -04:00
|
|
|
@items.values.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
|
|
|
private
|
|
|
|
|
|
|
|
def add_as(name, item)
|
|
|
|
if registered?(name)
|
|
|
|
raise DuplicateDefinitionError, "Already defined: #{name}"
|
|
|
|
else
|
|
|
|
@items[name.to_sym] = item
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|