Class: Yoga::Location
- Inherits:
-
Object
- Object
- Yoga::Location
- Defined in:
- lib/yoga/location.rb
Overview
A location in a file. This can be used for debugging purposes.
Instance Attribute Summary collapse
-
#column ⇒ ::Range
readonly
The column the location on.
-
#file ⇒ ::String
readonly
The file the location is positioned in.
-
#hash ⇒ ::Numeric
readonly
private
A "hash" of the location.
-
#line ⇒ ::Range
readonly
The line the location is on.
Class Method Summary collapse
-
.default(file = "<unknown>") ⇒ Location
Creates a "default" location.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Determines if the other object is equal to the current instance.
-
#ensure_range(value) ⇒ ::Range
private
Ensures that the given value is a range.
-
#initialize(file, line, column) ⇒ Location
constructor
Initialize the location with the given information.
-
#inspect ⇒ ::String
Pretty inspect.
-
#range(value) ⇒ ::String
private
Returns a string version of the range.
-
#to_s ⇒ ::String
Creates a string representing this location in a file.
-
#union(other) ⇒ Location
(also: #|)
Unions this location with another location.
Constructor Details
#initialize(file, line, column) ⇒ Location
Initialize the location with the given information.
48 49 50 51 52 53 54 |
# File 'lib/yoga/location.rb', line 48 def initialize(file, line, column) @file = file.freeze @line = ensure_range(line).freeze @column = ensure_range(column).freeze @hash = [@file, @line, @column].hash freeze end |
Instance Attribute Details
#column ⇒ ::Range (readonly)
The column the location on. This can be a range of columns, or a single column.
24 25 26 |
# File 'lib/yoga/location.rb', line 24 def column @column end |
#file ⇒ ::String (readonly)
The file the location is positioned in. This should just be a string that uniquely identifies the file from all possible values of the location.
12 13 14 |
# File 'lib/yoga/location.rb', line 12 def file @file end |
#hash ⇒ ::Numeric (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
A "hash" of the location. This is a number that is meant to roughly represent the value of this location. Used primarily for the Hash class.
32 33 34 |
# File 'lib/yoga/location.rb', line 32 def hash @hash end |
#line ⇒ ::Range (readonly)
The line the location is on. This can be a range of lines, or a single line.
18 19 20 |
# File 'lib/yoga/location.rb', line 18 def line @line end |
Class Method Details
.default(file = "<unknown>") ⇒ Location
Creates a "default" location. This is a location that can be given if the location is unknown.
39 40 41 |
# File 'lib/yoga/location.rb', line 39 def self.default(file = "<unknown>") new(file, 0..0, 0..0) end |
Instance Method Details
#==(other) ⇒ Boolean
Determines if the other object is equal to the current instance. This
checks equal? first to determine if they are strict equals; otherwise,
it checks if the other is a Yoga::Location. If it is, it checks that the
properties are equal.
89 90 91 92 |
# File 'lib/yoga/location.rb', line 89 def ==(other) equal?(other) || other.is_a?(Location) && @file == other.file && @line == other.line && @column == other.column end |
#ensure_range(value) ⇒ ::Range (private)
Ensures that the given value is a range. If it's numeric, it's turned into a range with the same starting and ending values. If it's a range, it's returned. Otherwise, it fails.
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/yoga/location.rb', line 151 def ensure_range(value) case value when ::Numeric value..value when ::Range value else fail ArgumentError, "Unexpected #{value.class}, expected Range" end end |
#inspect ⇒ ::String
Pretty inspect.
66 67 68 |
# File 'lib/yoga/location.rb', line 66 def inspect "#<#{self.class} #{self}>" end |
#range(value) ⇒ ::String (private)
Returns a string version of the range. If there is no distance between
the starting and ending for the given range, it returns a string of the
value. Otherwise, it returns <starting>-<ending>.
168 169 170 171 |
# File 'lib/yoga/location.rb', line 168 def range(value) return value.first.to_s unless value.first != value.last "#{value.first}-#{value.last}" end |
#to_s ⇒ ::String
Creates a string representing this location in a file.
59 60 61 |
# File 'lib/yoga/location.rb', line 59 def to_s "#{file}:#{range(line)}.#{range(column)}" end |
#union(other) ⇒ Location Also known as: |
Unions this location with another location. This creates a new location, with the two locations combined. A conflict in the file name causes an error to be raised.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/yoga/location.rb', line 123 def union(other) fail ArgumentError, "Expected #{self.class}, got #{other.class}" \ unless other.is_a?(Location) oline = other.line ocolumn = other.column file = @file == other.file ? @file : "<unknown>" line = (@line.first < oline.first ? @line.first : oline.first).. (@line.last > oline.last ? @line.last : oline.last) column = (@column.first < ocolumn.first ? @column.first : ocolumn.first).. (@column.last > ocolumn.last ? @column.last : ocolumn.last) Location.new(file, line, column) end |