Class: LintFu::FileRange

Inherits:
Object
  • Object
show all
Defined in:
lib/lint_fu/file_range.rb

Constant Summary collapse

NONE =
FileRange.new('/dev/null', 0)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, min_line, max_line = nil, content = nil) ⇒ FileRange

Returns a new instance of FileRange.



5
6
7
8
9
10
# File 'lib/lint_fu/file_range.rb', line 5

def initialize(filename, min_line, max_line=nil, content=nil)
  @filename = File.expand_path(filename)
  @min_line = min_line
  @max_line = max_line || min_line
  @content = content
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



3
4
5
# File 'lib/lint_fu/file_range.rb', line 3

def filename
  @filename
end

#max_lineObject (readonly)

Returns the value of attribute max_line.



3
4
5
# File 'lib/lint_fu/file_range.rb', line 3

def max_line
  @max_line
end

#min_lineObject (readonly)

Returns the value of attribute min_line.



3
4
5
# File 'lib/lint_fu/file_range.rb', line 3

def min_line
  @min_line
end

Instance Method Details

#==(other) ⇒ Object



29
30
31
32
33
# File 'lib/lint_fu/file_range.rb', line 29

def ==(other)
  (self.filename == other.filename) &&
  (self.min_line == other.min_line) &&
  (self.max_line == other.max_line)
end

#contentObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lint_fu/file_range.rb', line 16

def content
  unless @content
    #TODO optimize
    all_content = File.readlines(self.file)

    min_line = [self.min_line - 1, 0].max
    max_line = [self.max_line - 1, all_content.size - 1].max
    @content = all_content[min_line..max_line]
  end

  return @content
end

#include?(range) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/lint_fu/file_range.rb', line 35

def include?(range)
  (self.filename == range.filename) &&
  (self.min_line <= range.min_line) &&
  (self.max_line >= range.max_line)
end

#lineObject



12
13
14
# File 'lib/lint_fu/file_range.rb', line 12

def line
  min_line
end