class Relaxo::QueryServer::DesignDocument
Represents a design document which includes a variety of functionality for processing documents.
Constants
- VALIDATED
Public Class Methods
new(context, name, attributes = {})
click to toggle source
# File lib/relaxo/query_server/designer.rb, line 127 def initialize(context, name, attributes = {}) @context = context @name = name @attributes = attributes end
Public Instance Methods
[](key)
click to toggle source
Lookup the given key in the design document’s attributes.
# File lib/relaxo/query_server/designer.rb, line 135 def [] key @attributes[key] end
filters(function, documents, request)
click to toggle source
Implements the ‘filters` action.
# 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)
click to toggle source
Looks a up a function given a key path into the design document.
# 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)
click to toggle source
Implements the ‘lists` action.
# 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)
click to toggle source
Runs the given function with the given arguments.
# 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)
click to toggle source
Implements the ‘shows` action.
# 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)
click to toggle source
Implements the ‘updates` action.
# 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)
click to toggle source
Implements the ‘validates_doc_update` action.
# 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
Also aliased as: validate_doc_update
wrap_response(response)
click to toggle source
Ensures that the response is the correct form.
# File lib/relaxo/query_server/designer.rb, line 190 def wrap_response(response) String === response ? {"body" => response} : response end