5a59a915d57cebfd9f62f1c1c9654a3bc861e5d0
[packages/precise/mcollective.git] / lib / mcollective / aggregate / base.rb
1 module MCollective
2   class Aggregate
3     class Base
4       attr_accessor :name, :result, :output_name, :action, :aggregate_format, :arguments
5
6       def initialize(output_name, arguments, aggregate_format, action)
7         @name = self.class.to_s
8         @output_name = output_name
9
10         # Any additional arguments passed in the ddl after the output field will
11         # be stored in the arguments array which can be used in the function
12         @arguments = arguments
13         @aggregate_format = aggregate_format
14         @action = action
15         @result = {:value => nil, :type => nil, :output => output_name}
16
17         startup_hook
18       end
19
20       ['startup_hook', 'process_result'].each do |method|
21         define_method method do
22           raise RuntimeError, "'#{method}' method of function class #{@name} has not yet been implemented"
23         end
24       end
25
26       # Stops execution of the function and returns a specific ResultObject,
27       # aggregate functions will most likely override this but this is the simplest
28       # case so we might as well default to that
29       def summarize
30         raise "Result type is not set while trying to summarize aggregate function results" unless @result[:type]
31
32         result_class(@result[:type]).new(@result, @aggregate_format, @action)
33       end
34
35       def result_class(type)
36         Result.const_get("#{type.to_s.capitalize}Result")
37       end
38     end
39   end
40 end