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.
14
15
16
17
18
|
# File 'lib/arbre/element.rb', line 14
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.
12
13
14
|
# File 'lib/arbre/element.rb', line 12
def arbre_context
@arbre_context
end
|
#children ⇒ Object
Returns the value of attribute children.
12
13
14
|
# File 'lib/arbre/element.rb', line 12
def children
@children
end
|
#parent ⇒ Object
Returns the value of attribute parent.
11
12
13
|
# File 'lib/arbre/element.rb', line 11
def parent
@parent
end
|
Instance Method Details
#<<(child) ⇒ Object
65
66
67
|
# File 'lib/arbre/element.rb', line 65
def <<(child)
add_child(child)
end
|
#add_child(child) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# 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
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
81
82
83
84
85
86
87
|
# File 'lib/arbre/element.rb', line 81
def ancestors
if parent?
[parent] + parent.ancestors
else
[]
end
end
|
#assigns ⇒ Object
20
21
22
|
# File 'lib/arbre/element.rb', line 20
def assigns
arbre_context.assigns
end
|
#build(*args, &block) ⇒ Object
32
33
34
35
|
# File 'lib/arbre/element.rb', line 32
def build(*args, &block)
append_return_block(block.call(self)) if block
end
|
#children? ⇒ Boolean
69
70
71
|
# File 'lib/arbre/element.rb', line 69
def children?
@children.any?
end
|
#content ⇒ Object
119
120
121
|
# File 'lib/arbre/element.rb', line 119
def content
children.to_s
end
|
#content=(contents) ⇒ Object
94
95
96
97
|
# File 'lib/arbre/element.rb', line 94
def content=(contents)
clear_children!
add_child(contents)
end
|
#each(&block) ⇒ Object
131
132
133
|
# File 'lib/arbre/element.rb', line 131
def each(&block)
[to_s].each(&block)
end
|
#find_first_ancestor(type) ⇒ Object
TODO: Shouldn’t grab whole tree
90
91
92
|
# 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) ⇒ Object
Also known as:
find_by_class
109
110
111
112
113
114
115
116
|
# 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
|
#get_elements_by_tag_name(tag_name) ⇒ Object
Also known as:
find_by_tag
99
100
101
102
103
104
105
106
|
# 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
|
#helpers ⇒ Object
24
25
26
|
# File 'lib/arbre/element.rb', line 24
def helpers
arbre_context.helpers
end
|
#html_safe ⇒ Object
123
124
125
|
# File 'lib/arbre/element.rb', line 123
def html_safe
to_s
end
|
#indent_level ⇒ Object
127
128
129
|
# File 'lib/arbre/element.rb', line 127
def indent_level
parent? ? parent.indent_level + 1 : 0
end
|
#inspect ⇒ Object
135
136
137
|
# File 'lib/arbre/element.rb', line 135
def inspect
to_s
end
|
#parent? ⇒ Boolean
77
78
79
|
# File 'lib/arbre/element.rb', line 77
def parent?
!@parent.nil?
end
|
#remove_child(child) ⇒ Object
60
61
62
63
|
# 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 ⇒ Object
28
29
30
|
# File 'lib/arbre/element.rb', line 28
def tag_name
@tag_name ||= self.class.name.demodulize.downcase
end
|
#to_ary ⇒ Object
Also known as:
to_a
#to_s ⇒ Object
143
144
145
|
# File 'lib/arbre/element.rb', line 143
def to_s
content
end
|
#to_str ⇒ Object
139
140
141
|
# File 'lib/arbre/element.rb', line 139
def to_str
to_s
end
|