Class: Modloc::Source
- Inherits:
-
String
- Object
- String
- Modloc::Source
- Defined in:
- lib/modloc/source.rb
Overview
A Modloc source string
Defined Under Namespace
Classes: Line
Instance Attribute Summary collapse
-
#ext ⇒ Object
readonly
Returns the value of attribute ext.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(file) ⇒ Source
constructor
Creates a new Modloc source object.
-
#lines ⇒ Array<Modloc::Source::Line>
Returns the sources lines as line objects.
-
#locate_all(pattern) ⇒ Array
Locate all the locations matching a given pattern.
-
#strip ⇒ Modloc::Source
Return the source without comments or whitespace for each line.
Constructor Details
#initialize(file) ⇒ Source
Creates a new Modloc source object
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
#ext ⇒ Object (readonly)
Returns the value of attribute ext.
6 7 8 |
# File 'lib/modloc/source.rb', line 6 def ext @ext end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
6 7 8 |
# File 'lib/modloc/source.rb', line 6 def path @path end |
Instance Method Details
#lines ⇒ Array<Modloc::Source::Line>
Returns the sources lines as line objects
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
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 |
#strip ⇒ Modloc::Source
Return the source without comments or whitespace for each line
42 43 44 |
# File 'lib/modloc/source.rb', line 42 def strip dup.replace lines.map(&:strip).join end |