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)


141
142
143
# File 'lib/hashlib.rb', line 141

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



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

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



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

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/hashlib.rb', line 121

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



136
137
138
# File 'lib/hashlib.rb', line 136

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
# 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.split('.')
  end

# 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



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

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



90
91
92
93
94
# File 'lib/hashlib.rb', line 90

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

#set(path, value) ⇒ Object



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

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

#unset(path) ⇒ Object



86
87
88
# File 'lib/hashlib.rb', line 86

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