Class: Hash
Overview
:nodoc:
Instance Method Summary collapse
-
#slice(*keys) ⇒ Object
:nodoc:.
-
#stringify_keys ⇒ Object
:nodoc:.
-
#stringify_keys! ⇒ Object
:nodoc:.
-
#stringify_keys_recursively ⇒ Object
:nodoc:.
-
#symbolize_keys ⇒ Object
:nodoc:.
-
#symbolize_keys! ⇒ Object
:nodoc:.
-
#symbolize_keys_recursively ⇒ Object
:nodoc:.
-
#to_struct ⇒ Object
:nodoc:.
-
#to_struct_recursively ⇒ Object
:nodoc:.
Instance Method Details
#slice(*keys) ⇒ Object
:nodoc:
2 3 4 5 6 7 |
# File 'lib/dropbox/extensions/hash.rb', line 2 def slice(*keys) #:nodoc: keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key) hash = self.class.new keys.each { |k| hash[k] = self[k] if has_key?(k) } hash end |
#stringify_keys ⇒ Object
:nodoc:
27 28 29 30 31 32 |
# File 'lib/dropbox/extensions/hash.rb', line 27 def stringify_keys # :nodoc: inject({}) do |, (key, value)| [(key.to_s rescue key) || key] = value end end |
#stringify_keys! ⇒ Object
:nodoc:
34 35 36 |
# File 'lib/dropbox/extensions/hash.rb', line 34 def stringify_keys! # :nodoc: self.replace(self.stringify_keys) end |
#stringify_keys_recursively ⇒ Object
:nodoc:
38 39 40 41 42 43 |
# File 'lib/dropbox/extensions/hash.rb', line 38 def stringify_keys_recursively # :nodoc: hsh = stringify_keys hsh.each { |k, v| hsh[k] = v.stringify_keys_recursively if v.kind_of?(Hash) } hsh.each { |k, v| hsh[k] = v.map { |i| i.kind_of?(Hash) ? i.stringify_keys_recursively : i } if v.kind_of?(Array) } return hsh end |
#symbolize_keys ⇒ Object
:nodoc:
9 10 11 12 13 14 |
# File 'lib/dropbox/extensions/hash.rb', line 9 def symbolize_keys # :nodoc: inject({}) do |, (key, value)| [(key.to_sym rescue key) || key] = value end end |
#symbolize_keys! ⇒ Object
:nodoc:
16 17 18 |
# File 'lib/dropbox/extensions/hash.rb', line 16 def symbolize_keys! # :nodoc: self.replace(self.symbolize_keys) end |
#symbolize_keys_recursively ⇒ Object
:nodoc:
20 21 22 23 24 25 |
# File 'lib/dropbox/extensions/hash.rb', line 20 def symbolize_keys_recursively # :nodoc: hsh = symbolize_keys hsh.each { |k, v| hsh[k] = v.symbolize_keys_recursively if v.kind_of?(Hash) } hsh.each { |k, v| hsh[k] = v.map { |i| i.kind_of?(Hash) ? i.symbolize_keys_recursively : i } if v.kind_of?(Array) } return hsh end |
#to_struct ⇒ Object
:nodoc:
45 46 47 48 49 50 51 52 53 |
# File 'lib/dropbox/extensions/hash.rb', line 45 def to_struct # :nodoc: struct = Struct.new(*keys).new(*values) # attach methods for any predicate keys, since Struct.new doesn't seem to do that pred_keys = slice(*(keys.select { |key| key.to_s.ends_with?('?') })) pred_keys.each do |key, val| struct.eigenclass.send(:define_method, key.to_sym) { return val } end return struct end |
#to_struct_recursively ⇒ Object
:nodoc:
55 56 57 58 59 60 |
# File 'lib/dropbox/extensions/hash.rb', line 55 def to_struct_recursively # :nodoc: hsh = dup hsh.each { |k, v| hsh[k] = v.to_struct_recursively if v.kind_of?(Hash) } hsh.each { |k, v| hsh[k] = v.map { |i| i.kind_of?(Hash) ? i.to_struct_recursively : i } if v.kind_of?(Array) } return hsh.to_struct end |