X-Git-Url: https://git.donarmstrong.com/?p=dsa-puppet.git;a=blobdiff_plain;f=3rdparty%2Fmodules%2Faviator%2Ffeature%2Faviator%2Fhashish.rb;fp=3rdparty%2Fmodules%2Faviator%2Ffeature%2Faviator%2Fhashish.rb;h=26025af488e6719bee3438d6969df05e0805b696;hp=0000000000000000000000000000000000000000;hb=b7626cbcbb2fb8e7ce3dc5ac60e80a981175f9d3;hpb=8132e6bb1199463f5e334326659c974d4772b3e3 diff --git a/3rdparty/modules/aviator/feature/aviator/hashish.rb b/3rdparty/modules/aviator/feature/aviator/hashish.rb new file mode 100644 index 00000000..26025af4 --- /dev/null +++ b/3rdparty/modules/aviator/feature/aviator/hashish.rb @@ -0,0 +1,111 @@ +# Hash-ish! +# +# This class is implemented using composition rather than inheritance so +# that we have control over what operations it exposes to peers. +class Hashish + include Enumerable + + def initialize(hash={}) + @hash = hash + hashishify_values + end + + def ==(other_obj) + other_obj.class == self.class && + other_obj.hash == self.hash + end + + def [](key) + @hash[normalize(key)] + end + + def []=(key, value) + @hash[normalize(key)] = value + end + + def each(&block) + @hash.each(&block) + end + + def empty? + @hash.empty? + end + + def has_key?(name) + @hash.has_key? normalize(name) + end + + def hash + @hash + end + + def keys + @hash.keys + end + + def length + @hash.length + end + + def merge(other_hash) + Hashish.new(@hash.merge(other_hash)) + end + + def merge!(other_hash) + @hash.merge! other_hash + self + end + + def to_json(obj) + @hash.to_json(obj) + end + + def to_s + str = "{" + @hash.each do |key, value| + if value.kind_of? String + value = "'#{value}'" + elsif value.nil? + value = "nil" + elsif value.kind_of? Array + value = "[#{value.join(", ")}]" + end + + str += " #{key}: #{value}," + end + + str = str[0...-1] + " }" + str + end + + private + + # Hashishify all the things! + def hashishify_values + @hash.each do |key, value| + if @hash[key].kind_of? Hash + @hash[key] = Hashish.new(value) + elsif @hash[key].kind_of? Array + @hash[key].each_index do |index| + element = @hash[key][index] + if element.kind_of? Hash + @hash[key][index] = Hashish.new(element) + end + end + end + end + end + + def normalize(key) + if @hash.has_key? key + key + elsif key.is_a? String + key.to_sym + elsif key.is_a? Symbol + key.to_s + else + key + end + end + +end