291fd69cd64499e3df3aa218d0edbe441845a5ee
[packages/precise/mcollective.git] / lib / mcollective / rpc / result.rb
1 module MCollective
2   module RPC
3     # Simple class to manage compliant results from MCollective::RPC agents
4     #
5     # Currently it just fakes Hash behaviour to the result to remain backward
6     # compatible but it also knows which agent and action produced it so you
7     # can associate results to a DDL
8     class Result
9       attr_reader :agent, :action, :results
10
11       include Enumerable
12
13       def initialize(agent, action, result={})
14         @agent = agent
15         @action = action
16         @results = result
17       end
18
19       def [](idx)
20         @results[idx]
21       end
22
23       def []=(idx, item)
24         @results[idx] = item
25       end
26
27       def fetch(key, default)
28         @results.fetch(key, default)
29       end
30
31       def each
32         @results.each_pair {|k,v| yield(k,v) }
33       end
34
35       def to_json(*a)
36         {:agent => @agent,
37          :action => @action,
38          :sender => @results[:sender],
39          :statuscode => @results[:statuscode],
40          :statusmsg => @results[:statusmsg],
41          :data => @results[:data]}.to_json(*a)
42       end
43     end
44   end
45 end