ruby-nuggets 0.5.7 → 0.5.8
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/unroll_mixin.rb +15 -5
- data/lib/nuggets/version.rb +1 -1
- metadata +1 -1
data/README
CHANGED
@@ -31,7 +31,7 @@ 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 => ...) => anArray
|
35
35
|
# hash.unroll(*value_keys) { |value_hash| ... } => anArray
|
36
36
|
#
|
37
37
|
# "Unrolls" a nested hash, so that each path through +hash+ results in a
|
@@ -57,21 +57,31 @@ module Nuggets
|
|
57
57
|
# #=> [[:foo, :bar, :b, 6, nil], [:foo, :bar, :a, 4, nil]]
|
58
58
|
def unroll(*value_keys, &block)
|
59
59
|
args = value_keys.dup
|
60
|
-
|
61
60
|
options = value_keys.last.is_a?(::Hash) ? value_keys.pop : {}
|
62
|
-
|
61
|
+
|
62
|
+
do_sort = if options.has_key?(:sort_by)
|
63
|
+
lambda { sort_by(&options[:sort_by]) }
|
64
|
+
elsif options.has_key?(:sort)
|
65
|
+
sort_opt = options[:sort]
|
66
|
+
|
67
|
+
if sort_opt == true
|
68
|
+
lambda { sort }
|
69
|
+
elsif sort_opt.respond_to?(:to_proc)
|
70
|
+
lambda { sort(&sort_opt) }
|
71
|
+
end
|
72
|
+
end
|
63
73
|
|
64
74
|
rows = []
|
65
75
|
|
66
76
|
if values.first.is_a?(self.class) # if any is, then all are
|
67
|
-
(do_sort ?
|
77
|
+
(do_sort ? do_sort.call : self).each { |key, value|
|
68
78
|
value.unroll(*args, &block).each { |row| rows << [key, *row] }
|
69
79
|
}
|
70
80
|
else
|
71
81
|
block[self] if block
|
72
82
|
|
73
83
|
rows << if value_keys.empty?
|
74
|
-
do_sort ?
|
84
|
+
do_sort ? do_sort.call.map { |key, value| value } : values
|
75
85
|
else
|
76
86
|
values_at(*value_keys)
|
77
87
|
end
|
data/lib/nuggets/version.rb
CHANGED