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

Version Path
rubysmith-8.0.0 lib/rubysmith/text/inserter.rb
rubysmith-7.9.0 lib/rubysmith/text/inserter.rb
rubysmith-7.8.0 lib/rubysmith/text/inserter.rb
rubysmith-7.7.0 lib/rubysmith/text/inserter.rb
rubysmith-7.6.0 lib/rubysmith/text/inserter.rb
rubysmith-7.5.0 lib/rubysmith/text/inserter.rb
rubysmith-7.4.0 lib/rubysmith/text/inserter.rb
rubysmith-7.3.0 lib/rubysmith/text/inserter.rb
rubysmith-7.2.0 lib/rubysmith/text/inserter.rb
rubysmith-7.1.0 lib/rubysmith/text/inserter.rb
rubysmith-7.0.0 lib/rubysmith/text/inserter.rb
rubysmith-6.10.0 lib/rubysmith/text/inserter.rb
rubysmith-6.9.0 lib/rubysmith/text/inserter.rb
rubysmith-6.6.0 lib/rubysmith/text/inserter.rb
rubysmith-6.5.0 lib/rubysmith/text/inserter.rb
rubysmith-6.4.0 lib/rubysmith/text/inserter.rb
rubysmith-6.3.0 lib/rubysmith/text/inserter.rb
rubysmith-6.2.1 lib/rubysmith/text/inserter.rb
rubysmith-6.2.0 lib/rubysmith/text/inserter.rb
rubysmith-6.1.0 lib/rubysmith/text/inserter.rb