Module: Arbre::Element::Building
- Included in:
- Arbre::Element
- Defined in:
- lib/arbre/element/building.rb
Overview
Element building concern. Contains methods pertaining to building and inserting child elements.
Defined Under Namespace
Modules: BuilderMethod
Class Method Summary collapse
-
.included(klass) ⇒ Object
Builder methods.
Instance Method Summary collapse
-
#append_within(element, &block) ⇒ Object
(also: #within)
Executes a block within the context of the given element, or DOM query.
-
#append_within?(element, &block) ⇒ Boolean
(also: #within?)
Same as ##append_within, except this doesn’t do anything if the given element is
nil
or not found. -
#build(klass, *args, &block) ⇒ Object
Builds an element of the given class using the given arguments and block, in the same arbre context as this element.
- #current_element ⇒ Object
- #current_flow ⇒ Object
-
#insert(klass, *args, &block) ⇒ Object
Builds an element of the given class using the given arguments and block, in the same arbre context as this element, and adds it to the current arbre element’s children.
-
#insert_child(child) ⇒ Object
Inserts a child element at the right place in the child array, taking the current flow into account.
-
#prepend_within(element, &block) ⇒ Object
Executes a block within the context of the given element, or DOM query.
-
#prepend_within?(element, &block) ⇒ Boolean
Same as ##prepend_within, except this doesn’t do anything if the given element is
nil
or not found. -
#temporary(&block) ⇒ Object
Builds a temporary container using the given block, but doesn’t add it to the tree.
Class Method Details
.included(klass) ⇒ Object
Builder methods
15 16 17 18 |
# File 'lib/arbre/element/building.rb', line 15 def self.included(klass) klass.send :include, BuilderMethods klass.send :extend, BuilderMethod end |
Instance Method Details
#append_within(element, &block) ⇒ Object Also known as: within
Executes a block within the context of the given element, or DOM query.
60 61 62 63 |
# File 'lib/arbre/element/building.rb', line 60 def append_within(element, &block) element = find(element).first if element.is_a?(String) arbre_context.with_current element: element, flow: :append, &block end |
#append_within?(element, &block) ⇒ Boolean Also known as: within?
Same as ##append_within, except this doesn’t do anything if the given element is nil
or not found.
67 68 69 70 |
# File 'lib/arbre/element/building.rb', line 67 def append_within?(element, &block) element = find(element).first if element.is_a?(String) arbre_context.with_current element: element, flow: :append, &block if element end |
#build(klass, *args, &block) ⇒ Object
Builds an element of the given class using the given arguments and block, in the same arbre context as this element.
40 41 42 43 44 |
# File 'lib/arbre/element/building.rb', line 40 def build(klass, *args, &block) element = klass.new(arbre_context) within(element) { element.build! *args, &block } element end |
#current_element ⇒ Object
156 157 158 |
# File 'lib/arbre/element/building.rb', line 156 def current_element arbre_context.current_element end |
#current_flow ⇒ Object
160 161 162 |
# File 'lib/arbre/element/building.rb', line 160 def current_flow arbre_context.current_flow end |
#insert(klass, *args, &block) ⇒ Object
Builds an element of the given class using the given arguments and block, in the same arbre context as this element, and adds it to the current arbre element’s children.
49 50 51 52 53 54 |
# File 'lib/arbre/element/building.rb', line 49 def insert(klass, *args, &block) element = klass.new(arbre_context) current_element.insert_child element within(element) { element.build! *args, &block } element end |
#insert_child(child) ⇒ Object
Inserts a child element at the right place in the child array, taking the current flow into account.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/arbre/element/building.rb', line 122 def insert_child(child) case current_flow when :append children << child when :prepend children.insert_at 0, child # Update the flow - the next element should be added after this one, not be # prepended. arbre_context.replace_current_flow [:after, child] else # flow: [ :before, element ] or [ :after, element ] operation, element = current_flow children.send :"insert_#{operation}", element, child if operation == :after # Now that we've inserted something after the element, we need to # make sure that the next element we insert will be after this one. arbre_context.replace_current_flow [:after, child] end end end |
#prepend_within(element, &block) ⇒ Object
Executes a block within the context of the given element, or DOM query. All elements are prepended.
77 78 79 80 |
# File 'lib/arbre/element/building.rb', line 77 def prepend_within(element, &block) element = find(element).first if element.is_a?(String) arbre_context.with_current element: element, flow: :prepend, &block end |
#prepend_within?(element, &block) ⇒ Boolean
Same as ##prepend_within, except this doesn’t do anything if the given element is nil
or not found.
84 85 86 87 |
# File 'lib/arbre/element/building.rb', line 84 def prepend_within?(element, &block) element = find(element).first if element.is_a?(String) arbre_context.with_current element: element, flow: :prepend, &block if element end |
#temporary(&block) ⇒ Object
Builds a temporary container using the given block, but doesn’t add it to the tree. The block is executed within the current context.
152 153 154 |
# File 'lib/arbre/element/building.rb', line 152 def temporary(&block) build Element, &block end |