8ffe4d2f34861d8ddd9f347cf63f628f13f5bb81
[packages/precise/mcollective.git] / lib / mcollective / data / result.rb
1 module MCollective
2   module Data
3     class Result
4       # remove some methods that might clash with commonly
5       # used return data to improve the effectiveness of the
6       # method_missing lookup strategy
7       undef :type if method_defined?(:type)
8
9       def initialize(outputs)
10         @data = {}
11
12         outputs.keys.each do |output|
13           @data[output] = outputs[output].fetch(:default, nil)
14         end
15       end
16
17       def include?(key)
18         @data.include?(key.to_sym)
19       end
20
21       def [](key)
22         @data[key.to_sym]
23       end
24
25       def []=(key, val)
26         raise "Can only store String, Integer, Float or Boolean data but got #{val.class} for key #{key}" unless [String, Fixnum, Bignum, Float, TrueClass, FalseClass].include?(val.class)
27
28         @data[key.to_sym] = val
29       end
30
31       def keys
32         @data.keys
33       end
34
35       def method_missing(method, *args)
36         key = method.to_sym
37
38         raise NoMethodError, "undefined local variable or method `%s'" % key unless include?(key)
39
40         @data[key]
41       end
42     end
43   end
44 end