class Relaxo::QueryServer::Reducer

Implements the ‘reduce` and `rereduce` functions along with all associated state.

Public Class Methods

new(context) click to toggle source

Create a reducer attached to the given context.

Calls superclass method Relaxo::QueryServer::Loader::new
# File lib/relaxo/query_server/reducer.rb, line 41
def initialize(context)
        super()
        
        @context = context
end

Public Instance Methods

reduce(functions, items) click to toggle source

Apply the reduce function to a given list of items. Functions are typically in the form of:

functions = [lambda{|keys,values,rereduce| ...}]

such that:

items = [[key1, value1], [key2, value2], [key3, value3]]
functions.map{|function| function.call(all keys, all values, false)}

@param [Array] functions

An array of functions to apply.

@param [Array] items

A composite list of items.
# File lib/relaxo/query_server/reducer.rb, line 58
def reduce(functions, items)
        load_default
        
        functions = functions.collect do |function_text|
                @context.parse_function(function_text, binding)
        end
        
        keys, values = [], []
        
        items.each do |value|
                keys << value[0]
                values << value[1]
        end
        
        result = functions.map do |function|
                ReducingProcess.new(self, function).run(keys, values, false)
        end
        
        return [true, result]
end
rereduce(functions, values) click to toggle source

Apply the rereduce functions to a given list of values.

lambda{|keys,values,rereduce| ...}.call([], values, true)

@param [Array] functions

An array of functions to apply, in the form of:

@param [Array] values

An array of values to reduce
# File lib/relaxo/query_server/reducer.rb, line 86
def rereduce(functions, values)
        load_default
        
        functions = functions.collect do |function_text|
                @context.parse_function(function_text, binding)
        end
        
        result = functions.map do |function|
                ReducingProcess.new(self, function).run([], values, true)
        end
        
        return [true, result]
end