class Rubysmith::Text::Inserter

Inserts content before or after a line for a given pattern in an array of lines.

Attributes

kind[R]
lines[R]

Public Class Methods

new(lines, kind = :after) click to toggle source
# File lib/rubysmith/text/inserter.rb, line 7
def initialize lines, kind = :after
  @lines = lines.dup
  @kind = kind
end

Public Instance Methods

call(content, pattern) click to toggle source
# File lib/rubysmith/text/inserter.rb, line 12
def call content, pattern
  lines.index { |line| line.match? pattern }
       .then { |index| lines.insert index + offset, content if index }
  lines
end

Private Instance Methods

offset() click to toggle source
# File lib/rubysmith/text/inserter.rb, line 22
def offset
  case kind
    when :before then 0
    when :after then 1
    else fail StandardError, "Unknown kind of insert: #{kind}."
  end
end