Class: FactoryBotFactory::BaseFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/factory_bot_factory/factories/base_factory.rb

Direct Known Subclasses

HashFactory, ModelFactory, OpenStructFactory

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BaseFactory

Returns a new instance of BaseFactory.



4
5
6
7
8
9
10
# File 'lib/factory_bot_factory/factories/base_factory.rb', line 4

def initialize(options = {})
  @factory_name  = options[:factory_name]
  @file_path     = options[:file_path]
  @nested_level  = [(options[:nested_level] || 1), 5].min
  @line_writer   = LineWriter.new(options)
  @factory_queue = []
end

Instance Method Details

#generate(data) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/factory_bot_factory/factories/base_factory.rb', line 12

def generate(data)
  output = LineWriter.wrap_definition do
    push_to_factory_queue(@factory_name, data, @nested_level)
    inner_output = []

    loop do
      factory_option = @factory_queue.shift
      inner_output += build_factory(*factory_option)
      break if @factory_queue.empty?
      inner_output << LineWriter::WHITE_SPACE
    end

    inner_output
  end.join(LineWriter::NEW_LINE)

  write_to_file(output)
  output
end