Class: Utopia::Content::Transaction

Inherits:
Object
  • 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

Instance Method Summary collapse

Constructor Details

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

extend Gem::Deprecate



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

def initialize(request, response, attributes = {})
  @request = request
  @response = response
  
  @attributes = attributes
  
  @begin_tags = []
  @end_tags = []
end

Instance Attribute Details

#attributesObject (readonly)

Per-transaction global attributes.



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

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.



78
79
80
# File 'lib/utopia/content/transaction.rb', line 78

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.



82
83
84
# File 'lib/utopia/content/transaction.rb', line 82

def end_tags
  @end_tags
end

#requestObject (readonly)

The Rack::Request for this transaction.



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

def request
  @request
end

#responseObject (readonly)

The mutable Rack::Response for this transaction.



70
71
72
# File 'lib/utopia/content/transaction.rb', line 70

def response
  @response
end

Instance Method Details

#cdata(text) ⇒ Object



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

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

#contentObject

The content of the node



192
193
194
# File 'lib/utopia/content/transaction.rb', line 192

def content
  @end_tags.last.content
end

#controllerObject

A helper method for accessing controller variables from view:



54
55
56
# File 'lib/utopia/content/transaction.rb', line 54

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

#currentObject

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



187
188
189
# File 'lib/utopia/content/transaction.rb', line 187

def current
  @begin_tags.last
end

#firstObject



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

def first
  @begin_tags.first
end

#localizationObject



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

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

#lookup(tag) ⇒ Object

Takes an instance of Tag



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/utopia/content/transaction.rb', line 167

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



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

def parent
  @end_tags[-2]
end

#parse_markup(markup) ⇒ Object



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

def parse_markup(markup)
  Processor.parse_markup(markup, self)
end

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



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

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

  return tag_end
end

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

Yields:

  • (node)


84
85
86
87
88
89
90
91
92
# File 'lib/utopia/content/transaction.rb', line 84

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



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

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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/utopia/content/transaction.rb', line 94

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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/utopia/content/transaction.rb', line 132

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.markup(buffer)
    end

    return buffer
  else
    current.tag_end(tag)
  end

  return nil
end