Class: Modloc::Source

Inherits:
String
  • Object
show all
Defined in:
lib/modloc/source.rb

Overview

A Modloc source string

Defined Under Namespace

Classes: Line

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Source

Creates a new Modloc source object

Parameters:

  • file (String)


10
11
12
13
14
15
16
# File 'lib/modloc/source.rb', line 10

def initialize(file)
  @ext  = File.extname(file)
  @path = file
  super File.read file
rescue Errno::ENOENT
  super ''
end

Instance Attribute Details

#extObject (readonly)

Returns the value of attribute ext.



6
7
8
# File 'lib/modloc/source.rb', line 6

def ext
  @ext
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/modloc/source.rb', line 6

def path
  @path
end

Instance Method Details

#linesArray<Modloc::Source::Line>

Returns the sources lines as line objects

Returns:



20
21
22
# File 'lib/modloc/source.rb', line 20

def lines
  super.map { |line| Line.new line }
end

#locate_all(pattern) ⇒ Array

Locate all the locations matching a given pattern

Parameters:

  • pattern (Regexp)

Returns:

  • (Array)


27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/modloc/source.rb', line 27

def locate_all(pattern)
  [].tap do |locations|
    strip.lines.each_with_index.reduce('') do |contents, (line, index)|
      line_num = index + 1
      contents << line
      if contents.match(/#{pattern.source}\n\z/)
        locations << [path, line_num]
      end
      contents
    end
  end
end

#stripModloc::Source

Return the source without comments or whitespace for each line

Returns:



42
43
44
# File 'lib/modloc/source.rb', line 42

def strip
  dup.replace lines.map(&:strip).join
end