Sha256: 48fcbb9ec75db4aa45d029635f447913959d1ccf7de9d346519870f55f2551f6

Contents?: true

Size: 715 Bytes

Versions: 3

Compression:

Stored size: 715 Bytes

Contents

require "dry/core/memoizable"

module Rfix
  class Indentation
    include Dry::Core::Memoizable
    SPACE = " ".freeze

    attr_reader :extra_indentation, :input

    def initialize(input, extra_indentation: 0)
      @input = input
      @extra_indentation = extra_indentation
    end

    def stripped
      lines.map do |line|
        line.sub(/^\s{#{min_indentation}}/, "")
      end.map do |line|
        [SPACE * extra_indentation, line].join
      end.join
    end

    alias call stripped

    private

    def min_indentation
      lines.map do |line|
        line.match(/^\s*/)[0].length
      end.min || 0
    end

    def lines
      input.lines
    end

    memoize :min_indentation, :lines
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rfix-1.4.1 lib/rfix/indentation.rb
rfix-1.4.0.pre.201 lib/rfix/indentation.rb
rfix-1.4.0 lib/rfix/indentation.rb