Class: Tocer::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/tocer/writer.rb

Overview

Writes table of contents to a Markdown document. :reek:DataClump

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(builder: Builder.new) ⇒ Writer

Returns a new instance of Writer.



23
24
25
# File 'lib/tocer/writer.rb', line 23

def initialize builder: Builder.new
  @builder = builder
end

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