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

Instance Method Summary collapse

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

#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_elementObject



140
141
142
# File 'lib/arbre/element/building.rb', line 140

def current_element
  arbre_context.current_element
end

#current_flowObject



144
145
146
# File 'lib/arbre/element/building.rb', line 144

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.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/arbre/element/building.rb', line 106

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.



68
69
70
71
# File 'lib/arbre/element/building.rb', line 68

def prepend_within(element, &block)
  element = find(element).first if element.is_a?(String)
  arbre_context.with_current element: element, flow: :prepend, &block
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.



136
137
138
# File 'lib/arbre/element/building.rb', line 136

def temporary(&block)
  build Element, &block
end