Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq_unique_jobs/core_ext.rb

Overview

Monkey patches for the ruby Hash

Instance Method Summary collapse

Instance Method Details

#deep_stringify_keysHash<String>

Depp converts all keys to string

Returns:



32
33
34
# File 'lib/sidekiq_unique_jobs/core_ext.rb', line 32

def deep_stringify_keys
  deep_transform_keys(&:to_s)
end

#deep_transform_keysHash<String>

Deep transfor all keys by yielding to the caller

Returns:



44
45
46
# File 'lib/sidekiq_unique_jobs/core_ext.rb', line 44

def deep_transform_keys(&)
  _deep_transform_keys_in_object(self, &)
end

#slice(*keys) ⇒ Hash

Returns only the matching keys in a new hash

Parameters:

  • keys (Array<String>, Array<Symbol>)

    the keys to match

Returns:



19
20
21
22
# File 'lib/sidekiq_unique_jobs/core_ext.rb', line 19

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

Parameters:

  • keys (Array<String>, Array<Symbol>)

    the keys to match

Returns:



85
86
87
88
89
90
91
92
93
# File 'lib/sidekiq_unique_jobs/core_ext.rb', line 85

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_keysHash<String>

Converts all keys to string

Returns:



56
57
58
# File 'lib/sidekiq_unique_jobs/core_ext.rb', line 56

def stringify_keys
  transform_keys(&:to_s)
end

#transform_keysHash

Transforms all keys by yielding to the caller

Returns:



68
69
70
71
72
73
74
# File 'lib/sidekiq_unique_jobs/core_ext.rb', line 68

def transform_keys
  result = {}
  each_key do |key|
    result[yield(key)] = self[key]
  end
  result
end