Class: DevSuite::Utils::FileWriter::Writer::Base
- Inherits:
-
Construct::Component::Base
- Object
- Construct::Component::Base
- DevSuite::Utils::FileWriter::Writer::Base
- Defined in:
- lib/dev_suite/utils/file_writer/writer/base.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #append(content) ⇒ Object
- #append_array(array_key, new_elements) ⇒ Object
- #delete_key(key, **options) ⇒ Object
- #delete_lines(start_line, end_line = start_line) ⇒ Object
-
#initialize(path) ⇒ Base
constructor
A new instance of Base.
- #read ⇒ Object
- #update_key(key, value, **options) ⇒ Object
- #write(content, backup: false) ⇒ Object
Methods inherited from Construct::Component::Base
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
#path ⇒ Object (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, **) content = read Utils::Data.delete_key_by_path(content, key) write(content, **) 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 |
#read ⇒ Object
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, **) content = read Utils::Data.set_value_by_path(content, key, value) write(content, **) 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 |