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

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

Class Method Details

.indent(level, value) ⇒ Object



34
35
36
# File 'lib/factory_bot_factory/line_writer.rb', line 34

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

.wrap_definition(&_block) ⇒ Object



38
39
40
41
42
43
# File 'lib/factory_bot_factory/line_writer.rb', line 38

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



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

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
# File 'lib/factory_bot_factory/line_writer.rb', line 13

def build(key, value)
  wrap_block(key, Attribute.new(key, value, @options).build)
end

#build_nested_line(prefix, key) ⇒ Object



17
18
19
# File 'lib/factory_bot_factory/line_writer.rb', line 17

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