mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
2e0b47639c
Closes #1196 We deprecated looking up factories by class in #877. We introduced `allow_class_lookup` option so that people could disable the behavior entirely after fixing the deprecation warning. In preparation for factory_bot 5, I am removing the deprecation warning and the `allow_class_lookup` option. It is no longer possible to look up factories by class. This has also made the ClassKeyHash decorator unnecessary. The behavior is technically a little different now that we are using HashWithIndifferentAccess instead of calling to_sym on the key, but it should behave identically for any standard, documented factory_bot usage.
40 lines
679 B
Ruby
40 lines
679 B
Ruby
require "active_support/core_ext/hash/indifferent_access"
|
|
|
|
module FactoryBot
|
|
class Registry
|
|
include Enumerable
|
|
|
|
attr_reader :name
|
|
|
|
def initialize(name)
|
|
@name = name
|
|
@items = ActiveSupport::HashWithIndifferentAccess.new
|
|
end
|
|
|
|
def clear
|
|
@items.clear
|
|
end
|
|
|
|
def each(&block)
|
|
@items.values.uniq.each(&block)
|
|
end
|
|
|
|
def find(name)
|
|
if registered?(name)
|
|
@items[name]
|
|
else
|
|
raise ArgumentError, "#{@name} not registered: #{name}"
|
|
end
|
|
end
|
|
|
|
alias :[] :find
|
|
|
|
def register(name, item)
|
|
@items[name] = item
|
|
end
|
|
|
|
def registered?(name)
|
|
@items.key?(name)
|
|
end
|
|
end
|
|
end
|