class Hash

Extension class for Hash

Public Instance Methods

apply_defaults(other_hash) click to toggle source

Apply other hash values if current value is blank

# File lib/libis/tools/extend/hash.rb, line 48
def apply_defaults(other_hash)
  self.merge(other_hash) {|_,v, w| v.blank? ? w : v}
end
apply_defaults!(other_hash) click to toggle source

Apply in-place other hash values if current value is blank

# File lib/libis/tools/extend/hash.rb, line 53
def apply_defaults!(other_hash)
  self.merge!(other_hash) {|_,v, w| v.blank? ? w : v}
end
cleanup() click to toggle source

Removes all hash entries for which value.empty? is true

# File lib/libis/tools/extend/hash.rb, line 5
def cleanup
  self.delete_if { |_,v| v.nil? || (v.respond_to?(:empty?) ? v.empty? : false) }
end
key_strings_to_symbols(options = {}) click to toggle source

Return new Hash with all keys converted to symbols. @param [Hash] options valid options are:

* recursive : perform operation recursively
* upcase : convert all keys to upper case
* downcase : convert all keys to lower case
all options are false by default
# File lib/libis/tools/extend/hash.rb, line 77
def key_strings_to_symbols(options = {})
  options = {resursive: false, upcase: false, downcase: false}.merge options

  r = Hash.new
  self.each_pair do |k,v|

    k = k.to_s if k.kind_of? Symbol
    if k.kind_of? String
      k = k.downcase if options[:downcase]
      k = k.upcase if options[:upcase]
      k = k.to_sym
    end

    if options[:recursive]
      case v
        when Hash
          v = v.key_strings_to_symbols options
        when Array
          # noinspection RubyResolve
          v = v.collect { |a| (a.kind_of? Hash) ? a.key_strings_to_symbols(options) :  Marshal.load(Marshal.dump(a)) }
        else
          # noinspection RubyResolve
          v = Marshal.load(Marshal.dump(v))
      end
    end

    r[k] = v

  end

  r
end
key_strings_to_symbols!(options = {}) click to toggle source

Convert all keys to symbols. In-place operation. @param (see key_strings_to_symbols)

# File lib/libis/tools/extend/hash.rb, line 67
def key_strings_to_symbols!(options = {})
  self.replace self.key_strings_to_symbols options
end
key_symbols_to_strings(options = {}) click to toggle source

Return new Hash with all keys converted to strings. (see key_strings_to_symbols) @param (see key_strings_to_symbols)

# File lib/libis/tools/extend/hash.rb, line 128
def key_symbols_to_strings(options = {})
options = {resursive: false, upcase: false, downcase: false}.merge options

  r = Hash.new
  self.each_pair do |k,v|

    k = k.to_sym if k.kind_of? String
    if k.kind_of? Symbol
      k = k.to_s
      k = k.downcase if options[:downcase]
      k = k.upcase if options[:upcase]
    end

    if options[:recursive]
      case v
        when Hash
          v = v.key_symbols_to_strings(options)
        when Array
          # noinspection RubyResolve
          v = v.collect { |a| (a.kind_of? Hash) ? a.key_symbols_to_strings(options) : Marshal.load(Marshal.dump(a)) }
        else
          # noinspection RubyResolve
          v = Marshal.load(Marshal.dump(v))
      end
    end

    r[k] = v

  end

  r
end
key_symbols_to_strings!(options = {}) click to toggle source

Convert all keys to strings. In-place operation. (@see key_symbols_to_strings) @param (see key_symbols_to_strings)

# File lib/libis/tools/extend/hash.rb, line 121
def key_symbols_to_strings!(options = {})
  self.replace self.key_symbols_to_strings options
end
recursive_cleanup() click to toggle source

Removes all hash entries for which value.empty? is true. Performed recursively.

# File lib/libis/tools/extend/hash.rb, line 10
def recursive_cleanup
  each { |_, v| v.recursive_cleanup if Array === v || Hash === v }
  cleanup
end
recursive_merge(other_hash) click to toggle source

Merges two hashes, but does so recursively.

# File lib/libis/tools/extend/hash.rb, line 16
def recursive_merge(other_hash)
  self.merge(other_hash) do |_, old_val, new_val|
    if old_val.is_a? Hash
      old_val.recursive_merge new_val
    else
      new_val
    end
  end
end
recursive_merge!(other_hash) click to toggle source

Merges two hashes in-place, but does so recursively.

# File lib/libis/tools/extend/hash.rb, line 27
def recursive_merge!(other_hash)
  self.merge!(other_hash) do |_, old_val, new_val|
    if old_val.is_a? Hash
      old_val.recursive_merge new_val
    else
      new_val
    end
  end
end
reverse_merge(other_hash) click to toggle source

Merges two hashes with priority for the first hash

# File lib/libis/tools/extend/hash.rb, line 38
def reverse_merge(other_hash)
  self.merge(other_hash) {|_,v, _| v}
end
reverse_merge!(other_hash) click to toggle source

Merges two hashes in-place with priority for the first hash

# File lib/libis/tools/extend/hash.rb, line 43
def reverse_merge!(other_hash)
  self.merge!(other_hash) {|_,v, _| v}
end
stringify_keys() click to toggle source
# File lib/libis/tools/extend/hash.rb, line 110
def stringify_keys
  self.transform_keys {|k| k.to_s}
end
stringify_keys!() click to toggle source
# File lib/libis/tools/extend/hash.rb, line 114
def stringify_keys!
  self.transform_keys! {|k| k.to_s}
end
symbolize_keys() click to toggle source
# File lib/libis/tools/extend/hash.rb, line 57
def symbolize_keys
  self.transform_keys {|k| k.to_sym}
end
symbolize_keys!() click to toggle source
# File lib/libis/tools/extend/hash.rb, line 61
def symbolize_keys!
  self.transform_keys! {|k| k.to_sym}
end
transform_keys() { |key| ... } click to toggle source
# File lib/libis/tools/extend/hash.rb, line 161
def transform_keys
  result = {}
  each_key do |key|
    result[yield(key)] = self[key]
  end
  result
end
transform_keys!() { |key| ... } click to toggle source
# File lib/libis/tools/extend/hash.rb, line 169
def transform_keys!
  keys.each do |key|
    self[yield(key)] = delete(key)
  end
  self
end
transform_values() { |value| ... } click to toggle source
# File lib/libis/tools/extend/hash.rb, line 176
def transform_values
  return enum_for(:transform_values) { size } unless block_given?
  return {} if empty?
  result = self.class.new
  each do |key, value|
    result[key] = yield(value)
  end
  result
end
transform_values!() { |value| ... } click to toggle source
# File lib/libis/tools/extend/hash.rb, line 186
def transform_values!
  return enum_for(:transform_values!) { size } unless block_given?
  each do |key, value|
    self[key] = yield(value)
  end
end