Sha256: e2fb7b47a0b52625d7c578e947dd8d70947d11a4b4bebf1b7e96ded485a90c6d
Contents?: true
Size: 706 Bytes
Versions: 82
Compression:
Stored size: 706 Bytes
Contents
# frozen_string_literal: true module Rubysmith module Text # Inserts content before or after a line for a given pattern in an array of lines. class Inserter def initialize lines, kind = :after @lines = lines.dup @kind = kind end def call content, pattern lines.index { |line| line.match? pattern } .then { |index| lines.insert index + offset, content if index } lines end private attr_reader :lines, :kind def offset case kind when :before then 0 when :after then 1 else fail StandardError, "Unknown kind of insert: #{kind}." end end end end end
Version data entries
82 entries across 82 versions & 1 rubygems