Class: FactoryManager
- Inherits:
-
Object
- Object
- FactoryManager
- Defined in:
- lib/factory_manager.rb
Overview
A factory manager of factory bots.
Class Method Summary collapse
-
.build { ... } ⇒ Object
Initializes and builds a new factory.
-
.create { ... } ⇒ Object
Initializes and creates a new factory.
Instance Method Summary collapse
-
#generate { ... } ⇒ OpenStruct
Generate the factory.
-
#initialize(strategy:) ⇒ FactoryManager
constructor
Initializes a new factory.
Constructor Details
#initialize(strategy:) ⇒ FactoryManager
Initializes a new factory.
6 7 8 9 10 11 |
# File 'lib/factory_manager.rb', line 6 def initialize(strategy:) @locals = {} @results = {} @scopes = {} @strategy = strategy end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *arguments, &block) ⇒ ActiveRecord::Base (private)
Generate a factory record for the missing method.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/factory_manager.rb', line 110 def method_missing(method, *arguments, &block) super unless respond_to_missing?(method) if _assignment_method?(method) _assign_local(method, arguments.first) else record = _generate_factory(method, *arguments) _add_to_scope(record, scope: method) do generate(&block) if block end record end end |
Class Method Details
.build { ... } ⇒ Object
Initializes and builds a new factory.
16 17 18 19 |
# File 'lib/factory_manager.rb', line 16 def self.build(&block) instance = new(strategy: :build) instance.generate(&block) end |
.create { ... } ⇒ Object
Initializes and creates a new factory.
24 25 26 27 |
# File 'lib/factory_manager.rb', line 24 def self.create(&block) instance = new(strategy: :create) instance.generate(&block) end |
Instance Method Details
#generate { ... } ⇒ OpenStruct
Generate the factory.
33 34 35 36 37 |
# File 'lib/factory_manager.rb', line 33 def generate(&block) instance_eval(&block) OpenStruct.new(@locals) end |