Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/sidekiq_unique_jobs/core_ext.rb
Overview
Monkey patches for the ruby Hash
Instance Method Summary collapse
-
#deep_stringify_keys ⇒ Hash<String>
Depp converts all keys to string.
-
#deep_transform_keys(&block) ⇒ Hash<String>
Deep transfor all keys by yielding to the caller.
-
#slice(*keys) ⇒ Hash
Returns only the matching keys in a new hash.
-
#slice!(*keys) ⇒ Hash
Removes all keys not provided from the current hash and returns it.
-
#stringify_keys ⇒ Hash<String>
Converts all keys to string.
-
#transform_keys ⇒ Hash
Transforms all keys by yielding to the caller.
Instance Method Details
#deep_stringify_keys ⇒ Hash<String>
Depp converts all keys to string
30 31 32 |
# File 'lib/sidekiq_unique_jobs/core_ext.rb', line 30 def deep_stringify_keys deep_transform_keys(&:to_s) end |
#deep_transform_keys(&block) ⇒ Hash<String>
Deep transfor all keys by yielding to the caller
42 43 44 |
# File 'lib/sidekiq_unique_jobs/core_ext.rb', line 42 def deep_transform_keys(&block) _deep_transform_keys_in_object(self, &block) end |
#slice(*keys) ⇒ Hash
Returns only the matching keys in a new hash
17 18 19 20 |
# File 'lib/sidekiq_unique_jobs/core_ext.rb', line 17 def slice(*keys) keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true) keys.each_with_object(self.class.new) { |k, hash| hash[k] = self[k] if key?(k) } end |
#slice!(*keys) ⇒ Hash
Removes all keys not provided from the current hash and returns it
83 84 85 86 87 88 89 90 91 |
# File 'lib/sidekiq_unique_jobs/core_ext.rb', line 83 def slice!(*keys) keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true) omit = slice(*self.keys - keys) hash = slice(*keys) hash.default = default hash.default_proc = default_proc if default_proc replace(hash) omit end |
#stringify_keys ⇒ Hash<String>
Converts all keys to string
54 55 56 |
# File 'lib/sidekiq_unique_jobs/core_ext.rb', line 54 def stringify_keys transform_keys(&:to_s) end |
#transform_keys ⇒ Hash
Transforms all keys by yielding to the caller
66 67 68 69 70 71 72 |
# File 'lib/sidekiq_unique_jobs/core_ext.rb', line 66 def transform_keys result = {} each_key do |key| result[yield(key)] = self[key] end result end |