Class: Arbre::Element
- Inherits:
-
Object
- Object
- Arbre::Element
- Includes:
- Building, Html::Querying
- Defined in:
- lib/arbre/element.rb,
lib/arbre/element/building.rb
Overview
Base class for all Arbre elements. Rendering is not implemented, and should be implemented by subclasses.
Direct Known Subclasses
Defined Under Namespace
Modules: BuilderMethods, Building
Instance Method Summary collapse
-
#+(element) ⇒ Object
Set operations.
- #<<(child) ⇒ Object
-
#ancestors ⇒ ElementCollection
Retrieves all ancestors (ordered from near to far) for this element.
-
#arbre_context ⇒ Object
Context.
- #assigns ⇒ Object
-
#build! {|_self| ... } ⇒ Object
Override this method to build your element.
- #children ⇒ Object
- #content ⇒ Object
-
#content=(content) ⇒ Object
Content.
-
#descendants ⇒ ElementCollection
Retrieves all descendants (in prefix form) for this element.
- #empty? ⇒ Boolean
- #has_children? ⇒ Boolean
- #helpers ⇒ Object
- #indent_level ⇒ Object
-
#initialize(arbre_context = Arbre::Context.new) ⇒ Element
constructor
Initializes a new Arbre element.
-
#inspect ⇒ Object
Provide a clean element description when inspect is used.
- #orphan? ⇒ Boolean
-
#parent ⇒ Object
Hierarchy.
- #parent=(parent) ⇒ Object
-
#remove! ⇒ Object
Removes this element from its parent.
-
#respond_to?(method, include_private = false) ⇒ Boolean
Helpers & assigns accessing.
- #to_html ⇒ Object
- #to_s ⇒ Object
Methods included from Html::Querying
#child_tags, #descendant_tags, #find, #find_by_classes, #find_by_id, #find_by_tag, #find_by_tag_and_classes, #find_first
Methods included from Building
#append_within, #build, #current_element, #current_flow, included, #insert, #insert_child, #prepend_within, #temporary
Constructor Details
#initialize(arbre_context = Arbre::Context.new) ⇒ Element
Initializes a new Arbre element. Pass an existing Arbre context to re-use it.
16 17 18 19 20 21 |
# File 'lib/arbre/element.rb', line 16 def initialize(arbre_context = Arbre::Context.new) @_arbre_context = arbre_context @_children = ChildElementCollection.new(self) expose_assigns end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
Access helper methods from any Arbre element through its context.
176 177 178 179 180 181 182 183 |
# File 'lib/arbre/element.rb', line 176 def method_missing(name, *args, &block) if helpers && helpers.respond_to?(name) define_helper_method name send name, *args, &block else super end end |
Instance Method Details
#+(element) ⇒ Object
Set operations
120 121 122 123 124 125 126 |
# File 'lib/arbre/element.rb', line 120 def +(element) if element.is_a?(Enumerable) ElementCollection.new([self] + element) else ElementCollection.new([ self, element]) end end |
#<<(child) ⇒ Object
57 58 59 |
# File 'lib/arbre/element.rb', line 57 def <<(child) children << child end |
#ancestors ⇒ ElementCollection
Retrieves all ancestors (ordered from near to far) for this element.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/arbre/element.rb', line 75 def ancestors ancestors = ElementCollection.new unless orphan? ancestors << parent ancestors.concat parent.ancestors end ancestors end |
#arbre_context ⇒ Object
Context
26 27 28 |
# File 'lib/arbre/element.rb', line 26 def arbre_context @_arbre_context end |
#assigns ⇒ Object
30 31 32 |
# File 'lib/arbre/element.rb', line 30 def assigns arbre_context.assigns end |
#build! {|_self| ... } ⇒ Object
Override this method to build your element.
132 133 134 135 136 |
# File 'lib/arbre/element.rb', line 132 def build! yield self if block_given? self end |
#children ⇒ Object
48 49 50 |
# File 'lib/arbre/element.rb', line 48 def children @_children end |
#content ⇒ Object
113 114 115 |
# File 'lib/arbre/element.rb', line 113 def content children.to_s end |
#content=(content) ⇒ Object
Content
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/arbre/element.rb', line 101 def content=(content) children.clear case content when Element children << content when ElementCollection children.concat content else children << TextNode.from_string(content.to_s) end end |
#descendants ⇒ ElementCollection
Retrieves all descendants (in prefix form) for this element.
88 89 90 91 92 93 94 95 96 |
# File 'lib/arbre/element.rb', line 88 def descendants descendants = ElementCollection.new children.each do |child| descendants << child descendants.concat child.descendants end descendants end |
#empty? ⇒ Boolean
65 66 67 |
# File 'lib/arbre/element.rb', line 65 def empty? !has_children? end |
#has_children? ⇒ Boolean
61 62 63 |
# File 'lib/arbre/element.rb', line 61 def has_children? children.any? end |
#helpers ⇒ Object
34 35 36 |
# File 'lib/arbre/element.rb', line 34 def helpers arbre_context.helpers end |
#indent_level ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/arbre/element.rb', line 138 def indent_level if parent parent.indent_level + 1 else 0 end end |
#inspect ⇒ Object
Provide a clean element description when inspect is used.
155 156 157 |
# File 'lib/arbre/element.rb', line 155 def inspect "#<#{self.class.name}:0x#{object_id.to_s(16)}>" end |
#orphan? ⇒ Boolean
69 70 71 |
# File 'lib/arbre/element.rb', line 69 def orphan? !parent end |
#parent ⇒ Object
Hierarchy
41 42 43 |
# File 'lib/arbre/element.rb', line 41 def parent @_parent end |
#parent=(parent) ⇒ Object
44 45 46 |
# File 'lib/arbre/element.rb', line 44 def parent=(parent) @_parent = parent end |
#remove! ⇒ Object
Removes this element from its parent.
53 54 55 |
# File 'lib/arbre/element.rb', line 53 def remove! parent.children.remove self if parent end |
#respond_to?(method, include_private = false) ⇒ Boolean
Helpers & assigns accessing
162 163 164 |
# File 'lib/arbre/element.rb', line 162 def respond_to?(method, include_private = false) super || (helpers && helpers.respond_to?(method)) end |
#to_html ⇒ Object
150 151 152 |
# File 'lib/arbre/element.rb', line 150 def to_html to_s end |
#to_s ⇒ Object
146 147 148 |
# File 'lib/arbre/element.rb', line 146 def to_s content end |