Class: Arbo::Element
- Inherits:
-
Object
show all
- Includes:
- BuilderMethods
- Defined in:
- lib/arbo/element.rb,
lib/arbo/element/proxy.rb,
lib/arbo/element/builder_methods.rb
Defined Under Namespace
Modules: BuilderMethods
Classes: Proxy
Instance Attribute Summary collapse
Instance Method Summary
collapse
#build_tag, #current_arbo_element, included, #insert_tag, #with_current_arbo_element
Constructor Details
#initialize(arbo_context = Arbo::Context.new) ⇒ Element
Returns a new instance of Element.
14
15
16
17
18
|
# File 'lib/arbo/element.rb', line 14
def initialize(arbo_context = Arbo::Context.new)
@arbo_context = arbo_context
@children = ElementCollection.new
@parent = nil
end
|
Instance Attribute Details
#arbo_context ⇒ Object
Returns the value of attribute arbo_context.
12
13
14
|
# File 'lib/arbo/element.rb', line 12
def arbo_context
@arbo_context
end
|
#children ⇒ Object
Returns the value of attribute children.
12
13
14
|
# File 'lib/arbo/element.rb', line 12
def children
@children
end
|
#parent ⇒ Object
Returns the value of attribute parent.
11
12
13
|
# File 'lib/arbo/element.rb', line 11
def parent
@parent
end
|
Instance Method Details
#<<(child) ⇒ Object
65
66
67
|
# File 'lib/arbo/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/arbo/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 = Arbo::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/arbo/element.rb', line 81
def ancestors
if parent?
[parent] + parent.ancestors
else
[]
end
end
|
#assigns ⇒ Object
20
21
22
|
# File 'lib/arbo/element.rb', line 20
def assigns
arbo_context.assigns
end
|
#build(*args, &block) ⇒ Object
32
33
34
35
|
# File 'lib/arbo/element.rb', line 32
def build(*args, &block)
append_return_block(block.call(self)) if block
end
|
#children? ⇒ Boolean
69
70
71
|
# File 'lib/arbo/element.rb', line 69
def children?
@children.any?
end
|
#content ⇒ Object
119
120
121
|
# File 'lib/arbo/element.rb', line 119
def content
children.to_s
end
|
#content=(contents) ⇒ Object
94
95
96
97
|
# File 'lib/arbo/element.rb', line 94
def content=(contents)
clear_children!
add_child(contents)
end
|
#each(&block) ⇒ Object
131
132
133
|
# File 'lib/arbo/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/arbo/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/arbo/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/arbo/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/arbo/element.rb', line 24
def helpers
arbo_context.helpers
end
|
#html_safe ⇒ Object
123
124
125
|
# File 'lib/arbo/element.rb', line 123
def html_safe
to_s
end
|
#indent_level ⇒ Object
127
128
129
|
# File 'lib/arbo/element.rb', line 127
def indent_level
parent? ? parent.indent_level + 1 : 0
end
|
#inspect ⇒ Object
135
136
137
|
# File 'lib/arbo/element.rb', line 135
def inspect
content
end
|
#parent? ⇒ Boolean
77
78
79
|
# File 'lib/arbo/element.rb', line 77
def parent?
!@parent.nil?
end
|
#remove_child(child) ⇒ Object
60
61
62
63
|
# File 'lib/arbo/element.rb', line 60
def remove_child(child)
child.parent = nil if child.respond_to?(:parent=)
@children.delete(child)
end
|
#render_in(context = arbo_context) ⇒ Object
Rendering strategy that visits all elements and appends output to a buffer.
150
151
152
153
154
|
# File 'lib/arbo/element.rb', line 150
def render_in(context = arbo_context)
children.collect do |element|
element.render_in_or_to_s(context)
end.join('')
end
|
#render_in_or_to_s(context) ⇒ Object
Use render_in to render element unless closer ancestor overrides :to_s only.
157
158
159
160
161
162
163
164
|
# File 'lib/arbo/element.rb', line 157
def render_in_or_to_s(context)
if method_distance(:render_in) <= method_distance(:to_s)
render_in(context)
else
ActiveSupport::Deprecation.warn("#render_in should be defined for rendering #{method_owner(:to_s)} instead of #to_s")
to_s.tap { |s| context.output_buffer << s }
end
end
|
#tag_name ⇒ Object
28
29
30
|
# File 'lib/arbo/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
144
145
146
147
|
# File 'lib/arbo/element.rb', line 144
def to_s
ActiveSupport::Deprecation.warn("#render_in should be defined for rendering #{method_owner(:to_s)} instead of #to_s")
content
end
|
#to_str ⇒ Object
139
140
141
142
|
# File 'lib/arbo/element.rb', line 139
def to_str
ActiveSupport::Deprecation.warn("don't rely on implicit conversion of Element to String")
content
end
|