Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#_is_empty?(i) ⇒ Boolean

Returns:



144
145
146
# File 'lib/hashlib.rb', line 144

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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/hashlib.rb', line 104

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



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/hashlib.rb', line 143

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/hashlib.rb', line 124

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, &block)
    else
      rv = yield(k, v, path)
      root[k] = rv if inplace === true
    end

    path.pop
  end
end

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



139
140
141
# File 'lib/hashlib.rb', line 139

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
# 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)
      if rv.has_key?(path[i])
        rv = rv[path[i]]
        next
      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



99
100
101
102
# File 'lib/hashlib.rb', line 99

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



93
94
95
96
97
# File 'lib/hashlib.rb', line 93

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

#set(path, value) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/hashlib.rb', line 69

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



165
166
167
168
169
170
171
# File 'lib/hashlib.rb', line 165

def stringify_keys()
  rv = {}
  each do |k, v|
    rv[k.to_s] = (v.is_a?(Hash) ? v.stringify_keys() : v)
  end
  return rv
end

#symbolize_keysObject



173
174
175
176
177
178
179
# File 'lib/hashlib.rb', line 173

def symbolize_keys()
  rv = {}
  each do |k, v|
    rv[(k.to_sym rescue k)] = (v.is_a?(Hash) ? v.symbolize_keys() : v)
  end
  return rv
end

#unset(path) ⇒ Object



89
90
91
# File 'lib/hashlib.rb', line 89

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