module Arbre::Element::BuilderMethods

Public Class Methods

included(klass) click to toggle source
# File lib/arbre/element/builder_methods.rb, line 6
def self.included(klass)
  klass.extend ClassMethods
end

Public Instance Methods

build_tag(klass, *args) { || ... } click to toggle source
# File lib/arbre/element/builder_methods.rb, line 22
def build_tag(klass, *args, &block)
  tag = klass.new(arbre_context)
  tag.parent = current_arbre_element

  with_current_arbre_element tag do
    if block_given? && block.arity > 0
      tag.build(*args, &block)
    else
      tag.build(*args)
      append_return_block(yield) if block_given?
    end
  end

  tag
end
current_arbre_element() click to toggle source
# File lib/arbre/element/builder_methods.rb, line 44
def current_arbre_element
  arbre_context.current_arbre_element
end
insert_tag(klass, *args, &block) click to toggle source
# File lib/arbre/element/builder_methods.rb, line 38
def insert_tag(klass, *args, &block)
  tag = build_tag(klass, *args, &block)
  current_arbre_element.add_child(tag)
  tag
end
with_current_arbre_element(tag, &block) click to toggle source
# File lib/arbre/element/builder_methods.rb, line 48
def with_current_arbre_element(tag, &block)
  arbre_context.with_current_arbre_element(tag, &block)
end
Also aliased as: within
within(tag, &block)

Private Instance Methods

append_return_block(tag) click to toggle source

Appends the value to the current DOM element if there are no existing DOM Children and it responds to to_s

# File lib/arbre/element/builder_methods.rb, line 57
def append_return_block(tag)
  return nil if current_arbre_element.children?

  if appendable_tag?(tag)
    current_arbre_element << Arbre::HTML::TextNode.from_string(tag.to_s)
  end
end
appendable_tag?(tag) click to toggle source

Returns true if the object should be converted into a text node and appended into the DOM.

# File lib/arbre/element/builder_methods.rb, line 67
def appendable_tag?(tag)
  # Array.new.to_s prints out an empty array ("[]"). In
  # Arbre, we append the return value of blocks to the output, which
  # can cause empty arrays to show up within the output. To get
  # around this, we check if the object responds to #empty?
  if tag.respond_to?(:empty?) && tag.empty?
    false
  else
    !tag.is_a?(Arbre::Element) && tag.respond_to?(:to_s)
  end

end