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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BaseFactory

Returns a new instance of BaseFactory.



8
9
10
11
12
13
14
# File 'lib/factory_bot_factory/factories/base_factory.rb', line 8

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

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



6
7
8
# File 'lib/factory_bot_factory/factories/base_factory.rb', line 6

def file_path
  @file_path
end

Instance Method Details

#generate(data) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/factory_bot_factory/factories/base_factory.rb', line 16

def generate(data)
  push_to_factory_queue(@factory_name, data, @nested_level)
  output = []

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

  if @file_path
    write_file(output)
  else
    output = LineWriter.wrap_definition { output }
  end

  output
ensure
  FactoryBot.reload if Object.const_defined?("FactoryBot")
  puts(output) if FactoryBotFactory.config.print_output
end