Class: BOB::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/BOB/element.rb

Constant Summary collapse

@@data =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(selector, options = nil, parent = nil, preBob = nil, contentBob = nil, postBob = nil) ⇒ Element

Returns a new instance of Element.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/BOB/element.rb', line 39

def initialize (selector, options=nil, parent=nil, preBob=nil, contentBob=nil, postBob=nil)
	if selector.include?(" ")
		raise StandardError, "Invalid Element selector. \"" + selector.to_s + "\" contains \" \"(space). Only allowed is \"tag\", \"tag.class\", or \"tag#id\"."
	end

	@parent = parent

	#Flatting so Element.data gets parsed out to the correct thing.
	@options = {}
	if options
		options.each do |key, value|
			@options[key] = Element.toVariable(value)
		end
	end

	@preBob = preBob
	@innerBob = contentBob
	@postBob = postBob

	@type = selector
	@object_class = nil
	@object_id = nil
	@object_content = ""
	@object_style = nil

	if selector.include?(".")
		@type, @object_class = selector.split(".")
	elsif selector.include?("#")
		@type, @object_id = selector.split("#")
	end
end

Instance Attribute Details

#object_contentObject

Returns the value of attribute object_content.



6
7
8
# File 'lib/BOB/element.rb', line 6

def object_content
  @object_content
end

#parentObject

Returns the value of attribute parent.



6
7
8
# File 'lib/BOB/element.rb', line 6

def parent
  @parent
end

Class Method Details

.dObject



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

def self.d
	return @@data
end

.dataObject

TODO: this selects an existing element, and append/prepend/inserts into that



12
13
14
# File 'lib/BOB/element.rb', line 12

def self.data
	return @@data
end

.data=(data) ⇒ Object



15
16
17
# File 'lib/BOB/element.rb', line 15

def self.data=(data)
	@@data = data
end

.find(selector) ⇒ Object



9
10
11
# File 'lib/BOB/element.rb', line 9

def self.find(selector)
	#TODO: this selects an existing element, and append/prepend/inserts into that
end

.get_or_create_bob(data, options, parent) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/BOB/element.rb', line 21

def self.get_or_create_bob(data, options, parent)
	child_bob = nil
	if data.is_a?(Element)
		child_bob = data
	else
		child_bob = Element.new(data, options, parent)
	end
	return child_bob
end

.toVariable(data) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/BOB/element.rb', line 31

def self.toVariable(data)
	if data.respond_to?(:call)
		return data.call
	else
		return data
	end
end

Instance Method Details

#a(data, options = nil) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/BOB/element.rb', line 112

def a (data, options=nil)
	par = self
	par = @parent if @parent
	new_bob = Element.get_or_create_bob(data, options, par)
	if @postBob
		@postBob.a(new_bob)
	else
		@postBob = new_bob
	end
end

#append(data, options = nil) ⇒ Object



109
110
111
# File 'lib/BOB/element.rb', line 109

def append (data, options=nil)
	self.a(data, options)
end

#cl(object_class) ⇒ Object



89
90
91
92
# File 'lib/BOB/element.rb', line 89

def cl (object_class)
	@object_class = Element.toVariable(object_class)
	return self
end

#classs(object_class) ⇒ Object



86
87
88
# File 'lib/BOB/element.rb', line 86

def classs (object_class)
	self.cl(object_class)
end

#co(content) ⇒ Object



74
75
76
77
78
# File 'lib/BOB/element.rb', line 74

def co (content)
	child = self.i("")
	child.object_content = Element.toVariable(content)
	return self
end

#content(content) ⇒ Object



71
72
73
# File 'lib/BOB/element.rb', line 71

def content (content)
	self.co(content)
end

#d(dataset) ⇒ Object



138
139
140
141
142
143
# File 'lib/BOB/element.rb', line 138

def d (dataset)
	child_array = ChildArray.new(dataset, self)
	# @doData = dataset
	# @inDO = true
	return child_array
end

#do(dataset) ⇒ Object



135
136
137
# File 'lib/BOB/element.rb', line 135

def do (dataset)
	self.d(dataset)
end

#i(data, options = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/BOB/element.rb', line 100

