Class: Prosereflect::Text

Inherits:
Node
  • Object
show all
Defined in:
lib/prosereflect/text.rb

Constant Summary collapse

PM_TYPE =
"text"

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#add_child, #copy, #descendants, #find_all, #find_children, #find_first, #initialize, #marks, #marks=, #node, #nodes_between, #parse_content, #process_attrs_data, #raw_marks, #resolve, #to_yaml

Constructor Details

This class inherits a constructor from Prosereflect::Node

Class Method Details

.create(text = "", marks = nil) ⇒ Object



18
19
20
# File 'lib/prosereflect/text.rb', line 18

def self.create(text = "", marks = nil)
  new(text: text, marks: marks)
end

Instance Method Details

#cut(from = 0, to = nil) ⇒ Object

Return a copy of this text node with content restricted to range



37
38
39
40
41
# File 'lib/prosereflect/text.rb', line 37

def cut(from = 0, to = nil)
  txt = text || ""
  to ||= txt.length
  self.class.new(text: txt[from...to], marks: raw_marks)
end

#eq?(other) ⇒ Boolean

Check equality with another text node

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/prosereflect/text.rb', line 44

def eq?(other)
  return false unless other.is_a?(self.class)

  text == other.text && to_h == other.to_h
end

#node_sizeObject

Text node size is text length + 1 (for the opening token)



27
28
29
# File 'lib/prosereflect/text.rb', line 27

def node_size
  (text || "").length + 1
end

#text?Boolean

Text nodes are text nodes

Returns:

  • (Boolean)


32
33
34
# File 'lib/prosereflect/text.rb', line 32

def text?
  true
end

#text_contentObject



22
23
24
# File 'lib/prosereflect/text.rb', line 22

def text_content
  text || ""
end

#to_hObject

Override the to_h method to include the text attribute



51
52
53
54
55
# File 'lib/prosereflect/text.rb', line 51

def to_h
  result = super
  result["text"] = text
  result
end