Class: Utopia::Content::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/content/transaction.rb

Overview

A single request through content middleware.

Defined Under Namespace

Classes: State

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, response) ⇒ Transaction

Returns a new instance of Transaction.



121
122
123
124
125
126
127
# File 'lib/utopia/content/transaction.rb', line 121

def initialize(request, response)
	@begin_tags = []
	@end_tags = []

	@request = request
	@response = response
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



287
288
289
290
291
292
293
294
295
# File 'lib/utopia/content/transaction.rb', line 287

def method_missing(name, *args)
	@begin_tags.reverse_each do |state|
		if state.node.respond_to? name
			return state.node.send(name, *args)
		end
	end

	super
end

Instance Attribute Details

#begin_tagsObject (readonly)

Begin tags represents a list from outer to inner most tag. At any point in parsing xml, begin_tags is a list of the inner most tag, then the next outer tag, etc. This list is used for doing dependent lookups.



144
145
146
# File 'lib/utopia/content/transaction.rb', line 144

def begin_tags
  @begin_tags
end

#end_tagsObject (readonly)

End tags represents a list of execution order. This is the order that end tags have appeared when evaluating nodes.



148
149
150
# File 'lib/utopia/content/transaction.rb', line 148

def end_tags
  @end_tags
end

#requestObject (readonly)

Returns the value of attribute request.



129
130
131
# File 'lib/utopia/content/transaction.rb', line 129

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



130
131
132
# File 'lib/utopia/content/transaction.rb', line 130

def response
  @response
end

Instance Method Details

#attributesObject



150
151
152
# File 'lib/utopia/content/transaction.rb', line 150

def attributes
	return current.attributes
end

#cdata(text) ⇒ Object



218
219
220
# File 'lib/utopia/content/transaction.rb', line 218

def cdata(text)
	current.cdata(text)
end

#contentObject



162
163
164
# File 'lib/utopia/content/transaction.rb', line 162

def content
	@end_tags[-1].content
end

#controllerObject

A helper method for accessing controller variables from view:



133
134
135
# File 'lib/utopia/content/transaction.rb', line 133

def controller
	@request.env[VARIABLES_KEY]
end

#currentObject



158
159
160
# File 'lib/utopia/content/transaction.rb', line 158

def current
	@begin_tags[-1]
end

#firstObject



170
171
172
# File 'lib/utopia/content/transaction.rb', line 170

def first
	@begin_tags[0]
end

#localizationObject



154
155
156
# File 'lib/utopia/content/transaction.rb', line 154

def localization
	@localization ||= Utopia::Localization[request]
end

#lookup(tag) ⇒ Object

Takes an instance of Tag



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/utopia/content/transaction.rb', line 268

def lookup(tag)
	result = tag
	node = nil

	@begin_tags.reverse_each do |state|
		result = state.lookup(result)
		
		node ||= state.node if state.node.respond_to? :lookup

		return result if Node === result
	end

	@end_tags.reverse_each do |state|
		return state.node.lookup(result) if state.node.respond_to? :lookup
	end

	return nil
end

#parentObject



166
167
168
# File 'lib/utopia/content/transaction.rb', line 166

def parent
	end_tags[-2]
end

#parse_xml(xml_data) ⇒ Object



137
138
139
# File 'lib/utopia/content/transaction.rb', line 137

def parse_xml(xml_data)
	Processor.parse_xml(xml_data, self)
end

#partial(*args, &block) ⇒ Object Also known as: deferred_tag



222
223
224
225
226
227
228
229
230
# File 'lib/utopia/content/transaction.rb', line 222

def partial(*args, &block)
	if block_given?
		current.defer(&block)
	else
		current.defer do
			tag(*args)
		end
	end
end

#render_node(node, attributes = {}) ⇒ Object



260
261
262
263
264
265
# File 'lib/utopia/content/transaction.rb', line 260

def render_node(node, attributes = {})
	state = State.new(attributes, node)
	@begin_tags << state

	return tag_end
end

#tag(name, attributes = {}) {|node| ... } ⇒ Object

Yields:

  • (node)


174
175
176
177
178
179
180
181
182
# File 'lib/utopia/content/transaction.rb', line 174

def tag(name, attributes = {}, &block)
	tag = Tag.new(name, attributes)

	node = tag_begin(tag)

	yield node if block_given?

	tag_end(tag)
end

#tag_begin(tag, node = nil) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/utopia/content/transaction.rb', line 199

def tag_begin(tag, node = nil)
	node ||= lookup(tag)

	if node
		state = State.new(tag, node)
		@begin_tags << state

		if node.respond_to? :tag_begin
			node.tag_begin(self, state)
		end

		return node
	end

	current.tag_begin(tag)

	return nil
end

#tag_complete(tag, node = nil) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/utopia/content/transaction.rb', line 184

def tag_complete(tag, node = nil)
	if tag.name == "content"
		current.markup(content)
	else
		node ||= lookup(tag)

		if node
			tag_begin(tag, node)
			tag_end(tag)
		else
			current.tag_complete(tag)
		end
	end
end

#tag_end(tag = nil) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/utopia/content/transaction.rb', line 234

def tag_end(tag = nil)
	top = current

	if top.tags.empty?
		if top.node.respond_to? :tag_end
			top.node.tag_end(self, top)
		end

		@end_tags << top
		buffer = top.call(self)

		@begin_tags.pop
		@end_tags.pop

		if current
			current.markup(buffer)
		end

		return buffer
	else
		current.tag_end(tag)
	end

	return nil
end