Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/rack/tracker/extensions.rb
Instance Method Summary collapse
- #_deep_transform_keys_in_object!(object, &block) ⇒ Object
- #compact ⇒ Object
- #deep_merge!(other_hash, &block) ⇒ Object
-
#deep_stringify_keys! ⇒ Object
NOTE Back ported from Rails 4 to 3 Destructively convert all keys to strings.
-
#deep_transform_keys!(&block) ⇒ Object
NOTE Back ported from Rails 4 to 3 Destructively convert all keys by using the block operation.
- #stringify_values ⇒ Object
Instance Method Details
#_deep_transform_keys_in_object!(object, &block) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rack/tracker/extensions.rb', line 42 def _deep_transform_keys_in_object!(object, &block) case object when Hash object.keys.each do |key| value = object.delete(key) object[yield(key)] = _deep_transform_keys_in_object!(value, &block) end object when Array object.map! {|e| _deep_transform_keys_in_object!(e, &block)} else object end end |
#compact ⇒ Object
18 19 20 |
# File 'lib/rack/tracker/extensions.rb', line 18 def compact select { |_, value| !value.nil? } end |
#deep_merge!(other_hash, &block) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rack/tracker/extensions.rb', line 22 def deep_merge!(other_hash, &block) other_hash.each_pair do |k,v| tv = self[k] if tv.is_a?(Hash) && v.is_a?(Hash) self[k] = tv.deep_merge(v, &block) else self[k] = block && tv ? block.call(k, tv, v) : v end end self end |
#deep_stringify_keys! ⇒ Object
NOTE Back ported from Rails 4 to 3 Destructively convert all keys to strings. This includes the keys from the root hash and from all nested hashes.
61 62 63 |
# File 'lib/rack/tracker/extensions.rb', line 61 def deep_stringify_keys! deep_transform_keys!{ |key| key.to_s } end |
#deep_transform_keys!(&block) ⇒ Object
NOTE Back ported from Rails 4 to 3 Destructively convert all keys by using the block operation. This includes the keys from the root hash and from all nested hashes.
38 39 40 |
# File 'lib/rack/tracker/extensions.rb', line 38 def deep_transform_keys!(&block) _deep_transform_keys_in_object!(self, &block) end |
#stringify_values ⇒ Object
11 12 13 14 15 16 |
# File 'lib/rack/tracker/extensions.rb', line 11 def stringify_values inject({}) do |, (key, value)| [key] = value.to_s end end |