Class: Arbre::Element

Inherits:
Object
  • Object
show all
Includes:
BuilderMethods
Defined in:
lib/arbre/element.rb,
lib/arbre/element/proxy.rb,
lib/arbre/element/builder_methods.rb

Direct Known Subclasses

Context, HTML::Tag, HTML::TextNode

Defined Under Namespace

Modules: BuilderMethods Classes: Proxy

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BuilderMethods

#build_tag, #current_arbre_element, included, #insert_tag, #with_current_arbre_element

Constructor Details

#initialize(arbre_context = Arbre::Context.new) ⇒ Element

Returns a new instance of Element.


13
14
15
16
# File 'lib/arbre/element.rb', line 13

def initialize(arbre_context = Arbre::Context.new)
  @arbre_context = arbre_context
  @children = ElementCollection.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)

Implements the method lookup chain. When you call a method that doesn't exist, we:

1. Try to call the method on the current DOM context
2. Return an assigned variable of the same name
3. Call the method on the helper object
4. Call super

174
175
176
177
178
179
180
181
182
183
184
# File 'lib/arbre/element.rb', line 174

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

Instance Attribute Details

#arbre_contextObject (readonly)

Returns the value of attribute arbre_context


11
12
13
# File 'lib/arbre/element.rb', line 11

def arbre_context
  @arbre_context
end

#childrenObject (readonly)

Returns the value of attribute children


11
12
13
# File 'lib/arbre/element.rb', line 11

def children
  @children
end

#parentObject

Returns the value of attribute parent


10
11
12
# File 'lib/arbre/element.rb', line 10

def parent
  @parent
end

Instance Method Details

#+(element) ⇒ Object


145
146
147
148
149
150
151
152
# File 'lib/arbre/element.rb', line 145

def +(element)
  case element
  when Element, ElementCollection
  else
    element = Arbre::HTML::TextNode.from_string(element)
  end
  to_ary + element
end

#<<(child) ⇒ Object


63
64
65
# File 'lib/arbre/element.rb', line 63

def <<(child)
  add_child(child)
end

#add_child(child) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/arbre/element.rb', line 35

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

#ancestorsObject


79
80
81
82
83
84
85
# File 'lib/arbre/element.rb', line 79

def ancestors
  if parent?
    [parent] + parent.ancestors
  else
    []
  end
end

#assignsObject


18
19
20
# File 'lib/arbre/element.rb', line 18

def assigns
  arbre_context.assigns
end

#build(*args, &block) ⇒ Object


30
31
32
33
# File 'lib/arbre/element.rb', line 30

def build(*args, &block)
  # Render the block passing ourselves in
  append_return_block(block.call(self)) if block
end

#children?Boolean

Returns:

  • (Boolean)

67
68
69
# File 'lib/arbre/element.rb', line 67

def children?
  @children.any?
end

#contentObject


117
118
119
# File 'lib/arbre/element.rb', line 117

def content
  children.to_s
end

#content=(contents) ⇒ Object


92
93
94
95
# File 'lib/arbre/element.rb', line 92

def content=(contents)
  clear_children!
  add_child(contents)
end

#each(&block) ⇒ Object


129
130
131
# File 'lib/arbre/element.rb', line 129

def each(&block)
  [to_s].each(&block)
end

#find_first_ancestor(type) ⇒ Object

TODO: Shouldn't grab whole tree


88
89
90
# File 'lib/arbre/element.rb', line 88

def find_first_ancestor(type)
  ancestors.find{|a| a.is_a?(type) }
end

#get_elements_by_class_name(class_name) ⇒ Object Also known as: find_by_class


107
108
109
110
111
112
113
114
# File 'lib/arbre/element.rb', line 107

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

#get_elements_by_tag_name(tag_name) ⇒ Object Also known as: find_by_tag


97
98
99
100
101
102
103
104
# File 'lib/arbre/element.rb', line 97

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

#helpersObject


22
23
24
# File 'lib/arbre/element.rb', line 22

def helpers
  arbre_context.helpers
end

#html_safeObject


121
122
123
# File 'lib/arbre/element.rb', line 121

def html_safe
  to_s
end

#indent_levelObject


125
126
127
# File 'lib/arbre/element.rb', line 125

def indent_level
  parent? ? parent.indent_level + 1 : 0
end

#inspectObject


133
134
135
# File 'lib/arbre/element.rb', line 133

def inspect
  to_s
end

#parent?Boolean

Returns:

  • (Boolean)

75
76
77
# File 'lib/arbre/element.rb', line 75

def parent?
  !@parent.nil?
end

#remove_child(child) ⇒ Object


58
59
60
61
# File 'lib/arbre/element.rb', line 58

def remove_child(child)
  child.parent = nil if child.respond_to?(:parent=)
  @children.delete(child)
end

#tag_nameObject


26
27
28
# File 'lib/arbre/element.rb', line 26

def tag_name
  @tag_name ||= self.class.name.demodulize.downcase
end

#to_aryObject Also known as: to_a


154
155
156
# File 'lib/arbre/element.rb', line 154

def to_ary
  ElementCollection.new [Proxy.new(self)]
end

#to_sObject


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

def to_s
  content
end

#to_strObject


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

def to_str
  to_s
end