Class: Hash

Inherits:
Object show all
Defined in:
lib/sc-core-ext/hash.rb

Instance Method Summary collapse

Instance Method Details

#camelize_keysObject



58
59
60
61
62
63
64
# File 'lib/sc-core-ext/hash.rb', line 58

def camelize_keys
  stringified = stringify_keys
  stringified.rename(stringified.inject(self.class.new) do |renamed, (key, value)|
    renamed[key] = key.camelize
    renamed
  end)
end

#deep_dupObject

Performs a “deep copy” of this hash; that is, returns a Hash that is a duplicate of this Hash, and whose keys and values have each, in turn, had #deep_dup or #dup called on them. This should produce a Hash whose every element is a copy of the original.

This operation is expensive, and should be used sparingly.



25
26
27
28
29
30
31
32
# File 'lib/sc-core-ext/hash.rb', line 25

def deep_dup
  inject(self.class.new) do |new_hash, (key, value)|
    key = key.respond_to?(:deep_dup) ? key.deep_dup : key.dup?
    value = value.respond_to?(:deep_dup) ? value.deep_dup : value.dup?
    new_hash[key] = value
    new_hash
  end
end

#keys?(*items) ⇒ Boolean

Returns true if #key? would return true for each item specified.

Returns:

  • (Boolean)


15
16
17
18
# File 'lib/sc-core-ext/hash.rb', line 15

def keys?(*items)
  items.flatten.each { |item| return false unless key?(item) }
  true
end

#optionalizeObject Also known as: without_nil_values

Returns a hash that is a copy of this one, except that all nil values have been removed, making them essentially “optional” keys.



52
53
54
# File 'lib/sc-core-ext/hash.rb', line 52

def optionalize
  without_values(nil)
end

#rename(to) ⇒ Object

Takes a hash whose keys must match keys in this hash. Those keys will be renamed to match the corresponding value in the specified hash.

Keys not found are ignored.

Returns self.

Example:

{ :a => 1 }.rename(:a => :b)
  => {:b => 1}


77
78
79
80
81
82
83
# File 'lib/sc-core-ext/hash.rb', line 77

def rename(to)
  merge!(inject(self.class.new) do |hash, (old_key, value)|
    hash[to[old_key] || old_key] = value
    delete(old_key)
    hash
  end)
end

#stringify_valuesObject



10
11
12
# File 'lib/sc-core-ext/hash.rb', line 10

def stringify_values
  dup.stringify_values!
end

#stringify_values!Object

As the ActiveSupport extension #stringify_keys! except that it’s applied to values instead.



3
4
5
6
7
8
# File 'lib/sc-core-ext/hash.rb', line 3

def stringify_values!
  inject(self.class.new) do |new_hash, (key, value)|
    new_hash[key] = value.kind_of?(String) ? value : value.to_s
    new_hash
  end
end

#without(*keys) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/sc-core-ext/hash.rb', line 34

def without(*keys)
  keys.flatten!
  inject(self.class.new) do |hash, (key, value)|
    hash[key] = value unless keys.include?(key)
    hash
  end
end

#without_values(*values) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/sc-core-ext/hash.rb', line 42

def without_values(*values)
  values.flatten!
  inject(self.class.new) do |hash, (key, value)|
    hash[key] = value unless values.include?(value)
    hash
  end
end