Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/hashlib.rb
Instance Method Summary collapse
- #_is_empty?(i) ⇒ Boolean
- #coalesce(prefix = nil, base = nil, delimiter = '_') ⇒ Object
- #compact ⇒ Object
- #diff(other) ⇒ Object
- #each_recurse(options = {}, &block) ⇒ Object
- #each_recurse!(options = {}, &block) ⇒ Object
- #get(path, default = nil) ⇒ Object
- #join(inner_delimiter, outer_delimiter = nil) ⇒ Object
- #rekey(from, to) ⇒ Object
- #rget(path, default = nil) ⇒ Object
- #rset(path, value) ⇒ Object
- #set(path, value) ⇒ Object
- #stringify_keys ⇒ Object
- #symbolize_keys ⇒ Object
- #unset(path) ⇒ Object
Instance Method Details
#_is_empty?(i) ⇒ Boolean
170 171 172 |
# File 'lib/hashlib.rb', line 170 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
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/hashlib.rb', line 120 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 |
#compact ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/hashlib.rb', line 169 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(options = {}, &block) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/hashlib.rb', line 140 def each_recurse(={}, &block) [:root] = self.clone() if [:root].nil? [:path] = [] if [:path].nil? [:root].each do |k,v| [:path] << k if v.is_a?(Hash) yield(k, v, [:path]) if [:intermediate] === true each_recurse(.merge({ :root => v, :path => [:path] }), &block) else rv = yield(k, v, [:path]) self.set([:path], rv) if [:inplace] === true end [:path].pop end end |
#each_recurse!(options = {}, &block) ⇒ Object
163 164 165 166 167 |
# File 'lib/hashlib.rb', line 163 def each_recurse!(={}, &block) each_recurse(.merge({ :inplace => true }), &block) end |
#get(path, default = nil) ⇒ Object
15 16 17 |
# File 'lib/hashlib.rb', line 15 def get(path, default=nil) rget(path, default) end |
#join(inner_delimiter, outer_delimiter = nil) ⇒ Object
115 116 117 118 |
# File 'lib/hashlib.rb', line 115 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
109 110 111 112 113 |
# File 'lib/hashlib.rb', line 109 def rekey(from, to) value = get(from) unset(from) set(to, value) end |
#rget(path, default = nil) ⇒ Object
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 76 77 78 79 80 81 82 83 |
# File 'lib/hashlib.rb', line 23 def rget(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 |
#rset(path, value) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/hashlib.rb', line 85 def rset(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 |
#set(path, value) ⇒ Object
19 20 21 |
# File 'lib/hashlib.rb', line 19 def set(path, value) rset(path, value) end |
#stringify_keys ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/hashlib.rb', line 191 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_keys ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/hashlib.rb', line 212 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
105 106 107 |
# File 'lib/hashlib.rb', line 105 def unset(path) set(path, nil) end |