class Arbre::Element

Attributes

arbre_context[R]
children[R]
parent[R]

Public Class Methods

new(arbre_context = Arbre::Context.new) click to toggle source
# File lib/arbre/element.rb, line 14
def initialize(arbre_context = Arbre::Context.new)
  @arbre_context = arbre_context
  @children = ElementCollection.new
  @parent = nil
end

Public Instance Methods

+(element) click to toggle source
# File lib/arbre/element.rb, line 147
def +(element)
  case element
  when Element, ElementCollection
  else
    element = Arbre::HTML::TextNode.from_string(element)
  end
  to_ary + element
end
<<(child) click to toggle source
# File lib/arbre/element.rb, line 65
def <<(child)
  add_child(child)
end
add_child(child) click to toggle source
# File lib/arbre/element.rb, line 37
def add_child(child)
  return unless child

  if child.is_a?(Array)
    child.each{|item| add_child(item) }
    return @children
  end

  # If its not an element, wrap it in a TextNode
  unless child.is_a?(Element)
    child = Arbre::HTML::TextNode.from_string(child)
  end

  if child.respond_to?(:parent)
    # Remove the child
    child.parent.remove_child(child) if child.parent && child.parent != self
    # Set ourselves as the parent
    child.parent = self
  end

  @children << child
end
ancestors() click to toggle source
# File lib/arbre/element.rb, line 81
def ancestors
  if parent?
    [parent] + parent.ancestors
  else
    []
  end
end
assigns() click to toggle source
# File lib/arbre/element.rb, line 20
def assigns
  arbre_context.assigns
end
build(*args, &block) click to toggle source
# File lib/arbre/element.rb, line 32
def build(*args, &block)
  # Render the block passing ourselves in
  append_return_block(block.call(self)) if block
end
children?() click to toggle source
# File lib/arbre/element.rb, line 69
def children?
  @children.any?
end
content() click to toggle source
# File lib/arbre/element.rb, line 119
def content
  children.to_s
end
content=(contents) click to toggle source
# File lib/arbre/element.rb, line 94
def content=(contents)
  clear_children!
  add_child(contents)
end
each(&block) click to toggle source
# File lib/arbre/element.rb, line 131
def each(&block)
  [to_s].each(&block)
end
find_by_class(class_name)
find_by_tag(tag_name)
find_first_ancestor(type) click to toggle source

TODO: Shouldn't grab whole tree

# File lib/arbre/element.rb, line 90
def find_first_ancestor(type)
  ancestors.find{|a| a.is_a?(type) }
end
get_elements_by_class_name(class_name) click to toggle source
# File lib/arbre/element.rb, line 109
def get_elements_by_class_name(class_name)
  elements = ElementCollection.new
  children.each do |child|
    elements << child if child.class_list.include?(class_name)
    elements.concat(child.get_elements_by_class_name(class_name))
  end
  elements
end
Also aliased as: find_by_class
get_elements_by_tag_name(tag_name) click to toggle source
# File lib/arbre/element.rb, line 99
def get_elements_by_tag_name(tag_name)
  elements = ElementCollection.new
  children.each do |child|
    elements << child if child.tag_name == tag_name
    elements.concat(child.get_elements_by_tag_name(tag_name))
  end
  elements
end
Also aliased as: find_by_tag
helpers() click to toggle source
# File lib/arbre/element.rb, line 24
def helpers
  arbre_context.helpers
end
html_safe() click to toggle source
# File lib/arbre/element.rb, line 123
def html_safe
  to_s
end
indent_level() click to toggle source
# File lib/arbre/element.rb, line 127
def indent_level
  parent? ? parent.indent_level + 1 : 0
end
inspect() click to toggle source
# File lib/arbre/element.rb, line 135
def inspect
  to_s
end
parent=(parent) click to toggle source
# File lib/arbre/element.rb, line 73
def parent=(parent)
  @parent = parent
end
parent?() click to toggle source
# File lib/arbre/element.rb, line 77
def parent?
  !@parent.nil?
end
remove_child(child) click to toggle source
# File lib/arbre/element.rb, line 60
def remove_child(child)
  child.parent = nil if child.respond_to?(:parent=)
  @children.delete(child)
end
tag_name() click to toggle source
# File lib/arbre/element.rb, line 28
def tag_name
  @tag_name ||= self.class.name.demodulize.downcase
end
to_a()
Alias for: to_ary
to_ary() click to toggle source
# File lib/arbre/element.rb, line 156
def to_ary
  ElementCollection.new [Proxy.new(self)]
end
Also aliased as: to_a
to_s() click to toggle source
# File lib/arbre/element.rb, line 143
def to_s
  content
end
to_str() click to toggle source
# File lib/arbre/element.rb, line 139
def to_str
  to_s
end

Private Instance Methods

clear_children!() click to toggle source

Resets the Elements children

# File lib/arbre/element.rb, line 164
def clear_children!
  @children.clear
end
method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/arbre/element.rb, line 176
               def method_missing(name, *args, &block)
  if current_arbre_element.respond_to?(name)
    current_arbre_element.send name, *args, &block
  elsif assigns && assigns.has_key?(name)
    assigns[name]
  elsif helpers.respond_to?(name)
    helpers.send(name, *args, &block)
  else
    super
  end
end