def i (data, options=nil)
	child_bob = Element.get_or_create_bob(data, options, self)
	if @innerBob
		@innerBob.a(child_bob)
	else
		@innerBob = child_bob
	end
	return child_bob
end

#id(object_id) ⇒ Object



93
94
95
96
# File 'lib/BOB/element.rb', line 93

def id (object_id)
	@object_id = Element.toVariable(object_id)
	return self
end

#insert(data, options = nil) ⇒ Object



97
98
99
# File 'lib/BOB/element.rb', line 97

def insert (data, options=nil)
	self.i(data,options)
end

#p(data, options = nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/BOB/element.rb', line 125

def p (data, options=nil)
	par = self
	par = @parent if @parent
	new_bob = Element.get_or_create_bob(data, options, par)
	if @preBob
		@preBob.p(new_bob)
	else
		@preBob = new_bob
	end
end

#ppObject



153
154
155
# File 'lib/BOB/element.rb', line 153

def pp
	self.s(true)
end

#prepend(data, options = nil) ⇒ Object



122
123
124
# File 'lib/BOB/element.rb', line 122

def prepend (data, options=nil)
	self.p(data, options)
end

#prettyPrintObject



150
151
152
# File 'lib/BOB/element.rb', line 150

def prettyPrint
	self.pp()
end

#s(pretty = false) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/BOB/element.rb', line 159

def s (pretty = false)
	#this makes the toString bubble to the top if it is cast on a sub-element
	if @parent
		return @parent.s(pretty)
	end
	
	#kill parents so they will print out.
	@innerBob.parent = nil if @innerBob
	@preBob.parent     = nil if @preBob
	@postBob.parent    = nil if @postBob

	prepend = ''
	append = ''
	printself = ''
	content_b = ''

	content_b = @innerBob.s(pretty) if @innerBob
	prepend = @preBob.s(pretty) if @preBob
	append = @postBob.s(pretty) if @postBob
	#TODO: Make special case for img (or those without content?) and no-type tag, which is pure text content.
	
	if @type != ""
		printself += '<' + @type + ' '
		@options.each do |key, value|
			unless key == 'style' && @object_style || key == 'id' && @object_id || key == 'class' && @object_class
				printself << key.to_s() + '="' + value.to_s() + '" '
			end
		end
		
		printself << 'class="' + @object_class.to_s() + '" ' if @object_class
		printself << 'id="'    + @object_id.to_s()    + '" ' if @object_id
		printself << 'style="' + @object_style.to_s() + '" ' if @object_style

		printself = printself[0..-2]
		closable = (["area",
					"base",
					"br",
					"col",
					"embed",
					"hr",
					"img",
					"input",
					"keygen",
					"link",
					"menuitem",
					"meta",
					"param",
					"source",
					"track",
					"wbr",
					"basefont",
					"bgsound",
					"frame",
					"isindex"].include?(@type))

		if closable && content_b == ''
			printself << ' />'
		else	
			if pretty
				if content_b && content_b != ""
					content_b = "\n\t" +  content_b.gsub("\n", "\n\t").to_s() + "\n"
				else
					content_b = "\n"
				end
			end
			printself.to_s() << '>' + content_b.to_s() + '</' + @type.to_s() + '>'
		end
	else
		#pure text element (no type)
		printself = @object_content #it should not have any innerBob as it is never exposed when we are setting object_content
	end
	if pretty
		prepend =  prepend + "\n\t" if prepend && prepend != ""
		printself = printself.gsub("\n", "\n\t") if prepend && prepend != ""
		append = "\n" + append if append && append != ""
	end
			
	
	return prepend.to_s() + printself.to_s() + append.to_s()
end

#st(style) ⇒ Object



82
83
84
85
# File 'lib/BOB/element.rb', line 82

def st (style)
	@object_style = Element.toVariable(style)
	return self
end

#style(style) ⇒ Object



79
80
81
# File 'lib/BOB/element.rb', line 79

def style (style)
	self.st(style)
end

#toStringObject



156
157
158
# File 'lib/BOB/element.rb', line 156

def toString
	self.s()
end

#uObject



147
148
149
# File 'lib/BOB/element.rb', line 147

def u
	@parent
end

#upObject



144
145
146
# File 'lib/BOB/element.rb', line 144

def up
	@parent
end