Module: Extras::Hash

Defined in:
lib/extras/hash.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#any_key?(*keys) ⇒ Boolean


Check to see if any of the given keys exist.


Returns:

  • (Boolean)

121
122
123
124
125
# File 'lib/extras/hash.rb', line 121

def any_key?(*keys)
  keys.any? do |k|
    key?(k)
  end
end

#deep_merge(new_h) ⇒ Object


Merge hash into hash into hash into hash.



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/extras/hash.rb', line 104

def deep_merge(new_h)
  merge(new_h) do |_, ov, nv|
    if ov.respond_to?(:deep_merge) && nv.respond_to?(:deep_merge)
      then ov.deep_merge(
        nv
      )

    else
      nv
    end
  end
end

#stringify(allowed_keys: nil, allowed_vals: nil) ⇒ Object


Stringify keys and values of the current hash, skipping objects that are allowed to be skipped (leaving them as is, untouched.)



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/extras/hash.rb', line 57

def stringify(allowed_keys: nil, allowed_vals: nil)
  keys = allowed_keys || self.class.allowed[:keys]
  vals = allowed_vals || self.class.allowed[:vals]

  each_with_object({}) do |(k, v), h|
    v = v.to_s if !v.respond_to?(:stringify) && !vals.include?(v.class)
    k = k.to_s unless keys.include?(k.class)

    h[k] = !v.respond_to?(:stringify) ? v : v.stringify({
      :allowed_keys => allowed_keys,
      :allowed_vals => allowed_vals
    })
  end
end

#stringify_keys(allowed: nil) ⇒ Object


Stringify the keys of the current hash, skipping allowed objects.



76
77
78
79
80
81
82
83
84
# File 'lib/extras/hash.rb', line 76

def stringify_keys(allowed: nil)
  allowed ||= self.class.allowed[:keys]
  each_with_object({}) do |(k, v), h|
    k = k.to_s unless allowed.include?(k.class)
    h[k] = !v.respond_to?(:stringify_keys) ? v : v.stringify_keys({
      :allowed => allowed
    })
  end
end

#symbolize(allowed_keys: nil, allowed_vals: nil) ⇒ Object


Symbolize keys and values of the current hash, skipping allowed objects.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/extras/hash.rb', line 37

def symbolize(allowed_keys: nil, allowed_vals: nil)
  keys = allowed_keys || self.class.allowed[:keys]
  vals = allowed_vals || self.class.allowed[:vals]

  each_with_object({}) do |(k, v), h|
    k = k.to_sym unless !k.respond_to?(:to_sym) || keys.include?(k.class)
    v = v.to_sym unless !v.respond_to?(:to_sym) || vals.include?(v.class)

    h[k] = !v.respond_to?(:symbolize) ? v : v.symbolize({
      :allowed_keys => allowed_keys,
      :allowed_vals => allowed_vals
    })
  end
end

#symbolize_keys(allowed: nil) ⇒ Object


Symbolize the keys of the current hash, skipping allowed objects.



90
91
92
93
94
95
96
97
98
# File 'lib/extras/hash.rb', line 90

def symbolize_keys(allowed: nil)
  allowed ||= self.class.allowed[:keys]
  each_with_object({}) do |(k, v), h|
    k = k.to_sym if k.respond_to?(:to_sym) && !allowed.include?(k.class)
    h[k] = !v.respond_to?(:symbolize_keys) ? v : v.symbolize_keys({
      :allowed => allowed
    })
  end
end