Sha256: 1631b17503c252e90f0816cab5c842f217230d90948be07bf0cc9fc4780a00be
Contents?: true
Size: 1.71 KB
Versions: 14
Compression:
Stored size: 1.71 KB
Contents
# Override Hash class with convience methods class Hash def self.transform_keys_to_symbols(value) return value if not value.is_a?(Hash) hash = value.inject({}){|memo,(k,v)| memo[k.to_sym] = Hash.transform_keys_to_symbols(v); memo} return hash end # Take keys of hash and transform those to a symbols def transform_keys_to_symbols inject({}){|memo, (k, v)| memo[k.to_sym] = Hash.transform_keys_to_symbols(v); memo} end def find_all_values_for(key) result = [] result << self[key] self.values.each do |hash_value| next if hash_value.is_a? Array values = [hash_value] values.each do |value| result += value.find_all_values_for(key) if value.is_a? Hash end end result.compact end # Value present in nested Hash. def include_value?(value) each_value do |v| return true if v == value next unless v.is_a? Hash v.each_value do |v| return true if v == value next unless v.is_a? Hash v.each_value do |v| return true if v == value end end end false end # Whether key is present at least once def include_key?(key) result = find_all_values_for key result != [] end # Loop through each item within a key within a Hash if the key exists # @param [Key] Key within hash to iterate through def each_if_not_null(key) case key.class.to_s when 'String' if self[key] self[key].each do |list_item| yield(list_item) end end when 'Array', 'Hash' if self[key[0]] if self[key[0]][key[1]] self[key[0]][key[1]].each do |list_item| yield(list_item) end end end end end end
Version data entries
14 entries across 14 versions & 1 rubygems