Class: ObjectView::Element
- Inherits:
-
Hash
- Object
- Hash
- ObjectView::Element
show all
- Defined in:
- lib/object_view/element.rb
Direct Known Subclasses
Body, Button, Combobox, Div, FieldSet, Form, Head, Header, IFrame, Image, Input, Javascript, JavascriptFile, Label, Li, Link, Nav, Option, Page, Paragraph, Span, Stylesheet, TBody, TCell, THead, TRow, Table, TextArea, Title, Ul
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Element.
9
10
11
12
13
14
15
|
# File 'lib/object_view/element.rb', line 9
def initialize
@tab = " "
@single_line = false
@tag = nil
@children = []
@acceptable_children = [String, Element]
end
|
Instance Attribute Details
#acceptable_children ⇒ Object
Returns the value of attribute acceptable_children.
7
8
9
|
# File 'lib/object_view/element.rb', line 7
def acceptable_children
@acceptable_children
end
|
#children ⇒ Object
Returns the value of attribute children.
7
8
9
|
# File 'lib/object_view/element.rb', line 7
def children
@children
end
|
#single_line ⇒ Object
Returns the value of attribute single_line.
6
7
8
|
# File 'lib/object_view/element.rb', line 6
def single_line
@single_line
end
|
#tag ⇒ Object
Returns the value of attribute tag.
6
7
8
|
# File 'lib/object_view/element.rb', line 6
def tag
@tag
end
|
Instance Method Details
#<<(item) ⇒ Object
101
102
103
|
# File 'lib/object_view/element.rb', line 101
def <<(item)
self.add item
end
|
#add(element_or_string) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/object_view/element.rb', line 75
def add(element_or_string)
if (element_or_string)
if (self.is_acceptable_child?(element_or_string))
@children << element_or_string
elsif (element_or_string.is_a?(Array))
element_or_string.each do |child|
add child
end
else
raise IllegalChildElementError.new "Parent: #{self.tag} - Attempt to add an element that is not a valid child, and not an ObjectView::Element. Class of element: #{element_or_string.class.name}: #{element_or_string}\nTag of parent: #{self.tag}.\nAcceptable children: #{@acceptable_children.join(',')}"
end
end
return element_or_string
end
|
#add_with_tag(tag, content = nil) ⇒ Object
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/object_view/element.rb', line 90
def add_with_tag(tag, content = nil)
element = Element.new
element.tag = tag
if (content != nil)
element.children << content
end
element.single_line = false
self.children << element
return element
end
|
#attr(name, value) ⇒ Object
42
43
44
|
# File 'lib/object_view/element.rb', line 42
def attr(name, value)
self[name] = value
end
|
#attributes ⇒ Object
17
18
19
|
# File 'lib/object_view/element.rb', line 17
def attributes
return self
end
|
#css_class=(value) ⇒ Object
58
59
60
|
# File 'lib/object_view/element.rb', line 58
def css_class=(value)
self.attr("class", value)
end
|
#find_element_with_tag(tag) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/object_view/element.rb', line 26
def find_element_with_tag(tag)
found = []
@children.each do |child|
if (child.is_a?(Element)) && (child.tag == tag)
found << child
end
end
if (found.length == 0)
return nil
elsif (found.length == 1)
return found[0]
else
return found
end
end
|
#id=(value) ⇒ Object
62
63
64
|
# File 'lib/object_view/element.rb', line 62
def id=(value)
self.attr("id", value)
end
|
#is_acceptable_child?(child) ⇒ Boolean
66
67
68
69
70
71
72
73
|
# File 'lib/object_view/element.rb', line 66
def is_acceptable_child?(child)
@acceptable_children.each do |good_class|
if (child.is_a?(good_class))
return true
end
end
return false
end
|
#on_click=(value) ⇒ Object
50
51
52
|
# File 'lib/object_view/element.rb', line 50
def on_click=(value)
self.attr("onclick", value)
end
|
#render(indent = 0) ⇒ Object
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/object_view/element.rb', line 145
def render(indent = 0)
raise "tag not defined for class: #{self.class}" if (tag == nil) || (tag.strip.length == 0)
html = StringIO.new
if (indent != nil)
html << (@tab * indent)
end
if (children.length > 0)
html << "<#{tag}#{render_attributes}>"
if (self.single_line)
html << "#{render_children(nil)}"
else
if (indent == nil)
html << render_children(indent)
else
html << render_children(indent + 1)
end
end
if (!self.single_line)
html << "\n"
if (indent != nil)
html << @tab * indent
end
end
html << "</#{tag}>\n"
else
html << "<#{tag}#{render_attributes}/>"
end
return html.string
end
|
#render_attributes ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/object_view/element.rb', line 124
def render_attributes
html = StringIO.new
if (self.keys.length > 0)
self.each do |name, value|
if (value.class == Hash)
attr_value = ''
value.keys.each do |key|
attr_value += "#{key.to_s.gsub('_', '-')}: #{value[key]}"
if (key != value.keys.last)
attr_value += "; "
end
end
html << " #{name}=\"#{attr_value}\""
else
html << " #{name}=\"#{value}\""
end
end
end
return html.string
end
|
#render_children(indent = 0) ⇒ Object
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/object_view/element.rb', line 105
def render_children(indent = 0)
html = StringIO.new
@children.each do |child|
if (indent != nil)
if (!self.single_line)
html << "\n"
end
html << @tab * indent
end
if (child.class.name == "String")
html << child
else
child_html = child.render(indent)
html << child_html
end
end
return html.string
end
|
#style ⇒ Object
21
22
23
24
|
# File 'lib/object_view/element.rb', line 21
def style
self[:style] = {} if (!self[:style])
return self[:style]
end
|
#style=(the_style) ⇒ Object
54
55
56
|
# File 'lib/object_view/element.rb', line 54
def style=(the_style)
self.attr("style", the_style)
end
|