Class: Spellr::Token

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#lineObject (readonly)

Returns the value of attribute line.



10
11
12
# File 'lib/spellr/token.rb', line 10

def line
  @line
end

#locationObject (readonly)

Returns the value of attribute location.



10
11
12
# File 'lib/spellr/token.rb', line 10

def location
  @location
end

Class Method Details

.normalize(value) ⇒ Object



11
12
13
14
15
# File 'lib/spellr/token.rb', line 11

def self.normalize(value)
  return value.normalize if value.is_a?(Spellr::Token)

  value.strip.downcase.unicode_normalize.tr('', "'") + "\n"
end

.wrap(value) ⇒ Object



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

def self.wrap(value)
  return value if value.is_a?(Spellr::Token)

  Spellr::Token.new(value || '')
end

Instance Method Details

#byte_rangeObject



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

def byte_range
  @byte_range ||= location.byte_offset...(location.byte_offset + bytesize)
end

#char_rangeObject



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

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

#coordinatesObject



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

def coordinates
  location.coordinates
end

#file_nameObject



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

#inspectObject



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

#normalizeObject



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

#stripObject



29
30
31
32
33
34
35
# File 'lib/spellr/token.rb', line 29

def strip
  @strip ||= begin
    lstripped = lstrip
    new_column_location = lstripped_column_location(lstripped)
    Token.new(lstripped.rstrip, line: line, location: new_column_location)
  end
end