Class: Tocer::Writer
- Inherits:
-
Object
- Object
- Tocer::Writer
- Defined in:
- lib/tocer/writer.rb
Overview
Writes table of contents to a Markdown document. :reek:DataClump
Class Method Summary collapse
- .add(start_index:, old_lines:, new_lines:) ⇒ Object
- .remove(start_index, finish_index, lines) ⇒ Object
Instance Method Summary collapse
- #call(path, label: Container[:settings].label) ⇒ Object
-
#initialize(builder: Builder.new) ⇒ Writer
constructor
A new instance of Writer.
Constructor Details
Class Method Details
.add(start_index:, old_lines:, new_lines:) ⇒ Object
13 14 15 16 |
# File 'lib/tocer/writer.rb', line 13 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
18 19 20 21 |
# File 'lib/tocer/writer.rb', line 18 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
#call(path, label: Container[:settings].label) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/tocer/writer.rb', line 27 def call path, label: Container[:settings].label path.rewrite do |body| lines = body.each_line.to_a builder.prependable?(lines) ? prepend(lines, label) : replace(lines, label) end end |