Class: Utopia::Content::Transaction

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

Overview

A single request through content middleware. We use a struct to hide instance varibles since we instance_exec within this context.

Defined Under Namespace

Classes: State

Instance Attribute Summary collapse

Attributes inherited from Response

#headers, #status

Instance Method Summary collapse

Methods inherited from Response

#cache!, #content_type=, #do_not_cache!

Constructor Details

#initialize(request, attributes = {}) ⇒ Transaction

Returns a new instance of Transaction.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/utopia/content/transaction.rb', line 43

def initialize(request, attributes = {})
	@request = request
	
	@attributes = attributes
	
	@begin_tags = []
	@end_tags = []
	
	# TODO: Provide way to set encoding and content type.
	#@encoding = Encoding::UTF_8
	#self.content_type = "text/html; charset=#{@encoding.name}"
	
	super()
end

Instance Attribute Details

#attributesObject (readonly)

Per-transaction global attributes.



75
76
77
# File 'lib/utopia/content/transaction.rb', line 75

def attributes
  @attributes
end

#begin_tagsObject (readonly)

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



80
81
82
# File 'lib/utopia/content/transaction.rb', line 80

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.



84
85
86
# File 'lib/utopia/content/transaction.rb', line 84

def end_tags
  @end_tags
end

#requestObject (readonly)

The Rack::Request for this transaction.



72
73
74
# File 'lib/utopia/content/transaction.rb', line 72

def request
  @request
end

Instance Method Details

#contentObject

The content of the node



200
201
202
# File 'lib/utopia/content/transaction.rb', line 200

def content
	@end_tags.last.content
end

#controllerObject

A helper method for accessing controller variables from view:



59
60
61
# File 'lib/utopia/content/transaction.rb', line 59

def controller
	@controller ||= Utopia::Controller[request]
end

#currentObject

The current tag being processed/rendered. Prefer to access state directly.



195
196
197
# File 'lib/utopia/content/transaction.rb', line 195

def current
	@begin_tags.last
end

#firstObject



208
209
210
# File 'lib/utopia/content/transaction.rb', line 208

def first
	@begin_tags.first
end

#localizationObject



63
64
65
# File 'lib/utopia/content/transaction.rb', line 63

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

#lookup(tag) ⇒ Object

Takes an instance of Tag



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/utopia/content/transaction.rb', line 175

def lookup(tag)
	result = tag
	node = nil

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

		return result if result.is_a?(Node)
	end

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

	return nil
end

#parentObject



204
205
206
# File 'lib/utopia/content/transaction.rb', line 204

def parent
	@end_tags[-2]
end

#parse_markup(markup) ⇒ Object



67
68
69
# File 'lib/utopia/content/transaction.rb', line 67

def parse_markup(markup)
	MarkupParser.parse(markup, self)
end

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



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

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

	return tag_end
end

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

Yields:

  • (node)


86
87
88
89
90
91
92
93
94
95
# File 'lib/utopia/content/transaction.rb', line 86

def tag(name, attributes = {}, &block)
	# If we provide a block which can give inner data, we are not self-closing.
	tag = Tag.new(name, !block_given?, attributes)

	node = tag_begin(tag)

	yield node if block_given?

	tag_end(tag)
end

#tag_begin(tag, node = nil) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/utopia/content/transaction.rb', line 112

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

	if node
		state = State.new(tag, node)
		self.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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/utopia/content/transaction.rb', line 97

def tag_complete(tag, node = nil)
	if tag.name == CONTENT_TAG_NAME
		current.write(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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/utopia/content/transaction.rb', line 141

def tag_end(tag = nil)
	# Get the current tag which we are completing/ending:
	top = current
	
	if top.tags.empty?
		if top.node.respond_to? :tag_end
			top.node.tag_end(self, top)
		end

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

		self.begin_tags.pop
		self.end_tags.pop

		if current
			current.write(buffer)
		end

		return buffer
	else
		current.tag_end(tag)
	end

	return nil
end

#text(string) ⇒ Object



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

def text(string)
	current.text(string)
end

#write(string) ⇒ Object Also known as: cdata



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

def write(string)
	current.write(string)
end