Class: FactoryBotFactory::LineWriter

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

Constant Summary collapse

NEW_LINE =
"\n".freeze
WHITE_SPACE =
"\s".freeze
INDENT_SPACE =
2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ LineWriter

Returns a new instance of LineWriter.



9
10
11
# File 'lib/factory_bot_factory/line_writer.rb', line 9

def initialize(options = {})
  @options               = options
end

Instance Attribute Details

#hash_converterObject (readonly)

Returns the value of attribute hash_converter.



7
8
9
# File 'lib/factory_bot_factory/line_writer.rb', line 7

def hash_converter
  @hash_converter
end

#nil_converterObject (readonly)

Returns the value of attribute nil_converter.



7
8
9
# File 'lib/factory_bot_factory/line_writer.rb', line 7

def nil_converter
  @nil_converter
end

#numertic_converterObject (readonly)

Returns the value of attribute numertic_converter.



7
8
9
# File 'lib/factory_bot_factory/line_writer.rb', line 7

def numertic_converter
  @numertic_converter
end

#open_struct_converterObject (readonly)

Returns the value of attribute open_struct_converter.



7
8
9
# File 'lib/factory_bot_factory/line_writer.rb', line 7

def open_struct_converter
  @open_struct_converter
end

#string_converterObject (readonly)

Returns the value of attribute string_converter.



7
8
9
# File 'lib/factory_bot_factory/line_writer.rb', line 7

def string_converter
  @string_converter
end

Class Method Details

.indent(level, value) ⇒ Object



48
49
50
# File 'lib/factory_bot_factory/line_writer.rb', line 48

def indent(level, value)
  "#{WHITE_SPACE * INDENT_SPACE * level}#{value}"
end

.wrap_definition(&_block) ⇒ Object



52
53
54
55
56
57
# File 'lib/factory_bot_factory/line_writer.rb', line 52

def wrap_definition(&_block)
  output = ["FactoryBot.define do"]
  output += yield.map { |s| indent(1, s) }
  output << "end"
  output
end

.wrap_factory(name, target, &_block) ⇒ Object



59
60
61
62
63
64
# File 'lib/factory_bot_factory/line_writer.rb', line 59

def wrap_factory(name, target, &_block)
  output = ["factory :#{name}, class: #{target} do"]
  output += yield.map { |s| indent(1, s) }
  output << "end"
  output
end

Instance Method Details

#build(key, value) ⇒ Object



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

def build(key, value)
  options = [key, value, @options]

  values = if value.nil? || value == "null"
    FactoryBotFactory.config.nil_converter.call(*options)
  elsif value.is_a?(String) || value.is_a?(Symbol)
    FactoryBotFactory.config.string_converter.call(*options)
  elsif value.is_a?(Numeric)
    FactoryBotFactory.config.numertic_converter.call(*options)
  elsif value.is_a?(Hash) || value.is_a?(Array)
    FactoryBotFactory.config.hash_converter.call(*options)
  else value.is_a?(OpenStruct)
    FactoryBotFactory.config.open_struct_converter.call(*options)
  end

  wrap_block(key, values)
end

#build_nested_line(prefix, key) ⇒ Object



31
32
33
# File 'lib/factory_bot_factory/line_writer.rb', line 31

def build_nested_line(prefix, key)
  ["#{key} { build(:#{prefix}_#{key}) }"]
end