Class: Hash
Overview
:nodoc:
Class Method Summary collapse
Instance Method Summary collapse
-
#all_keys ⇒ Object
:nodoc:.
-
#except(*items) ⇒ Object
:nodoc:.
-
#except!(*keys) ⇒ Object
:nodoc:.
- #extractable_options? ⇒ Boolean
- #has_deep_key?(key) ⇒ Boolean
-
#serialize ⇒ Object
:nodoc:.
- #slice(*keys) ⇒ Object
- #slice!(*keys) ⇒ Object
-
#symbolize_keys ⇒ Object
:nodoc:.
-
#symbolize_keys! ⇒ Object
:nodoc:.
Class Method Details
.hash_traverse(hash, &block) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/yandex-webmaster/core_ext/hash.rb', line 76 def self.hash_traverse(hash, &block) hash.each do |key, val| block.call(key) case val when Hash val.keys.each do |k| _hash_traverse(val, &block) end when Array val.each do |item| _hash_traverse(item, &block) end end end return hash end |
Instance Method Details
#all_keys ⇒ Object
:nodoc:
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/yandex-webmaster/core_ext/hash.rb', line 61 def all_keys # :nodoc: keys = self.keys keys.each do |key| if self[key].is_a?(Hash) keys << self[key].all_keys.compact.flatten next end end keys.flatten end |
#except(*items) ⇒ Object
:nodoc:
4 5 6 |
# File 'lib/yandex-webmaster/core_ext/hash.rb', line 4 def except(*items) # :nodoc: self.dup.except!(*items) end |
#except!(*keys) ⇒ Object
:nodoc:
8 9 10 11 12 |
# File 'lib/yandex-webmaster/core_ext/hash.rb', line 8 def except!(*keys) # :nodoc: copy = self.dup keys.each { |key| copy.delete!(key) } copy end |
#extractable_options? ⇒ Boolean
53 54 55 |
# File 'lib/yandex-webmaster/core_ext/hash.rb', line 53 def instance_of?(Hash) end |
#has_deep_key?(key) ⇒ Boolean
72 73 74 |
# File 'lib/yandex-webmaster/core_ext/hash.rb', line 72 def has_deep_key?(key) self.all_keys.include? key end |
#serialize ⇒ Object
:nodoc:
57 58 59 |
# File 'lib/yandex-webmaster/core_ext/hash.rb', line 57 def serialize # :nodoc: self.map { |key, val| [key, val].join("=") }.join("&") end |
#slice(*keys) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/yandex-webmaster/core_ext/hash.rb', line 14 def slice(*keys) keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true) hash = self.class.new keys.each { |k| hash[k] = self[k] if has_key?(k) } hash end |
#slice!(*keys) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/yandex-webmaster/core_ext/hash.rb', line 21 def slice!(*keys) keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true) omit = slice(*self.keys - keys) hash = slice(*keys) replace(hash) omit end |
#symbolize_keys ⇒ Object
:nodoc:
29 30 31 32 33 34 |
# File 'lib/yandex-webmaster/core_ext/hash.rb', line 29 def symbolize_keys # :nodoc: inject({}) do |hash, (key, value)| hash[(key.to_sym rescue key) || key] = value hash end end |
#symbolize_keys! ⇒ Object
:nodoc:
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/yandex-webmaster/core_ext/hash.rb', line 36 def symbolize_keys! # :nodoc: hash = symbolize_keys hash.each do |key, val| hash[key] = case val when Hash val.symbolize_keys! when Array val.map do |item| item.is_a?(Hash) ? item.symbolize_keys! : item end else val end end return hash end |