Class: Hash

Inherits:
Object show all
Defined in:
lib/ruby/commons/core_ext/hash/keys.rb,
lib/ruby/commons/core_ext/hash/slice.rb,
lib/ruby/commons/core_ext/hash/except.rb,
lib/ruby/commons/core_ext/hash/values.rb,
lib/ruby/commons/core_ext/hash/compact.rb,
lib/ruby/commons/core_ext/object/blank.rb,
lib/ruby/commons/core_ext/object/deep_dup.rb

Instance Method Summary collapse

Instance Method Details

#mp_compactObject

Returns a hash with non nil values.

hash = { a: true, b: false, c: nil}
hash.compact # => { a: true, b: false}
hash # => { a: true, b: false, c: nil}
{ c: nil }.compact # => {}


9
10
11
# File 'lib/ruby/commons/core_ext/hash/compact.rb', line 9

def mp_compact
  self.select { |_, value| !value.nil? }
end

#mp_compact!Object

Replaces current hash with non nil values.

hash = { a: true, b: false, c: nil}
hash.compact! # => { a: true, b: false}
hash # => { a: true, b: false}


19
20
21
# File 'lib/ruby/commons/core_ext/hash/compact.rb', line 19

def mp_compact!
  self.reject! { |_, value| value.nil? }
end

#mp_deep_compact!Object

TODO



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby/commons/core_ext/hash/compact.rb', line 25

def mp_deep_compact!
  proc_val = Proc.new {}
  proc_hsh = Proc.new {}

  proc_val = Proc.new do |v|
    result = false
    case v
      when Hash
        v.reject!(&proc_hsh)
      when Array
        v.each(&proc_val)
      else
        result = v.nil?
    end
    result
  end

  proc_hsh = Proc.new do |k, v|
    proc_val.call(v)
  end

  delete_if(&proc_hsh)
end

#mp_deep_dupObject

Returns a deep copy of hash.

hash = { a: { b: 'b' } }
dup  = hash.mp_deep_dup
dup[:a][:c] = 'c'

hash[:a][:c] # => nil
dup[:a][:c]  # => "c"


44
45
46
47
48
49
# File 'lib/ruby/commons/core_ext/object/deep_dup.rb', line 44

def mp_deep_dup
  each_with_object(dup) do |(key, value), hash|
    hash.delete(key)
    hash[key.mp_deep_dup] = value.mp_deep_dup
  end
end

#mp_deep_stringify_keysObject

Returns a new hash with all keys converted to strings. This includes the keys from the root hash and from all nested hashes and arrays.

hash = { person: { name: 'Rob', age: '28' } }

hash.mp_deep_stringify_keys
# => {"person"=>{"name"=>"Rob", "age"=>"28"}}


98
99
100
# File 'lib/ruby/commons/core_ext/hash/keys.rb', line 98

def mp_deep_stringify_keys
  mp_deep_transform_keys(&:to_s)
end

#mp_deep_stringify_keys!Object

Destructively converts all keys to strings. This includes the keys from the root hash and from all nested hashes and arrays.



106
107
108
# File 'lib/ruby/commons/core_ext/hash/keys.rb', line 106

def mp_deep_stringify_keys!
  mp_deep_transform_keys!(&:to_s)
end

#mp_deep_symbolize_keysObject

Returns a new hash with all keys converted to symbols, as long as they respond to to_sym. This includes the keys from the root hash and from all nested hashes and arrays.

hash = { 'person' => { 'name' => 'Rob', 'age' => '28' } }

hash.mp_deep_symbolize_keys
# => {:person=>{:name=>"Rob", :age=>"28"}}


119
120
121
# File 'lib/ruby/commons/core_ext/hash/keys.rb', line 119

def mp_deep_symbolize_keys
  mp_deep_transform_keys { |key| key.to_sym rescue key }
end

#mp_deep_symbolize_keys!Object

Destructively converts all keys to symbols, as long as they respond to to_sym. This includes the keys from the root hash and from all nested hashes and arrays.



127
128
129
# File 'lib/ruby/commons/core_ext/hash/keys.rb', line 127

