2017-10-20 15:20:28 -04:00
|
|
|
module FactoryBot
|
2011-01-25 17:55:40 -05:00
|
|
|
class Registry
|
2011-05-19 10:56:45 -04:00
|
|
|
include Enumerable
|
|
|
|
|
2012-04-20 23:49:37 -04:00
|
|
|
attr_reader :name
|
|
|
|
|
2011-10-15 01:54:01 -04:00
|
|
|
def initialize(name)
|
|
|
|
@name = name
|
2012-06-08 23:54:22 -04:00
|
|
|
@items = Decorator::ClassKeyHash.new({})
|
2011-01-25 17:55:40 -05:00
|
|
|
end
|
|
|
|
|
2012-04-22 17:44:03 -04:00
|
|
|
def clear
|
|
|
|
@items.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
def each(&block)
|
|
|
|
@items.values.uniq.each(&block)
|
2011-01-25 17:55:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def find(name)
|
2012-04-22 17:44:03 -04:00
|
|
|
if registered?(name)
|
2012-06-08 23:54:22 -04:00
|
|
|
@items[name]
|
2012-04-22 17:44:03 -04:00
|
|
|
else
|
|
|
|
raise ArgumentError, "#{@name} not registered: #{name}"
|
|
|
|
end
|
2011-01-25 17:55:40 -05:00
|
|
|
end
|
|
|
|
|
2012-04-23 16:33:12 -04:00
|
|
|
alias :[] :find
|
|
|
|
|
2012-04-22 17:44:03 -04:00
|
|
|
def register(name, item)
|
2012-06-08 23:54:22 -04:00
|
|
|
@items[name] = item
|
2011-01-25 17:55:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def registered?(name)
|
2012-06-08 23:54:22 -04:00
|
|
|
@items.key?(name)
|
2011-01-25 17:55:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|