Class: ActionFactory::Registry
- Inherits:
-
Object
- Object
- ActionFactory::Registry
- Defined in:
- lib/action_factory/registry.rb
Class Method Summary collapse
Instance Method Summary collapse
- #class_name_for(name) ⇒ Object
- #factory_class_name_for(name) ⇒ Object
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
- #register(factory_class_name:, factory_name: nil, class_name: nil) ⇒ Object
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/action_factory/registry.rb', line 19 def initialize @class_name_by_factory_class_name = Hash.new do |class_names, factory_class_name| class_names[factory_class_name.to_s] = factory_class_name.delete_suffix("Factory") end @class_name_by_symbol = Hash.new do |class_names, symbol| class_names[symbol.to_sym] = symbol.to_s.classify end @factories_by_class_name = Hash.new do |factories, class_name| factories[class_name.to_s] = "#{class_name}Factory" end end |
Class Method Details
.clear ⇒ Object
14 15 16 |
# File 'lib/action_factory/registry.rb', line 14 def clear @instance = nil end |
.instance ⇒ Object
10 11 12 |
# File 'lib/action_factory/registry.rb', line 10 def instance @instance ||= new end |
Instance Method Details
#class_name_for(name) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/action_factory/registry.rb', line 52 def class_name_for(name) case name when Symbol # Presume that symbols are factory names @class_name_by_symbol[name] when String, Class # Presume that strings and classes are for factory class names @class_name_by_factory_class_name[name.to_s] else raise ArgumentError, "Invalid argument type #{name.class}" end end |
#factory_class_name_for(name) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/action_factory/registry.rb', line 39 def factory_class_name_for(name) case name when Symbol # Presume that the symbol is a factory name @factories_by_class_name[class_name_for(name)] when Class, String # Presume that classes and strings are a model classes @factories_by_class_name[name.to_s] else raise ArgumentError, "Invalid argument type #{name.class}" end end |
#register(factory_class_name:, factory_name: nil, class_name: nil) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/action_factory/registry.rb', line 31 def register(factory_class_name:, factory_name: nil, class_name: nil) class_name ||= factory_class_name.to_s.chomp("Factory") factory_name ||= class_name.underscore @class_name_by_factory_class_name[factory_class_name.to_s] = class_name @class_name_by_symbol[factory_name.to_sym] = class_name @factories_by_class_name[class_name.to_s] = factory_class_name end |