Class: DevSuite::Utils::FileWriter::Writer::Text

Inherits:
Base show all
Defined in:
lib/dev_suite/utils/file_writer/writer/text.rb

Instance Attribute Summary

Attributes inherited from Base

#path

Instance Method Summary collapse

Methods inherited from Base

#append_array, #delete_key, #delete_lines, #initialize, #read, #write

Methods inherited from Construct::Component::Base

component_key

Constructor Details

This class inherits a constructor from DevSuite::Utils::FileWriter::Writer::Base

Instance Method Details

#append(content) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/dev_suite/utils/file_writer/writer/text.rb', line 18

def append(content)
  ::File.open(path, "a") do |file|
    file.write("\n") if ::File.size(path).nonzero?

    file.write(content.strip)
  end
end

#update_key(key, value, backup: false) ⇒ Object

Updates a key-like pattern in a plain text file (find and replace)



9
10
11
12
13
14
15
16
# File 'lib/dev_suite/utils/file_writer/writer/text.rb', line 9

def update_key(key, value, backup: false)
  content = read

  # Simple pattern matching and replacement (e.g., `key: value`)
  updated_content = content.gsub(/^#{Regexp.escape(key)}:.*/, "#{key}: #{value}")

  write(updated_content, backup: backup)
end