Class: RichText::Document::Entry

Inherits:
Node
  • Object
show all
Defined in:
lib/richtext/document/entry.rb

Overview

Entry

The Entry class extends the basic Node class and adds methods that make handling text a little nicer. Essentially the :text attribute is given special status by allowing it to a) be set during initialization, b) only visible in leaf nodes and c) copied over when adding children to leaf nodes.

Some attributes are also supported explicitly by the inclusion of special accesser methods. The attributes are are bold, italic, underline, color and font.

Instance Attribute Summary

Attributes inherited from Node

#attributes

Instance Method Summary collapse

Methods inherited from Node

#+, #==, #[], #[]=, #count, #each, #each_child, #each_leaf, #equal?, #initialize_copy, #inspect, #leaf?, #minimal?, #optimize, #size

Constructor Details

#initialize(text = nil, **attributes) ⇒ Entry

Initialize

Extend the default Node initializer by also accepting a string. It will, if given, be stored as a text attribute.



20
21
22
23
# File 'lib/richtext/document/entry.rb', line 20

def initialize(text = nil, **attributes)
  super attributes
  self[:text] = text if text
end

Instance Method Details

#<<(child) ⇒ Object

Append

Since the text attribute is treated differently, and only leaf nodes can expose it, it must be pushed to a new child if a) this node was a leaf prior to this method call and b) its text attribute is not empty.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/richtext/document/entry.rb', line 39

def <<(child)
  if leaf?
    # Remove the text entry from the node and put it in a new leaf node
    # among the children, unless it is empty
    if (t = @attributes.delete :text)
      create_child(t) unless t.empty?
    end
  end

  super
end

#bold=(b) ⇒ Object



90
91
92
# File 'lib/richtext/document/entry.rb', line 90

def bold=(b)
  self[:bold] = b ? true : false
end

#bold?Boolean

Bold

Returns:

  • (Boolean)


86
87
88
# File 'lib/richtext/document/entry.rb', line 86

def bold?
  self[:bold]
end

#colorObject

Color



116
117
118
# File 'lib/richtext/document/entry.rb', line 116

def color
  self[:color]
end

#color=(c) ⇒ Object



120
121
122
# File 'lib/richtext/document/entry.rb', line 120

def color=(c)
  self[:color] = c
end

#create_child(text = '', **attributes) ⇒ Object



51
52
53
# File 'lib/richtext/document/entry.rb', line 51

def create_child(text = '', **attributes)
  super attributes.merge(text: text)
end

#fontObject

Font



126
127
128
# File 'lib/richtext/document/entry.rb', line 126

def font
  self[:font]
end

#font=(f) ⇒ Object



130
131
132
# File 'lib/richtext/document/entry.rb', line 130

def font=(f)
  self[:font] = f
end

#italic=(i) ⇒ Object



100
101
102
# File 'lib/richtext/document/entry.rb', line 100

def italic=(i)
  self[:italic] = i ? true : false
end

#italic?Boolean

Italic

Returns:

  • (Boolean)


96
97
98
# File 'lib/richtext/document/entry.rb', line 96

def italic?
  self[:italic]
end

#optimize!Object

Optimize!

See RichText::Node#optimize! for a description of the fundemental behavior. Entries differ from regular Nodes in that leaf children with no text in them will be removed.



60
61
62
# File 'lib/richtext/document/entry.rb', line 60

def optimize!
  super { |child| !child.leaf? || !child.text.empty? }
end

#textObject

Text

Read the text of the node. This will return nil unless the node is a leaf node. Note that nodes that are not leafs can have the text entry, but it is discouraged by dissalowing access using this method.



30
31
32
# File 'lib/richtext/document/entry.rb', line 30

def text
  self[:text] || '' if leaf?
end

#to_s(&block) ⇒ Object

To String

Combine the text from all the leaf nodes in the tree, from left to right. If a block is given the node, along with its text will be passed as arguments. The block will be called recursivly, starting at the leaf nodes and propagating up until the entire tree has been “rendered” in this way.



71
72
73
74
75
76
77
78
79
80
# File 'lib/richtext/document/entry.rb', line 71

def to_s(&block)
  string =
    if leaf?
      text
    else
      @children.reduce('') { |a, e| a + e.to_s(&block) }
    end

  block_given? ? yield(self, string) : string
end

#underline=(u) ⇒ Object



110
111
112
# File 'lib/richtext/document/entry.rb', line 110

def underline=(u)
  self[:underline] = u ? true : false
end

#underline?Boolean

Underline

Returns:

  • (Boolean)


106
107
108
# File 'lib/richtext/document/entry.rb', line 106

def underline?
  self[:underline]
end