/
hash.rb
387 lines (342 loc) · 8.22 KB
/
hash.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
class Hash
#
# `hash1 + hash2` merge the two hashes, returning a new hash
#
def +(other)
merge(other)
end
#
# `hash1 - hash2` removes keys from hash1 that exist in hash2, returning a new hash
#
def -(other)
dup.delete_if { |k,v| other.includes?(k) }
end
#
# 'true' if the Hash has no entries
#
def blank?
not any?
end
#
# Runs "remove_blank_values" on self.
#
def remove_blank_values!
delete_if{|k,v| v.blank?}
self
end
#
# Returns a new Hash where blank values have been removed.
# (It checks if the value is blank by calling #blank? on it)
#
def remove_blank_values
dup.remove_blank_values!
end
#
# Runs map_values on self.
#
def map_values!(&block)
keys.each do |key|
value = self[key]
self[key] = yield(value)
end
self
end
#
# Transforms the values of the hash by passing them into the supplied
# block, and then using the block's result as the new value.
#
def map_values(&block)
dup.map_values!(&block)
end
#
# Runs map_keys on self.
#
def map_keys!(&block)
keys.each do |key|
value = delete(key)
self[yield(key)] = value
end
self
end
alias_method :transform_keys!, :map_keys!
#
# Transforms the keys of the hash by passing them into the supplied block,
# and then using the blocks result as the new key.
#
def map_keys(&block)
dup.map_keys!(&block)
end
alias_method :transform_keys, :map_keys
#
# Translate keys to other keys, given a hash of old-key to new-key mappings.
#
# eg: hash.translate_keys!(
# "Extreme Sports!!!!" => :extreme_sports,
# "Mediocre sports" => :sports,
# "Pretty okay sports" => :sports,
# "Golf" => :liesure_activities,
# )
#
def translate_keys!(mapping)
# TODO: Allow regexes and lambdas (eg: translate_keys!(/.+/ => ->(key) { key.to_sym })
mapping.each do |src,dest|
if includes? src
self[dest] = delete(src)
end
end
self
end
#
# Same as `translate_keys!`, except it returns a copy of the hash
#
def translate_keys(mapping)
dup.translate_keys!(mapping)
end
#
# Convert the keys to symbols in-place, for fun and profit
#
def symbolize_keys!
map_keys! { |k| k.to_sym }
end
#
# Return a hash with its keys converted to symbols, for great justice
#
def symbolize_keys
dup.symbolize_keys!
end
#
# Returns a hash containing only the keys passed as arguments.
#
def slice(*keys)
dup.slice!(*keys)
end
#
# Alters the hash so it contains only the keys passed as arguments.
#
def slice!(*keys)
keys = Set.new keys
delete_if { |k,v| not keys.include? k }
self
end
#
# Returns a new Hash whose values default to empty arrays. (Good for collecting things!)
#
# eg:
# Hash.of_arrays[:yays] << "YAY!"
#
def self.of_arrays
new {|h,k| h[k] = [] }
end
#
# Returns a new Hash whose values default to empty sets. (Good for collecting unique things!)
#
# eg:
# Hash.of_sets[:yays] << "Yay!"
#
def self.of_sets
new {|h,k| h[k] = Set.new }
end
#
# Returns a new Hash whose values default to 0. (Good for counting things!)
#
# eg:
# Hash.of_integers[:yays] += 1
#
def self.of_integers
new(0)
end
#
# Returns a new Hash which automatically assigns each unique key to an increasing counter.
#
# eg:
# > h = Hash.of_unique_ids
# => {}
# > h["Person"] #=> 0
# > h["Another Person"] #=> 1
# > h["Yet Another Person"] #=> 2
# > h["Person"] #=> 0
# > h
# => {"Person"=>0, "Another Person"=>1, "Yet Another Person"=>2}
#
def self.of_unique_ids
new { |h,k| h[k] = h.size }
end
#
# Hash keys become methods, kinda like OpenStruct. These methods have the lowest priority,
# so be careful. They will be overridden by any methods on Hash.
#
def self.lazy!
Hash.class_eval do
def method_missing(name, *args)
if args.any?
super
else
self[name] || self[name.to_s]
end
end
end
end
#
# `key?` and `includes?` is an alias for `include?`
#
alias_method :key?, :include?
alias_method :includes?, :include?
#
# Makes each element in the `path` array point to a hash containing the next element in the `path`.
# Useful for turning a bunch of strings (paths, module names, etc.) into a tree.
#
# Example:
# h = {}
# h.mkdir_p(["a", "b", "c"]) #=> {"a"=>{"b"=>{"c"=>{}}}}
# h.mkdir_p(["a", "b", "whoa"]) #=> {"a"=>{"b"=>{"c"=>{}, "whoa"=>{}}}}
#
def mkdir_p(path)
return if path.empty?
dir = path.first
self[dir] ||= {}
self[dir].mkdir_p(path[1..-1])
self
end
#
# Turn some nested hashes into a tree (returns an array of strings, padded on the left with indents.)
#
def tree(level=0, indent=" ")
result = []
dent = indent * level
each do |key, val|
result << dent+key.to_s
result += val.tree(level+1) if val.any?
end
result
end
#
# Print the result of `tree`
#
def print_tree(level=0, indent=" ", &block)
dent = indent * level
each do |key, val|
puts block_given? ? yield(key, level) : "#{dent}#{key}"
val.print_tree(level+1, indent, &block) if val.any?
end
end
#
# Convert the hash into a GET query.
#
def to_query
params = ''
stack = []
each do |k, v|
if v.is_a?(Hash)
stack << [k,v]
else
params << "#{k}=#{v}&"
end
end
stack.each do |parent, hash|
hash.each do |k, v|
if v.is_a?(Hash)
stack << ["#{parent}[#{k}]", v]
else
params << "#{parent}[#{k}]=#{v}&"
end
end
end
params.chop! # trailing &
params
end
#
# Query a hash using MQL (see: http://wiki.freebase.com/wiki/MQL_operators for reference)
#
# Examples:
# > query(name: /steve/)
# > query(/title/ => ??)
# > query(articles: [{title: ??}])
# > query(responses: [])
# > query("date_of_birth<" => "2000")
#
def query(template)
results = []
template.each do |key,val|
case key
when Regexp, String
when Array
when Hash
results += hash.query(template)
end
end
map do |key,val|
end
end
alias_method :mql, :query
#
# Return all the changes necessary to transform `self` into `other`. (Works on nested hashes.) The result is a hash of {:key => [old value, new value]} pairs.
#
# (NOTE: Since "nil" is used to denote a value was removed, you can't use this method to diff hashes where a value is "nil".)
#
def diff(other)
(self.keys + other.keys).uniq.inject({}) do |memo, key|
unless self[key] == other[key]
if self[key].kind_of?(Hash) && other[key].kind_of?(Hash)
memo[key] = self[key].diff(other[key])
else
memo[key] = [self[key], other[key]]
end
end
memo
end
end
#
# Applies a Hash#diff changeset to this hash.
#
def apply_diff!(changes)
path = [[self, changes]]
pos, local_changes = path.pop
while local_changes
local_changes.each_pair do |key, change|
if change.kind_of?(Array)
if change[1].nil?
pos.delete key
else
pos[key] = change[1]
end
else
path.push([pos[key], change])
end
end
pos, local_changes = path.pop
end
self
end
#
# Applies a Hash#diff changeset and returns the transformed hash.
#
def apply_diff(changes)
deep_dup.apply_diff!(changes)
end
#
# Duplicate this hash, including hashes nested inside of it.
#
def deep_dup
duplicate = self.dup
duplicate.each_pair do |k,v|
tv = duplicate[k]
duplicate[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_dup : v
end
duplicate
end
#
# Convert this Hash (and all nested Hashes, and all nested Arrays containing Hashes) to OpenStruct(s)
#
def to_ostruct
OpenStruct.new(self.map_values do |v|
v.respond_to?(:to_ostruct) ? v.to_ostruct : v
end)
end
#
# Convert this Hash to indented JSON (using JSON.pretty_generate)
#
def to_nicejson
JSON.pretty_generate(self)
end
alias_method :to_nice_json, :to_nicejson
end