Class: Rubysmith::Text::Inserter

Inherits:
Object
  • Object
show all
Defined in:
lib/rubysmith/text/inserter.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(lines, kind = :after) ⇒ Inserter

Returns a new instance of Inserter.



7
8
9
10
# File 'lib/rubysmith/text/inserter.rb', line 7

def initialize lines, kind = :after
  @lines = lines.dup
  @kind = kind
end

Instance Method Details

#call(content, pattern) ⇒ Object



12
13
14
15
16
# 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