def mp_deep_symbolize_keys!
  mp_deep_transform_keys! { |key| key.to_sym rescue key }
end

#mp_deep_transform_keys(&block) ⇒ Object

Returns a new hash with all keys converted by the block operation. This includes the keys from the root hash and from all nested hashes and arrays.

hash = { person: { name: 'Rob', age: '28' } }

hash.mp_deep_transform_keys { |key| key.to_s.upcase }
# => {"PERSON"=>{"NAME"=>"Rob", "AGE"=>"28"}}


77
78
79
# File 'lib/ruby/commons/core_ext/hash/keys.rb', line 77

def mp_deep_transform_keys(&block)
  __mp_deep_transform_keys_in_object(self, &block)
end

#mp_deep_transform_keys!(&block) ⇒ Object

Destructively converts all keys by using the block operation. This includes the keys from the root hash and from all nested hashes and arrays.



85
86
87
# File 'lib/ruby/commons/core_ext/hash/keys.rb', line 85

def mp_deep_transform_keys!(&block)
  __mp_deep_transform_keys_in_object!(self, &block)
end

#mp_deep_transform_values(&block) ⇒ Object

Returns a new hash with all values converted by the block operation. This includes the values from the root hash and from all nested hashes and arrays.

hash = { person: { name: 'Rob', gender: 'Male' } }

hash.mp_deep_transform_values { |value| value.to_s.upcase }
# => {:person=>{:name=>"ROB", :gender=>"MALE"}}


35
36
37
# File 'lib/ruby/commons/core_ext/hash/values.rb', line 35

def mp_deep_transform_values(&block)
  __mp_deep_transform_values_in_object(self, &block)
end

#mp_deep_transform_values!(&block) ⇒ Object

Destructively converts all values by using the block operation. This includes the values from the root hash and from all nested hashes and arrays.



43
44
45
# File 'lib/ruby/commons/core_ext/hash/values.rb', line 43

def mp_deep_transform_values!(&block)
  __mp_deep_transform_values_in_object!(self, &block)
end

#mp_except(*keys) ⇒ Object

Returns a hash that includes everything but the given keys.

hash = { a: true, b: false, c: nil}
hash.mp_except(:c) # => { a: true, b: false}
hash # => { a: true, b: false, c: nil}

This is useful for limiting a set of parameters to everything but a few known toggles:

@person.update(params[:person].mp_except(:admin))


9
10
11
# File 'lib/ruby/commons/core_ext/hash/except.rb', line 9

def mp_except(*keys)
  dup.mp_except!(*keys)
end

#mp_except!(*keys) ⇒ Object

Replaces the hash without the given keys.

hash = { a: true, b: false, c: nil}
hash.mp_except!(:c) # => { a: true, b: false}
hash # => { a: true, b: false }


17
18
19
20
# File 'lib/ruby/commons/core_ext/hash/except.rb', line 17

def mp_except!(*keys)
  keys.each { |key| delete(key) }
  self
end

#mp_extract!(*keys) ⇒ Object

Removes and returns the key/value pairs matching the given keys.

{ a: 1, b: 2, c: 3, d: 4 }.mp_extract!(:a, :b) # => {:a=>1, :b=>2}
{ a: 1, b: 2 }.mp_extract!(:a, :x)             # => {:a=>1}


48
49
50
# File 'lib/ruby/commons/core_ext/hash/slice.rb', line 48

def mp_extract!(*keys)
  keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) }
end

#mp_slice(*keys) ⇒ Object

Slice a hash to include only the given keys. Returns a hash containing the given keys.

{ a: 1, b: 2, c: 3, d: 4 }.mp_slice(:a, :b)
# => {:a=>1, :b=>2}

This is useful for limiting an options hash to valid keys before passing to a method:

def search(criteria = {})
  criteria.assert_valid_keys(:mass, :velocity, :time)
end

search(options.mp_slice(:mass, :velocity, :time))

If you have an array of keys you want to limit to, you should splat them:

valid_keys = [:mass, :velocity, :time]
search(options.mp_slice(*valid_keys))


22
23
24
25
# File 'lib/ruby/commons/core_ext/hash/slice.rb', line 22

