class Relaxo::QueryServer::Context

A query server context includes all state required for implementing the query server protocol.

Attributes

config[R]
shell[R]

Public Class Methods

new(shell, options = {}) click to toggle source
# File lib/relaxo/query_server/context.rb, line 46
def initialize(shell, options = {})
        @shell = shell
        @options = options
        
        @mapper = Mapper.new(self)
        @reducer = Reducer.new(self)
        @designer = Designer.new(self)
        
        @config = {}
end

Public Instance Methods

error_for_exception(exception) click to toggle source

Return an error structure from the given exception.

# File lib/relaxo/query_server/context.rb, line 61
def error_for_exception(exception)
        ["error", exception.class.to_s, exception.message, exception.backtrace]
end
log(message) click to toggle source

Write a log message back to the shell.

# File lib/relaxo/query_server/context.rb, line 99
def log(message)
        @shell.write_object ['log', message]
end
parse_function(text, scope, filename = 'query-server') click to toggle source

Given a function as text, and an execution scope, return a callable object.

# File lib/relaxo/query_server/context.rb, line 34
def parse_function(text, scope, filename = 'query-server')
        safe_level = @options[:safe] || 0

        function = lambda { $SAFE = safe_level; eval(text, scope, filename) }.call

        unless function.respond_to? :call
                raise CompilationError.new('Expression does not evaluate to procedure!')
        end
        
        return function
end
run(command) click to toggle source

Process a single command as per the query server protocol.

# File lib/relaxo/query_server/context.rb, line 66
def run(command)
        case command[0]
        # ** Map functionality
        when 'add_fun'
                @mapper.add_function command[1]; true
        when 'add_lib'
                @mapper.add_libraries command[1]
                @reducer.add_libraries command[1]; true
        when 'map_doc'
                @mapper.map command[1]
        when 'reset'
                @config = command[1] || {}
                @mapper = Mapper.new(self); true
        
        # ** Reduce functionality
        when 'reduce'
                @reducer.reduce command[1], command[2]
        when 'rereduce'
                @reducer.rereduce command[1], command[2]
        
        # ** Design document functionality
        when 'ddoc'
                if command[1] == 'new'
                        @designer.create(command[2], command[3]); true
                else
                        @designer.run(command[1], command[2], command[3])
                end
        end
rescue Exception => exception
        error_for_exception(exception)
end