Class: FactoryManager

Inherits:
Object
  • Object
show all
Defined in:
lib/factory_manager.rb

Overview

A factory manager of factory bots.

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • method (Symbol)

    The name of the method.

  • arguments (Hash)

    The factory arguments.

Returns:

  • (ActiveRecord::Base)

    The built factory record.



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.

Yields:

  • Invokes the block to build the 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.

Yields:

  • Invokes the block to create the 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.

Yields:

  • Invokes the block to generate the factory.

Returns:

  • (OpenStruct)

    An object containing the record results.



33
34
35
36
37
# File 'lib/factory_manager.rb', line 33

def generate(&block)
  instance_eval(&block)

  OpenStruct.new(@locals)
end