def mp_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 has_key?(k) }
end

#mp_slice!(*keys) ⇒ Object

Replaces the hash with only the given keys. Returns a hash containing the removed key/value pairs.

{ a: 1, b: 2, c: 3, d: 4 }.mp_slice!(:a, :b)
# => {:c=>3, :d=>4}


33
34
35
36
37
38
39
40
41
# File 'lib/ruby/commons/core_ext/hash/slice.rb', line 33

def mp_slice!(*keys)
  keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
  omit = mp_slice(*self.keys - keys)
  hash = mp_slice(*keys)
  hash.default      = default
  hash.default_proc = default_proc if default_proc
  replace(hash)
  omit
end

#mp_stringify_keysObject

Returns a new hash with all keys converted to strings.

hash = { name: 'Rob', age: '28' }

hash.mp_stringify_keys
# => {"name"=>"Rob", "age"=>"28"}


36
37
38
# File 'lib/ruby/commons/core_ext/hash/keys.rb', line 36

def mp_stringify_keys
  mp_transform_keys(&:to_s)
end

#mp_stringify_keys!Object

Destructively converts all keys to strings. Same as mp_stringify_keys, but modifies self.



43
44
45
# File 'lib/ruby/commons/core_ext/hash/keys.rb', line 43

def mp_stringify_keys!
  mp_transform_keys!(&:to_s)
end

#mp_symbolize_keysObject Also known as: mp_to_options

Returns a new hash with all keys converted to symbols, as long as they respond to to_sym.

hash = { 'name' => 'Rob', 'age' => '28' }

hash.mp_symbolize_keys
# => {:name=>"Rob", :age=>"28"}


55
56
57
# File 'lib/ruby/commons/core_ext/hash/keys.rb', line 55

def mp_symbolize_keys
  mp_transform_keys { |key| key.to_sym rescue key }
end

#mp_symbolize_keys!Object Also known as: mp_to_options!

Destructively converts all keys to symbols, as long as they respond to to_sym. Same as mp_symbolize_keys, but modifies self.



63
64
65
# File 'lib/ruby/commons/core_ext/hash/keys.rb', line 63

def mp_symbolize_keys!
  mp_transform_keys! { |key| key.to_sym rescue key }
end

#mp_transform_keysObject

Returns a new hash with all keys converted using the block operation.

hash = { name: 'Rob', age: '28' }

hash.mp_transform_keys { |key| key.to_s.upcase }
# => {"NAME"=>"Rob", "AGE"=>"28"}


9
10
11
12
13
14
15
16
# File 'lib/ruby/commons/core_ext/hash/keys.rb', line 9

def mp_transform_keys
  return enum_for(:mp_transform_keys) unless block_given?
  result = self.class.new
  each_key do |key|
    result[yield(key)] = self[key]
  end
  result
end

#mp_transform_keys!Object

Destructively converts all keys using the block operations. Same as mp_transform_keys but modifies self.



21
22
23
24
25
26
27
# File 'lib/ruby/commons/core_ext/hash/keys.rb', line 21

def mp_transform_keys!
  return enum_for(:mp_transform_keys!) unless block_given?
  keys.each do |key|
    self[yield(key)] = delete(key)
  end
  self
end

#mp_transform_valuesObject

Returns a new hash with the results of running block once for every value. The keys are unchanged.

{ a: 1, b: 2, c: 3 }.mp_transform_values { |k,v| v * 2 }
# => { a: 2, b: 4, c: 6 }


8
9
10
11
12
13
14
15
# File 'lib/ruby/commons/core_ext/hash/values.rb', line 8

def mp_transform_values
  return enum_for(:mp_transform_values) unless block_given?
  result = self.class.new
  each do |key, value|
    result[key] = yield(key, value)
  end
  result
end

#mp_transform_values!Object

Destructive mp_transform_values



19
20
21
22
23
24
# File 'lib/ruby/commons/core_ext/hash/values.rb', line 19

def mp_transform_values!
  return enum_for(:mp_transform_values!) unless block_given?
  each do |key, value|
    self[key] = yield(key, value)
  end
end