Class: Relaxo::QueryServer::DesignDocument
- Inherits:
-
Object
- Object
- Relaxo::QueryServer::DesignDocument
- Defined in:
- lib/relaxo/query_server/designer.rb
Overview
Represents a design document which includes a variety of functionality for processing documents.
Constant Summary collapse
- VALIDATED =
1
Instance Method Summary collapse
-
#[](key) ⇒ Object
Lookup the given key in the design document’s attributes.
-
#filters(function, documents, request) ⇒ Object
Implements the ‘filters` action.
-
#function_for(path) ⇒ Object
Looks a up a function given a key path into the design document.
-
#initialize(context, name, attributes = {}) ⇒ DesignDocument
constructor
A new instance of DesignDocument.
-
#lists(function, head, request) ⇒ Object
Implements the ‘lists` action.
-
#run(function, arguments) ⇒ Object
Runs the given function with the given arguments.
-
#shows(function, document, request) ⇒ Object
Implements the ‘shows` action.
-
#updates(function, document, request) ⇒ Object
Implements the ‘updates` action.
-
#validates(function, new_document, old_document, user_context) ⇒ Object
(also: #validate_doc_update)
Implements the ‘validates_doc_update` action.
-
#wrap_response(response) ⇒ Object
Ensures that the response is the correct form.
Constructor Details
#initialize(context, name, attributes = {}) ⇒ DesignDocument
Returns a new instance of DesignDocument.
127 128 129 130 131 132 |
# File 'lib/relaxo/query_server/designer.rb', line 127 def initialize(context, name, attributes = {}) @context = context @name = name @attributes = attributes end |
Instance Method Details
#[](key) ⇒ Object
Lookup the given key in the design document’s attributes.
135 136 137 |
# File 'lib/relaxo/query_server/designer.rb', line 135 def [] key @attributes[key] end |
#filters(function, documents, request) ⇒ Object
Implements the ‘filters` action.
148 149 150 151 152 |
# File 'lib/relaxo/query_server/designer.rb', line 148 def filters(function, documents, request) results = documents.map{|document| !!function.call(document, request)} return [true, results] end |
#function_for(path) ⇒ Object
Looks a up a function given a key path into the design document.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/relaxo/query_server/designer.rb', line 195 def function_for(path) parent = @attributes function = path.inject(parent) do |current, key| parent = current throw ArgumentError.new("Invalid function name #{path.join(".")}") unless current current[key] end # Compile the function if required: if String === function parent[path.last] = @context.parse_function(function, binding, 'design-document') else function end end |
#lists(function, head, request) ⇒ Object
Implements the ‘lists` action.
173 174 175 |
# File 'lib/relaxo/query_server/designer.rb', line 173 def lists(function, head, request) ListRenderer.new(@context, function).run(head, request) end |
#run(function, arguments) ⇒ Object
Runs the given function with the given arguments.
140 141 142 143 144 145 |
# File 'lib/relaxo/query_server/designer.rb', line 140 def run(function, arguments) action = function[0] function = function_for(function) self.send(action, function, *arguments) end |
#shows(function, document, request) ⇒ Object
Implements the ‘shows` action.
155 156 157 158 159 |
# File 'lib/relaxo/query_server/designer.rb', line 155 def shows(function, document, request) response = function.call(document, request) return ["resp", wrap_response(response)] end |
#updates(function, document, request) ⇒ Object
Implements the ‘updates` action.
162 163 164 165 166 167 168 169 170 |
# File 'lib/relaxo/query_server/designer.rb', line 162 def updates(function, document, request) raise InvalidRequestError.new("Unsupported method #{request['method']}") unless request['method'] == 'POST' document, response = function.call(document, request) return ["up", document, wrap_response(response)] rescue InvalidRequestError => error return ["up", null, error.to_response] end |
#validates(function, new_document, old_document, user_context) ⇒ Object Also known as: validate_doc_update
Implements the ‘validates_doc_update` action.
178 179 180 181 182 183 184 185 |
# File 'lib/relaxo/query_server/designer.rb', line 178 def validates(function, new_document, old_document, user_context) Process.new(@context, function).run(new_document, old_document, user_context) # Unless ValidationError was raised, we are okay. return VALIDATED rescue ValidationError => error error.details end |
#wrap_response(response) ⇒ Object
Ensures that the response is the correct form.
190 191 192 |
# File 'lib/relaxo/query_server/designer.rb', line 190 def wrap_response(response) String === response ? {"body" => response} : response end |