Class: Spellr::Token

Inherits:
String show all
Defined in:
lib/spellr/token.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from String

#spellr_normalize

Constructor Details

#initialize(string, line: string, location: ColumnLocation.new) ⇒ Token

Returns a new instance of Token.



20
21
22
23
24
# File 'lib/spellr/token.rb', line 20

def initialize(string, line: string, location: ColumnLocation.new)
  @location = location
  @line = line
  super(string)
end

Instance Attribute Details

#lineObject

Returns the value of attribute line.



18
19
20
# File 'lib/spellr/token.rb', line 18

def line
  @line
end

#locationObject (readonly)

Returns the value of attribute location.



18
19
20
# File 'lib/spellr/token.rb', line 18

def location
  @location
end

#replacementObject (readonly)

Returns the value of attribute replacement.



18
19
20
# File 'lib/spellr/token.rb', line 18

def replacement
  @replacement
end

Instance Method Details

#byte_rangeObject

leftovers:allow i don’t want to delete this



42
43
44
45
# File 'lib/spellr/token.rb', line 42

def byte_range # leftovers:allow i don't want to delete this
  @byte_range ||=
    location.byte_offset...(location.byte_offset + bytesize)
end

#case_methodObject



70
71
72
73
74
75
76
77
# File 'lib/spellr/token.rb', line 70

def case_method
  @case_method ||= case self
  when /\A[[:lower:]]+\z/ then :downcase
  when /\A[[:upper:]]+\z/ then :upcase
  when /\A[[:upper:]][[:lower:]]*\z/ then :capitalize
  else :itself
  end
end

#char_rangeObject

:nocov:



37
38
39
40
# File 'lib/spellr/token.rb', line 37

def char_range
  @char_range ||=
    location.char_offset...(location.char_offset + length)
end

#coordinatesObject



57
58
59
# File 'lib/spellr/token.rb', line 57

def coordinates
  location.coordinates
end

#file_byte_rangeObject

leftovers:allow i don’t want to delete this



52
53
54
55
# File 'lib/spellr/token.rb', line 52

def file_byte_range # leftovers:allow i don't want to delete this
  @file_byte_range ||=
    location.absolute_byte_offset...(location.absolute_byte_offset + bytesize)
end

#file_char_rangeObject



47
48
49
50
# File 'lib/spellr/token.rb', line 47

def file_char_range
  @file_char_range ||=
    location.absolute_char_offset...(location.absolute_char_offset + length)
end

#highlight(range = char_range) ⇒ Object



61
62
63
# File 'lib/spellr/token.rb', line 61

def highlight(range = char_range)
  "#{slice(0...(range.first))}#{Spellr::StringFormat.red slice(range)}#{slice(range.last..-1)}"
end

#inspectObject

:nocov:



32
33
34
# File 'lib/spellr/token.rb', line 32

def inspect
  "#<#{self.class.name} #{to_s.inspect} @#{location}>"
end

#replace(replacement) ⇒ Object



65
66
67
68
# File 'lib/spellr/token.rb', line 65

def replace(replacement)
  @replacement = replacement
  location.file.insert(replacement, file_char_range)
end