e19a652ffa8b10a6c21dacf1735b62acae39ee0f
[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
10         @data = {}
11       end
12
13       def include?(key)
14         @data.include?(key.to_sym)
15       end
16
17       def [](key)
18         @data[key.to_sym]
19       end
20
21       def []=(key, val)
22         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)
23
24         @data[key.to_sym] = val
25       end
26
27       def keys
28         @data.keys
29       end
30
31       def method_missing(method, *args)
32         key = method.to_sym
33
34         raise NoMethodError, "undefined local variable or method `%s'" % key unless include?(key)
35
36         @data[key]
37       end
38     end
39   end
40 end