Class: LintTrappings::Location

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/lint_trappings/location.rb

Overview

Store location of a Lint in a document.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line = 1, column = 1) ⇒ Location

Returns a new instance of Location.

Parameters:

  • line (Integer) (defaults to: 1)

    One-based index

  • column (Integer) (defaults to: 1)

    One-based index

Raises:

  • (ArgumentError)


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

def initialize(line = 1, column = 1)
  raise ArgumentError, "Line must be >= 0, but was #{line}" if line < 0
  raise ArgumentError, "Column must be >= 0, but was #{column}" if column < 0

  @line   = line
  @column = column
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



6
7
8
# File 'lib/lint_trappings/location.rb', line 6

def column
  @column
end

#lineObject (readonly)

Returns the value of attribute line.



6
7
8
# File 'lib/lint_trappings/location.rb', line 6

def line
  @line
end

Instance Method Details

#<=>(other) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/lint_trappings/location.rb', line 26

def <=>(other)
  [:line, :column].each do |attr|
    result = send(attr) <=> other.send(attr)
    return result unless result == 0
  end

  0
end

#==(other) ⇒ Object Also known as: eql?



18
19
20
21
22
# File 'lib/lint_trappings/location.rb', line 18

def ==(other)
  [:line, :column].all? do |attr|
    send(attr) == other.send(attr)
  end
end

#to_sObject



35
36
37
# File 'lib/lint_trappings/location.rb', line 35

def to_s
  "(#{line},#{column})"
end