Class: Maccro::CodeRange

Inherits:
Object
  • Object
show all
Defined in:
lib/maccro/code_range.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(first_lineno, first_column, last_lineno, last_column) ⇒ CodeRange

Returns a new instance of CodeRange.



11
12
13
14
15
16
# File 'lib/maccro/code_range.rb', line 11

def initialize(first_lineno, first_column, last_lineno, last_column)
  @first_lineno = first_lineno
  @first_column = first_column
  @last_lineno = last_lineno
  @last_column = last_column
end

Instance Attribute Details

#first_columnObject (readonly)

Returns the value of attribute first_column.



9
10
11
# File 'lib/maccro/code_range.rb', line 9

def first_column
  @first_column
end

#first_linenoObject (readonly)

Returns the value of attribute first_lineno.



9
10
11
# File 'lib/maccro/code_range.rb', line 9

def first_lineno
  @first_lineno
end

#last_columnObject (readonly)

Returns the value of attribute last_column.



9
10
11
# File 'lib/maccro/code_range.rb', line 9

def last_column
  @last_column
end

#last_linenoObject (readonly)

Returns the value of attribute last_lineno.



9
10
11
# File 'lib/maccro/code_range.rb', line 9

def last_lineno
  @last_lineno
end

Class Method Details

.from_node(ast) ⇒ Object



5
6
7
# File 'lib/maccro/code_range.rb', line 5

def self.from_node(ast)
  CodeRange.new(ast.first_lineno, ast.first_column, ast.last_lineno, ast.last_column)
end

Instance Method Details

#<=>(other) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/maccro/code_range.rb', line 25

def <=>(other)
  if @first_lineno < other.first_lineno
    -1
  elsif @first_lineno == other.first_lineno
    if @first_column < other.first_column
      -1
    elsif @first_column == other.first_column
      if @last_lineno < other.last_lineno
        -1
      elsif @last_lineno == other.last_lineno
        if @last_column < other.last_column
          -1
        elsif @last_column == other.last_column
          0
        else # @last_column > other.last_column
          1
        end
      else # @last_lineno > other.last_lineno
        1
      end
    else # @first_column > other.first_column
      1
    end
  else # @first_lineno > other.first_lineno
    1
  end
end

#==(other) ⇒ Object



18
19
20
21
22
23
# File 'lib/maccro/code_range.rb', line 18

def ==(other)
  @first_lineno == other.first_lineno &&
    @first_column == other.first_column &&
    @last_lineno == other.last_lineno &&
    @last_column == other.last_column
end

#get(source) ⇒ Object



58
59
60
61
# File 'lib/maccro/code_range.rb', line 58

def get(source)
  range = CodeUtil.code_range_to_range(source, self)
  source[range]
end

#source(path) ⇒ Object



53
54
55
56
# File 'lib/maccro/code_range.rb', line 53

def source(path)
  source = File.open(path){|f| f.read } # open as binary?
  get(source)
end