Class: Kotoshu::Documents::TextNode

Inherits:
Object
  • Object
show all
Defined in:
lib/kotoshu/documents/document.rb

Overview

Text node abstraction for structured documents.

Represents a span of text in a document with location information. Used for spell checking individual text elements in structured formats.

Examples:

Creating a text node

node = TextNode.new("Hello world", location: Location.new(line: 5, column: 0))
node.text  # => "Hello world"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, location:, node_path: nil) ⇒ TextNode

Create a new text node.

Parameters:

  • text (String)

    The text content

  • location (Location)

    Location of the text

  • node_path (Array, nil) (defaults to: nil)

    Path in document AST



24
25
26
27
28
29
# File 'lib/kotoshu/documents/document.rb', line 24

def initialize(text, location:, node_path: nil)
  @text = text
  @location = location
  @node_path = node_path
  freeze
end

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



17
18
19
# File 'lib/kotoshu/documents/document.rb', line 17

def location
  @location
end

#node_pathObject (readonly)

Returns the value of attribute node_path.



17
18
19
# File 'lib/kotoshu/documents/document.rb', line 17

def node_path
  @node_path
end

#textObject (readonly)

Returns the value of attribute text.



17
18
19
# File 'lib/kotoshu/documents/document.rb', line 17

def text
  @text
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Check if this equals another text node.

Parameters:

  • other (Object)

    Another object

Returns:

  • (Boolean)

    True if text and location match



42
43
44
45
46
# File 'lib/kotoshu/documents/document.rb', line 42

def ==(other)
  return false unless other.is_a?(TextNode)

  @text == other.text && @location == other.location
end

#hashInteger

Hash code for hash table usage.

Returns:

  • (Integer)

    Hash code



52
53
54
# File 'lib/kotoshu/documents/document.rb', line 52

def hash
  [@text, @location].hash
end

#to_sString Also known as: inspect

String representation.

Returns:

  • (String)

    Human-readable representation



59
60
61
62
63
64
65
# File 'lib/kotoshu/documents/document.rb', line 59

def to_s
  if @location.line_column?
    "#{@location}: #{@text}"
  else
    @text
  end
end

#wordsArray<String>

Get words from this text node.

Returns:

  • (Array<String>)

    Words in the text



34
35
36
# File 'lib/kotoshu/documents/document.rb', line 34

def words
  @text.split
end