Class: Hash

Inherits:
Object show all
Defined in:
lib/libis/tools/extend/hash.rb

Overview

Extension class for Hash

Instance Method Summary collapse

Instance Method Details

#cleanupObject

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



7
8
9
# File 'lib/libis/tools/extend/hash.rb', line 7

def cleanup
  self.delete_if { |_,v| v.nil? || (v.respond_to?(:empty?) ? v.empty? : false) }
end

#key_strings_to_symbols(options = {}) ⇒ Object

Return new Hash with all keys converted to symbols.

Parameters:

  • opts (Hash)

    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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/libis/tools/extend/hash.rb', line 54

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 = {}) ⇒ Object

Convert all keys to symbols. In-place operation.

Parameters:

  • opts (Hash)

    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



44
45
46
# File 'lib/libis/tools/extend/hash.rb', line 44

def key_strings_to_symbols!(options = {})
  self.replace self.key_strings_to_symbols options
end

#key_symbols_to_strings(options = {}) ⇒ Object

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

Parameters:

  • opts (Hash)

    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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/libis/tools/extend/hash.rb', line 97

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 = {}) ⇒ Object

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

Parameters:

  • opts (Hash)

    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



90
91
92
# File 'lib/libis/tools/extend/hash.rb', line 90

def key_symbols_to_strings!(options = {})
  self.replace self.key_symbols_to_strings options
end

#recursive_cleanupObject

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



12
13
14
15
16
17
18
# File 'lib/libis/tools/extend/hash.rb', line 12

def recursive_cleanup
  delete_proc = Proc.new do |_, v|
    v.delete_if(&delete_proc) if v.kind_of?(Hash)
    v.nil? || (v.respond_to?(:empty?) ? v.empty? : false)
  end
  self.delete_if &delete_proc
end

#recursive_merge(other_hash) ⇒ Object

Merges two hashes, but does so recursively.



21
22
23
24
25
26
27
28
29
# File 'lib/libis/tools/extend/hash.rb', line 21

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) ⇒ Object

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



32
33
34
35
36
37
38
39
40
# File 'lib/libis/tools/extend/hash.rb', line 32

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