Class: RichText::Document::Entry
- Defined in:
- lib/richtext/document/entry.rb
Instance Attribute Summary
Attributes inherited from Node
Instance Method Summary collapse
-
#add(*new_children) ⇒ Object
(also: #<<)
Add child.
- #bold=(b) ⇒ Object
-
#bold? ⇒ Boolean
Bold.
-
#color ⇒ Object
Color.
- #color=(c) ⇒ Object
-
#font ⇒ Object
Font.
- #font=(f) ⇒ Object
-
#initialize(text = nil, **attributes) ⇒ Entry
constructor
Initialize.
- #italic=(i) ⇒ Object
-
#italic? ⇒ Boolean
Italic.
-
#text ⇒ Object
Text.
-
#to_s(&block) ⇒ Object
To String.
- #underline=(u) ⇒ Object
-
#underline? ⇒ Boolean
Underline.
Methods inherited from Node
#+, #==, #===, #[], #[]=, #count, #each, #each_child, #each_leaf, #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.
21 22 23 24 |
# File 'lib/richtext/document/entry.rb', line 21 def initialize text = nil, **attributes super attributes self[:text] = text if text end |
Instance Method Details
#add(*new_children) ⇒ Object Also known as: <<
Add child
A child is either another node or any object that respond to #to_s.
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/richtext/document/entry.rb', line 46 def add *new_children 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) new_children.unshift self.class.new(t) unless t.empty? end end super end |
#bold=(b) ⇒ Object
89 90 91 |
# File 'lib/richtext/document/entry.rb', line 89 def bold= b self[:bold] = b ? true : false end |
#bold? ⇒ Boolean
Bold
85 86 87 |
# File 'lib/richtext/document/entry.rb', line 85 def bold? self[:bold] end |
#color ⇒ Object
Color
121 122 123 |
# File 'lib/richtext/document/entry.rb', line 121 def color self[:color] end |
#color=(c) ⇒ Object
125 126 127 |
# File 'lib/richtext/document/entry.rb', line 125 def color= c self[:color] = c end |
#font ⇒ Object
Font
133 134 135 |
# File 'lib/richtext/document/entry.rb', line 133 def font self[:font] end |
#font=(f) ⇒ Object
137 138 139 |
# File 'lib/richtext/document/entry.rb', line 137 def font= f self[:font] = f end |
#italic=(i) ⇒ Object
101 102 103 |
# File 'lib/richtext/document/entry.rb', line 101 def italic= i self[:italic] = i ? true : false end |
#italic? ⇒ Boolean
Italic
97 98 99 |
# File 'lib/richtext/document/entry.rb', line 97 def italic? self[:italic] end |
#text ⇒ Object
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.
33 34 35 36 37 38 39 |
# File 'lib/richtext/document/entry.rb', line 33 def text if leaf? self[:text] || '' else nil end 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.
69 70 71 72 73 74 75 |
# File 'lib/richtext/document/entry.rb', line 69 def to_s &block string = leaf? ? text : @children.reduce('') {|str, child| str + child.to_s(&block) } block_given? ? yield(self, string) : string end |
#underline=(u) ⇒ Object
113 114 115 |
# File 'lib/richtext/document/entry.rb', line 113 def underline= u self[:underline] = u ? true : false end |
#underline? ⇒ Boolean
Underline
109 110 111 |
# File 'lib/richtext/document/entry.rb', line 109 def underline? self[:underline] end |