Class: Arbre::Element
Defined Under Namespace
Modules: BuilderMethods
Classes: Proxy
Instance Attribute Summary collapse
Instance Method Summary
collapse
#render
#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.
15
16
17
18
19
|
# File 'lib/arbre/element.rb', line 15
def initialize(arbre_context = Arbre::Context.new)
@arbre_context = arbre_context
@children = ElementCollection.new
@parent = nil
end
|
Instance Attribute Details
#arbre_context ⇒ Object
Returns the value of attribute arbre_context.
13
14
15
|
# File 'lib/arbre/element.rb', line 13
def arbre_context
@arbre_context
end
|
#children ⇒ Object
Returns the value of attribute children.
13
14
15
|
# File 'lib/arbre/element.rb', line 13
def children
@children
end
|
#parent ⇒ Object
Returns the value of attribute parent.
12
13
14
|
# File 'lib/arbre/element.rb', line 12
def parent
@parent
end
|
Instance Method Details
#<<(child) ⇒ Object
66
67
68
|
# File 'lib/arbre/element.rb', line 66
def <<(child)
add_child(child)
end
|
#add_child(child) ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/arbre/element.rb', line 38
def add_child(child)
return unless child
if child.is_a?(Array)
child.each{|item| add_child(item) }
return @children
end
unless child.is_a?(Element)
child = Arbre::HTML::TextNode.from_string(child)
end
if child.respond_to?(:parent)
child.parent.remove_child(child) if child.parent && child.parent != self
child.parent = self
end
@children << child
end
|
#ancestors ⇒ Object
82
83
84
85
86
87
88
|
# File 'lib/arbre/element.rb', line 82
def ancestors
if parent?
[parent] + parent.ancestors
else
[]
end
end
|
#assigns ⇒ Object
21
22
23
|
# File 'lib/arbre/element.rb', line 21
def assigns
arbre_context.assigns
end
|
#build(*args, &block) ⇒ Object
33
34
35
36
|
# File 'lib/arbre/element.rb', line 33
def build(*args, &block)
append_return_block(block.call(self)) if block
end
|
#children? ⇒ Boolean
70
71
72
|
# File 'lib/arbre/element.rb', line 70
def children?
@children.any?
end
|
#content ⇒ Object
120
121
122
|
# File 'lib/arbre/element.rb', line 120
def content
children.to_s
end
|
#content=(contents) ⇒ Object
95
96
97
98
|
# File 'lib/arbre/element.rb', line 95
def content=(contents)
clear_children!
add_child(contents)
end
|
#each(&block) ⇒ Object
132
133
134
|
# File 'lib/arbre/element.rb', line 132
def each(&block)
[to_s].each(&block)
end
|
#find_first_ancestor(type) ⇒ Object
TODO: Shouldn’t grab whole tree
91
92
93
|
# File 'lib/arbre/element.rb', line 91
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
110
111
112
113
114
115
116
117
|
# File 'lib/arbre/element.rb', line 110
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
100
101
102
103
104
105
106
107
|
# File 'lib/arbre/element.rb', line 100
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
|
#helpers ⇒ Object
25
26
27
|
# File 'lib/arbre/element.rb', line 25
def helpers
arbre_context.helpers
end
|
#html_safe ⇒ Object
124
125
126
|
# File 'lib/arbre/element.rb', line 124
def html_safe
to_s
end
|
#indent_level ⇒ Object
128
129
130
|
# File 'lib/arbre/element.rb', line 128
def indent_level
parent? ? parent.indent_level + 1 : 0
end
|
#inspect ⇒ Object
136
137
138
|
# File 'lib/arbre/element.rb', line 136
def inspect
to_s
end
|
#parent? ⇒ Boolean
78
79
80
|
# File 'lib/arbre/element.rb', line 78
def parent?
!@parent.nil?
end
|
#remove_child(child) ⇒ Object
61
62
63
64
|
# File 'lib/arbre/element.rb', line 61
def remove_child(child)
child.parent = nil if child.respond_to?(:parent=)
@children.delete(child)
end
|
#tag_name ⇒ Object
29
30
31
|
# File 'lib/arbre/element.rb', line 29
def tag_name
@tag_name ||= self.class.name.demodulize.downcase
end
|
#to_ary ⇒ Object
Also known as:
to_a
#to_s ⇒ Object
144
145
146
|
# File 'lib/arbre/element.rb', line 144
def to_s
content
end
|
#to_str ⇒ Object
140
141
142
|
# File 'lib/arbre/element.rb', line 140
def to_str
to_s
end
|