Class: FactoryBotFactory::FileWriter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ FileWriter

Returns a new instance of FileWriter.



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

def initialize(file_path)
  @file_path = file_path
end

Instance Attribute Details

#file_pahtObject (readonly)

Returns the value of attribute file_paht.



5
6
7
# File 'lib/factory_bot_factory/file_writer.rb', line 5

def file_paht
  @file_paht
end

#outputObject (readonly)

Returns the value of attribute output.



5
6
7
# File 'lib/factory_bot_factory/file_writer.rb', line 5

def output
  @output
end

Instance Method Details

#regroup_existing_lines(output) ⇒ Object



27
28
29
# File 'lib/factory_bot_factory/file_writer.rb', line 27

def regroup_existing_lines(output)
  File.read(@file_path) + LineWriter::NEW_LINE + LineWriter.wrap_definition { output }
end

#write(output) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/factory_bot_factory/file_writer.rb', line 11

def write(output)
  if File.file?(@file_path)
    output  = regroup_existing_lines(output)
    message = "New Factory successfully write to an existing file."
  else
    output  = LineWriter.wrap_definition { output }
    message = "New Factory successfully write to a new file."
  end

  FactoryBot.reload if Object.const_defined?("FactoryBot")

  File.open(@file_path, 'w') {|f| f.write(output) }
  Logger.alert(message + "\nPlease check your file in #{@file_path}")
  output
end