Class: Ruby::Rego::Location
- Inherits:
-
Object
- Object
- Ruby::Rego::Location
- Defined in:
- lib/ruby/rego/location.rb
Overview
Represents a source location in a Rego policy.
Instance Attribute Summary collapse
-
#column ⇒ Integer
readonly
Column number.
-
#length ⇒ Integer?
readonly
Length of the token or span.
-
#line ⇒ Integer
readonly
Line number.
-
#offset ⇒ Integer?
readonly
Character offset.
Class Method Summary collapse
-
.from(position) ⇒ Location
Coerce a hash or location into a Location instance.
Instance Method Summary collapse
-
#initialize(line:, column:, offset: nil, length: nil) ⇒ Location
constructor
Create a new source location.
-
#to_s ⇒ String
Convert the location to a readable string.
Constructor Details
#initialize(line:, column:, offset: nil, length: nil) ⇒ Location
Create a new source location.
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
#column ⇒ Integer (readonly)
Column number.
48 49 50 |
# File 'lib/ruby/rego/location.rb', line 48 def column @column end |
#length ⇒ Integer? (readonly)
Length of the token or span.
58 59 60 |
# File 'lib/ruby/rego/location.rb', line 58 def length @length end |
#line ⇒ Integer (readonly)
Line number.
43 44 45 |
# File 'lib/ruby/rego/location.rb', line 43 def line @line end |
#offset ⇒ Integer? (readonly)
Character offset.
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.
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_s ⇒ String
Convert the location to a readable 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 |