Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/hashlib.rb

Instance Method Summary collapse

Instance Method Details

#_is_empty?(i) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/hashlib.rb', line 152

def _is_empty?(i)
  i === nil or (i.is_a?(String) and i.strip.chomp.empty?) or (i.respond_to?(:empty?) and i.empty?)
end

#coalesce(prefix = nil, base = nil, delimiter = '_') ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/hashlib.rb', line 112

def coalesce(prefix=nil, base=nil, delimiter='_')
  base = self unless base
  rv = {}

  if base.is_a?(Hash)
    base.each do |k,v|
      if v
        base.coalesce(k,v,delimiter).each do |kk,vv|
          kk = (prefix.to_s+delimiter+kk.to_s) if prefix
          rv[kk.to_s] = vv
        end
      end
    end
  else
    rv[prefix.to_s] = base
  end

  rv
end

#compactObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/hashlib.rb', line 151

def compact
  def _is_empty?(i)
    i === nil or (i.is_a?(String) and i.strip.chomp.empty?) or (i.respond_to?(:empty?) and i.empty?)
  end

  each_recurse do |k,v,path|
    path = path.join('.')

    if v.is_a?(Array)
      v.reject!{|i| _is_empty?(i) }
      unset(path) if v.empty?

    else
      unset(path) if _is_empty?(v)
    end

    nil
  end

  self
end

#diff(other) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
# File 'lib/hashlib.rb', line 2

def diff(other)
  self.keys.inject({}) do |memo, key|
    unless self[key] == other[key]
      if other[key].is_a?(Hash)
        memo[key] = self[key].diff(other[key])
      else
        memo[key] = [self[key], other[key]]
      end
    end
    memo
  end
end

#each_recurse(root = self, path = [], inplace = false, &block) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/hashlib.rb', line 132

def each_recurse(root=self, path=[], inplace=false, &block)
  root.each do |k,v|
    path << k

    if v.is_a?(Hash)
      each_recurse(v, path, inplace, &block)
    else
      rv = yield(k, v, path)
      self.set(path, rv) if inplace === true
    end

    path.pop
  end
end

#each_recurse!(root = self, path = [], &block) ⇒ Object



147
148
149
# File 'lib/hashlib.rb', line 147

def each_recurse!(root=self, path=[], &block)
  each_recurse(root, path, true, &block)
end

#get(path, default = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/hashlib.rb', line 15

def get(path, default=nil)
  rv = self

# make path an array if not already
  if not path.is_a?(Array)
    path = path.to_s.split('.')
  end

# stringify path components
  path.collect!{|i| i.to_s }

# step through path components...
  path.each_index do |i|
  # hashes: if the current path component is in the hash, set the pointer
  #            and move on to the next path component
  #         otherwise return default value
  #
    if rv.is_a?(Hash)
    # attempt lookup by string
      if rv.keys.include?(path[i])
        rv = rv[path[i]]
        next

    # attempt lookup by symbol
      elsif (path[i].to_sym rescue nil).is_a?(Symbol) and rv.keys.include?(path[i].to_sym)
        rv = rv[path[i].to_sym]
        next

    # you fail it
      else
        return default
      end

  # arrays: flatten into one array, iterate over it.  for each element:
  #           if it is a hash, recurse into it from the next path component
  #           otherwise return it
  #         the resulant pointer should be fully populated from this point down
  #
    elsif rv.is_a?(Array)
      rv = (rv.flatten.collect do |j|
        if j.is_a?(Hash)
          j.get(path[(i)..-1], default)
        else
          j
        end
      end).compact

      next

  # scalars: if we're still in this loop and trying to look for a path component
  #          inside of a scalar value, then that won't exist and we just return default
    else
      return default
    end
  end

  return default if rv.nil? or (rv.is_a?(Array) and rv.flatten.compact.empty?)

# good job! you made it here!
  return rv
end

#join(inner_delimiter, outer_delimiter = nil) ⇒ Object



107
108
109
110
# File 'lib/hashlib.rb', line 107

def join(inner_delimiter, outer_delimiter=nil)
  outer_delimiter = inner_delimiter unless outer_delimiter
  self.to_a.collect{|i| i.join(inner_delimiter) }.join(outer_delimiter)
end

#rekey(from, to) ⇒ Object



101
102
103
104
105
# File 'lib/hashlib.rb', line 101

def rekey(from, to)
  value = get(from)
  unset(from)
  set(to, value)
end

#set(path, value) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/hashlib.rb', line 77

def set(path, value)
  if not path.is_a?(Array)
    path = path.to_s.strip.split(/[\/\.]/)
  end
  root = self

  path[0..-2].each do |p|
    root[p.to_s] = {} unless root[p.to_s].is_a?(Hash)
    root = root[p.to_s]
  end

  if value.nil?
    root.reject!{|k,v| k.to_s == path.last.to_s }
  else
    root[path.last.to_s] = value
  end

  self
end

#stringify_keysObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/hashlib.rb', line 173

def stringify_keys()
  rv = {}
  each do |k, v|
    if v.is_a?(Hash)
      v = v.stringify_keys()
    elsif v.is_a?(Array)
      v = v.collect do |i|
        if i.is_a?(Hash)
          i.stringify_keys()
        else
          i
        end
      end
    end

    rv[k.to_s] = v
  end

  return rv
end

#symbolize_keysObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/hashlib.rb', line 194

def symbolize_keys()
  rv = {}
  each do |k, v|
    if v.is_a?(Hash)
      v = v.symbolize_keys()
    elsif v.is_a?(Array)
      v = v.collect do |i|
        if i.is_a?(Hash)
          i.symbolize_keys()
        else
          i
        end
      end
    end

    rv[(k.to_sym rescue k)] = v
  end
  return rv
end

#unset(path) ⇒ Object



97
98
99
# File 'lib/hashlib.rb', line 97

def unset(path)
  set(path, nil)
end