Class: Spellr::Token
- Inherits:
-
String
- Object
- String
- Spellr::Token
- Defined in:
- lib/spellr/token.rb
Instance Attribute Summary collapse
-
#line ⇒ Object
readonly
Returns the value of attribute line.
-
#location ⇒ Object
readonly
Returns the value of attribute location.
Class Method Summary collapse
Instance Method Summary collapse
- #byte_range ⇒ Object
- #char_range ⇒ Object
- #coordinates ⇒ Object
- #file_name ⇒ Object
- #highlight(range = char_range) ⇒ Object
-
#initialize(string, line: string, location: ColumnLocation.new) ⇒ Token
constructor
A new instance of Token.
- #inspect ⇒ Object
- #lstripped_column_location(lstripped) ⇒ Object
- #normalize ⇒ Object
- #replace(replacement) ⇒ Object
- #strip ⇒ Object
Constructor Details
#initialize(string, line: string, location: ColumnLocation.new) ⇒ Token
Returns a new instance of Token.
23 24 25 26 27 |
# File 'lib/spellr/token.rb', line 23 def initialize(string, line: string, location: ColumnLocation.new) @location = location @line = line super(string) end |
Instance Attribute Details
#line ⇒ Object (readonly)
Returns the value of attribute line.
10 11 12 |
# File 'lib/spellr/token.rb', line 10 def line @line end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
10 11 12 |
# File 'lib/spellr/token.rb', line 10 def location @location end |
Class Method Details
Instance Method Details
#byte_range ⇒ Object
57 58 59 |
# File 'lib/spellr/token.rb', line 57 def byte_range @byte_range ||= location.byte_offset...(location.byte_offset + bytesize) end |
#char_range ⇒ Object
53 54 55 |
# File 'lib/spellr/token.rb', line 53 def char_range @char_range ||= location.char_offset...(location.char_offset + length) end |
#coordinates ⇒ Object
61 62 63 |
# File 'lib/spellr/token.rb', line 61 def coordinates location.coordinates end |
#file_name ⇒ Object
79 80 81 |
# File 'lib/spellr/token.rb', line 79 def file_name location.file_name end |
#highlight(range = char_range) ⇒ Object
65 66 67 |
# File 'lib/spellr/token.rb', line 65 def highlight(range = char_range) "#{slice(0...(range.first))}#{Spellr::StringFormat.red slice(range)}#{slice(range.last..-1)}" end |
#inspect ⇒ Object
49 50 51 |
# File 'lib/spellr/token.rb', line 49 def inspect "#<#{self.class.name} #{to_s.inspect} @#{location}>" end |
#lstripped_column_location(lstripped) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/spellr/token.rb', line 37 def lstripped_column_location(lstripped) ColumnLocation.new( byte_offset: bytesize - lstripped.bytesize, char_offset: length - lstripped.length, line_location: location.line_location ) end |
#normalize ⇒ Object
45 46 47 |
# File 'lib/spellr/token.rb', line 45 def normalize @normalize ||= self.class.normalize(to_s) end |
#replace(replacement) ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/spellr/token.rb', line 69 def replace(replacement) ::File.open(file_name, 'r+') do |f| body = f.read body[location.absolute_char_offset...(location.absolute_char_offset + length)] = replacement f.rewind f.truncate(0) f.write(body) end end |