Sha256: b30fc4c323fd251d17eeea72ea3f48361648ed351e10109a82e9ec0fb7d53a17

Contents?: true

Size: 1.75 KB

Versions: 438

Compression:

Stored size: 1.75 KB

Contents

module Effective
  class CodeReader
    attr_reader :lines

    def initialize(filename, &block)
      @lines = File.open(filename).readlines
      block.call(self) if block_given?
    end

    # Iterate over the lines with a depth, and passed the stripped line to the passed block
    def each_with_depth(from: nil, to: nil, &block)
      Array(lines).each_with_index do |line, index|
        next if index < (from || 0)

        depth = line.length - line.lstrip.length
        block.call(line.strip, depth, index)

        break if to == index
      end

      nil
    end

    # Returns the index of the first line where the passed block returns true
    def index(from: nil, to: nil, &block)
      each_with_depth(from: from, to: to) do |line, depth, index|
        return index if block.call(line, depth, index)
      end
    end

    # Returns the stripped contents of the line in which the passed block returns true
    def first(from: nil, to: nil, &block)
      each_with_depth(from: from, to: to) do |line, depth, index|
        return line if block.call(line, depth, index)
      end
    end
    alias_method :find, :first

    # Returns the stripped contents of the last line where the passed block returns true
    def last(from: nil, to: nil, &block)
      retval = nil

      each_with_depth(from: from, to: nil) do |line, depth, index|
        retval = line if block.call(line, depth, index)
      end

      retval
    end

    # Returns an array of stripped lines for each line where the passed block returns true
    def select(from: nil, to: nil, &block)
      retval = []

      each_with_depth(from: from, to: to) do |line, depth, index|
        retval << line if (block_given? == false || block.call(line, depth, index))
      end

      retval
    end

  end
end

Version data entries

438 entries across 438 versions & 1 rubygems

Version Path
effective_resources-2.27.9 app/models/effective/code_reader.rb
effective_resources-2.27.8 app/models/effective/code_reader.rb
effective_resources-2.27.7 app/models/effective/code_reader.rb
effective_resources-2.27.6 app/models/effective/code_reader.rb
effective_resources-2.27.5 app/models/effective/code_reader.rb
effective_resources-2.27.4 app/models/effective/code_reader.rb
effective_resources-2.27.3 app/models/effective/code_reader.rb
effective_resources-2.27.2 app/models/effective/code_reader.rb
effective_resources-2.27.1 app/models/effective/code_reader.rb
effective_resources-2.27.0 app/models/effective/code_reader.rb
effective_resources-2.26.5 app/models/effective/code_reader.rb
effective_resources-2.26.4 app/models/effective/code_reader.rb
effective_resources-2.26.3 app/models/effective/code_reader.rb
effective_resources-2.26.2 app/models/effective/code_reader.rb
effective_resources-2.26.1 app/models/effective/code_reader.rb
effective_resources-2.26.0 app/models/effective/code_reader.rb
effective_resources-2.25.16 app/models/effective/code_reader.rb
effective_resources-2.25.15 app/models/effective/code_reader.rb
effective_resources-2.25.14 app/models/effective/code_reader.rb
effective_resources-2.25.13 app/models/effective/code_reader.rb