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

.indent_lines(level, value) ⇒ Object



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

def indent_lines(level, value)
  value.map { |s| indent(level, s) }
end

.join(lines) ⇒ Object



42
43
44
# File 'lib/factory_bot_factory/line_writer.rb', line 42

def join(lines)
  lines.join(LineWriter::NEW_LINE)
end

.wrap_definition(&_block) ⇒ Object



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

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

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



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

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

#write(key, value) ⇒ Object



13
14
15
# File 'lib/factory_bot_factory/line_writer.rb', line 13

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

#write_nested_line(prefix, key) ⇒ Object



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

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