ruby-nuggets 0.5.6 → 0.5.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README +1 -1
- data/lib/nuggets/hash/nest_mixin.rb +20 -6
- data/lib/nuggets/hash/unroll_mixin.rb +26 -11
- data/lib/nuggets/version.rb +1 -1
- metadata +2 -2
data/README
CHANGED
@@ -50,14 +50,28 @@ module Nuggets
|
|
50
50
|
# hash[:foo][:bar][:b] = { :x => 0, :y => 3 }
|
51
51
|
# hash
|
52
52
|
# #=> {:foo=>{:bar=>{:b=>{:y=>3, :x=>0}, :a=>{:y=>2, :x=>1}}}}
|
53
|
-
def nest(depth = 0, value = default = Object.new
|
54
|
-
|
55
|
-
|
56
|
-
|
53
|
+
def nest(depth = 0, value = default = Object.new)
|
54
|
+
if depth.zero?
|
55
|
+
if default
|
56
|
+
if block_given?
|
57
|
+
new { |hash, key| hash[key] = yield(key) }
|
58
|
+
else
|
59
|
+
new { |hash, key| hash[key] = hash.default }
|
60
|
+
end
|
57
61
|
else
|
58
|
-
|
62
|
+
new { |hash, key| hash[key] = value }
|
59
63
|
end
|
60
|
-
|
64
|
+
else
|
65
|
+
if default
|
66
|
+
if block_given?
|
67
|
+
new { |hash, key| hash[key] = nest(depth - 1, &::Proc.new) }
|
68
|
+
else
|
69
|
+
new { |hash, key| hash[key] = nest(depth - 1) }
|
70
|
+
end
|
71
|
+
else
|
72
|
+
new { |hash, key| hash[key] = nest(depth - 1, value) }
|
73
|
+
end
|
74
|
+
end
|
61
75
|
end
|
62
76
|
|
63
77
|
end
|
@@ -31,32 +31,47 @@ module Nuggets
|
|
31
31
|
|
32
32
|
# call-seq:
|
33
33
|
# hash.unroll(*value_keys) => anArray
|
34
|
-
# hash.unroll(*value_keys
|
34
|
+
# hash.unroll(*value_keys, :sort_by => { |key, value| ... }) => anArray
|
35
|
+
# hash.unroll(*value_keys) { |value_hash| ... } => anArray
|
35
36
|
#
|
36
37
|
# "Unrolls" a nested hash, so that each path through +hash+ results in a
|
37
|
-
# row that is, e.g., suitable for use with CSV.
|
38
|
-
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
#
|
38
|
+
# row that is, e.g., suitable for use with CSV.
|
39
|
+
#
|
40
|
+
# Note that from the final hash ("value hash") only the values are used,
|
41
|
+
# namely, if +value_keys+ are given, the values at those keys are returned.
|
42
|
+
# If a block is given, the +value_hash+ is passed to that block for any
|
43
|
+
# additional processing or sanitization.
|
44
|
+
#
|
45
|
+
# If +sort_by+ is given, all hashes are passed through that block for
|
46
|
+
# sorting before being put into the result array.
|
42
47
|
#
|
43
48
|
# Examples:
|
44
49
|
#
|
45
50
|
# { :foo => { :bar => { :a => { :x => 1, :y => 2 }, :b => { :x => 0, :y => 3 } } } }.unroll
|
46
51
|
# #=> [[:foo, :bar, :b, 3, 0], [:foo, :bar, :a, 2, 1]]
|
47
52
|
#
|
48
|
-
# { :foo => { :bar => { :a => { :x => 1, :y => 2 }, :b => { :x => 0, :y => 3 } } } }.unroll(
|
53
|
+
# { :foo => { :bar => { :a => { :x => 1, :y => 2 }, :b => { :x => 0, :y => 3 } } } }.unroll(:sort_by => :to_s)
|
49
54
|
# #=> [[:foo, :bar, :a, 1, 2], [:foo, :bar, :b, 0, 3]]
|
50
|
-
|
55
|
+
#
|
56
|
+
# { :foo => { :bar => { :a => { :x => 1, :y => 2 }, :b => { :x => 0, :y => 3 } } } }.unroll { |data| data[:x] = nil; data[:y] *= 2 }
|
57
|
+
# #=> [[:foo, :bar, :b, 6, nil], [:foo, :bar, :a, 4, nil]]
|
58
|
+
def unroll(*value_keys, &block)
|
59
|
+
args = value_keys.dup
|
60
|
+
|
61
|
+
options = value_keys.last.is_a?(::Hash) ? value_keys.pop : {}
|
62
|
+
do_sort = options.has_key?(:sort_by)
|
63
|
+
|
51
64
|
rows = []
|
52
65
|
|
53
66
|
if values.first.is_a?(self.class) # if any is, then all are
|
54
|
-
(
|
55
|
-
value.unroll(*
|
67
|
+
(do_sort ? sort_by(&options[:sort_by]) : self).each { |key, value|
|
68
|
+
value.unroll(*args, &block).each { |row| rows << [key, *row] }
|
56
69
|
}
|
57
70
|
else
|
71
|
+
block[self] if block
|
72
|
+
|
58
73
|
rows << if value_keys.empty?
|
59
|
-
|
74
|
+
do_sort ? sort_by(&options[:sort_by]).map { |key, value| value } : values
|
60
75
|
else
|
61
76
|
values_at(*value_keys)
|
62
77
|
end
|
data/lib/nuggets/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-nuggets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jens Wille
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-07-
|
12
|
+
date: 2009-07-27 00:00:00 +02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|