Class: Tocer::Writer
- Inherits:
-
Object
- Object
- Tocer::Writer
- Defined in:
- lib/tocer/writer.rb
Overview
Writes table of contents to a Markdown document.
Class Method Summary collapse
- .add(start_index:, old_lines:, new_lines:) ⇒ Object
- .remove(start_index, finish_index, lines) ⇒ Object
Instance Method Summary collapse
-
#initialize(file_path, label: "## Table of Contents", builder: Builder.new(label: label)) ⇒ Writer
constructor
A new instance of Writer.
- #write ⇒ Object
Constructor Details
#initialize(file_path, label: "## Table of Contents", builder: Builder.new(label: label)) ⇒ Writer
Returns a new instance of Writer.
16 17 18 19 |
# File 'lib/tocer/writer.rb', line 16 def initialize file_path, label: "## Table of Contents", builder: Builder.new(label: label) @file_path = file_path @builder = builder end |
Class Method Details
.add(start_index:, old_lines:, new_lines:) ⇒ Object
6 7 8 9 |
# File 'lib/tocer/writer.rb', line 6 def self.add start_index:, old_lines:, new_lines: computed_new_lines = start_index.zero? ? new_lines : new_lines + "\n" old_lines.insert start_index, *computed_new_lines end |
.remove(start_index, finish_index, lines) ⇒ Object
11 12 13 14 |
# File 'lib/tocer/writer.rb', line 11 def self.remove start_index, finish_index, lines range = (start_index - 1)..finish_index lines.reject.with_index { |_, index| range.include? index } end |
Instance Method Details
#write ⇒ Object
21 22 23 24 25 |
# File 'lib/tocer/writer.rb', line 21 def write lines = File.readlines file_path body = builder.prependable?(lines) ? prepend(lines) : replace(lines) File.open(file_path, "w") { |file| file.write body } end |