Class: Ruby::Rego::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/rego/location.rb

Overview

Represents a source location in a Rego policy.

Examples:

location = Ruby::Rego::Location.new(line: 3, column: 12, offset: 42, length: 5)
location.to_s # => "line 3, column 12, offset 42, length 5"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line:, column:, offset: nil, length: nil) ⇒ Location

Create a new source location.

Parameters:

  • line (Integer)

    1-based line number

  • column (Integer)

    1-based column number

  • offset (Integer, nil) (defaults to: nil)

    0-based character offset

  • length (Integer, nil) (defaults to: nil)

    length of the token or span



33
34
35
36
37
38
# File 'lib/ruby/rego/location.rb', line 33

def initialize(line:, column:, offset: nil, length: nil)
  @line = line
  @column = column
  @offset = offset
  @length = length
end

Instance Attribute Details

#columnInteger (readonly)

Column number.

Returns:

  • (Integer)


48
49
50
# File 'lib/ruby/rego/location.rb', line 48

def column
  @column
end

#lengthInteger? (readonly)

Length of the token or span.

Returns:

  • (Integer, nil)


58
59
60
# File 'lib/ruby/rego/location.rb', line 58

def length
  @length
end

#lineInteger (readonly)

Line number.

Returns:

  • (Integer)


43
44
45
# File 'lib/ruby/rego/location.rb', line 43

def line
  @line
end

#offsetInteger? (readonly)

Character offset.

Returns:

  • (Integer, nil)


53
54
55
# File 'lib/ruby/rego/location.rb', line 53

def offset
  @offset
end

Class Method Details

.from(position) ⇒ Location

Coerce a hash or location into a Location instance.

Parameters:

  • position (Location, Hash)

    position data

Returns:



16
17
18
19
20
21
22
23
24
25
# File 'lib/ruby/rego/location.rb', line 16

def self.from(position)
  return position if position.is_a?(Location)

  new(
    line: position.fetch(:line),
    column: position.fetch(:column),
    offset: position[:offset],
    length: position[:length]
  )
end

Instance Method Details

#to_sString

Convert the location to a readable string.

Returns:

  • (String)


63
64
65
66
67
68
69
70
# File 'lib/ruby/rego/location.rb', line 63

def to_s
  {
    line: line,
    column: column,
    offset: offset,
    length: length
  }.compact.map { |key, value| "#{key} #{value}" }.join(", ")
end