Class: DevSuite::Utils::FileWriter::Writer::Base

Inherits:
Construct::Component::Base show all
Defined in:
lib/dev_suite/utils/file_writer/writer/base.rb

Direct Known Subclasses

Json, Text, Yaml

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Construct::Component::Base

component_key

Constructor Details

#initialize(path) ⇒ Base

Returns a new instance of Base.



10
11
12
13
# File 'lib/dev_suite/utils/file_writer/writer/base.rb', line 10

def initialize(path)
  super()
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/dev_suite/utils/file_writer/writer/base.rb', line 8

def path
  @path
end

Instance Method Details

#append(content) ⇒ Object



24
25
26
27
28
# File 'lib/dev_suite/utils/file_writer/writer/base.rb', line 24

def append(content)
  current_content = read
  updated_content = current_content.merge(content)
  write(updated_content)
end

#append_array(array_key, new_elements) ⇒ Object



48
49
50
51
52
53
# File 'lib/dev_suite/utils/file_writer/writer/base.rb', line 48

def append_array(array_key, new_elements)
  data = read
  data[array_key] ||= []
  data[array_key].concat(new_elements)
  write(data)
end

#delete_key(key, **options) ⇒ Object



42
43
44
45
46
# File 'lib/dev_suite/utils/file_writer/writer/base.rb', line 42

def delete_key(key, **options)
  content = read
  Utils::Data.delete_key_by_path(content, key)
  write(content, **options)
end

#delete_lines(start_line, end_line = start_line) ⇒ Object



30
31
32
33
34
# File 'lib/dev_suite/utils/file_writer/writer/base.rb', line 30

def delete_lines(start_line, end_line = start_line)
  lines = ::File.readlines(path)
  lines.slice!(start_line - 1, end_line - start_line + 1)
  write(lines.join)
end

#readObject



15
16
17
# File 'lib/dev_suite/utils/file_writer/writer/base.rb', line 15

def read
  FileLoader.load(path)
end

#update_key(key, value, **options) ⇒ Object



36
37
38
39
40
# File 'lib/dev_suite/utils/file_writer/writer/base.rb', line 36

def update_key(key, value, **options)
  content = read
  Utils::Data.set_value_by_path(content, key, value)
  write(content, **options)
end

#write(content, backup: false) ⇒ Object



19
20
21
22
# File 'lib/dev_suite/utils/file_writer/writer/base.rb', line 19

def write(content, backup: false)
  create_backup if backup
  AtomicWriter.new(path, content).write
end