Class: FactoryManager::Generator

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

Overview

The factory manager generator.

Instance Method Summary collapse

Constructor Details

#initialize(strategy:) ⇒ Generator

Initializes a new factory generator.



7
8
9
10
11
# File 'lib/factory_manager/generator.rb', line 7

def initialize(strategy:)
  @associations = {}
  @results = {}
  @strategy = strategy
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ ActiveRecord::Base (private)

Attempts to 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.



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/factory_manager/generator.rb', line 117

def method_missing(method, *arguments, &block)
  super unless respond_to_missing?(method)

  if _sequence?(method, arguments.first)
    _generate_sequence(*arguments)
  else
    record = _generate_factory(method, *arguments)

    _add_association(record, name: method) do
      generate(&block) if block
    end
  end
end

Instance Method Details

#generate { ... } ⇒ OpenStruct

Generates the factory.

Yields:

  • Instance executes the block to generate the factory.

Returns:

  • (OpenStruct)

    An object containing the generator results.



17
18
19
20
21
# File 'lib/factory_manager/generator.rb', line 17

def generate(&block)
  OpenStruct.new.tap do |locals| # rubocop:disable Style/OpenStructUse
    instance_exec(locals, &block)
  end
end