djc 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/djc.rb +23 -8
- metadata +1 -1
data/lib/djc.rb
CHANGED
@@ -11,12 +11,27 @@ module DJC
|
|
11
11
|
end
|
12
12
|
|
13
13
|
class ::Array
|
14
|
+
|
15
|
+
def valfor(obj)
|
16
|
+
|
17
|
+
end
|
18
|
+
|
14
19
|
def walk(obj)
|
15
20
|
path = self.dup
|
16
21
|
key = path.shift.to_s
|
22
|
+
|
17
23
|
val = if obj.is_a? Array
|
18
|
-
|
19
|
-
|
24
|
+
if key == '*'
|
25
|
+
if path.empty?
|
26
|
+
obj
|
27
|
+
else
|
28
|
+
sub = obj.map do |inner|
|
29
|
+
path.dup.walk(inner)
|
30
|
+
end
|
31
|
+
path.clear
|
32
|
+
sub
|
33
|
+
end
|
34
|
+
elsif /^[\d,-]+$/.match(key)
|
20
35
|
selected = key.split(',').map do |dex|
|
21
36
|
range = /(\d+)(?:-|\.\.\.?)(\d+)/.match(dex)
|
22
37
|
if range
|
@@ -42,6 +57,7 @@ module DJC
|
|
42
57
|
elsif obj.respond_to? key
|
43
58
|
obj.send(key)
|
44
59
|
end
|
60
|
+
|
45
61
|
path.empty? ? val : path.walk(val)
|
46
62
|
end
|
47
63
|
|
@@ -86,17 +102,17 @@ module DJC
|
|
86
102
|
end
|
87
103
|
|
88
104
|
def sum
|
89
|
-
@type, @block = 'sum', proc { |array| array.map(&:to_i).inject(0, :+) }
|
105
|
+
@type, @block = 'sum', proc { |array| array.map(&:to_i).inject(0, :+) if array }
|
90
106
|
self
|
91
107
|
end
|
92
108
|
|
93
109
|
def avg
|
94
|
-
@type, @block = 'avg', proc { |array| array.map(&:to_i).inject(0.0, :+) / array.size }
|
110
|
+
@type, @block = 'avg', proc { |array| ( array.map(&:to_i).inject(0.0, :+) / array.size) if array }
|
95
111
|
self
|
96
112
|
end
|
97
113
|
|
98
114
|
def each(&each_block)
|
99
|
-
@type, @block = 'each', proc { |array| array.map { |val| each_block.call(val) } }
|
115
|
+
@type, @block = 'each', proc { |array| array.map { |val| each_block.call(val) } if array }
|
100
116
|
self
|
101
117
|
end
|
102
118
|
|
@@ -202,13 +218,12 @@ module DJC
|
|
202
218
|
rows = []
|
203
219
|
if json.is_a? Array
|
204
220
|
json.each do |row|
|
205
|
-
#xxx ensmartern for arrayed vals, x-by-x
|
206
221
|
rows << @columns.map do |column|
|
207
222
|
column.rule.apply(row)
|
208
223
|
end
|
209
224
|
end
|
210
225
|
else
|
211
|
-
rows
|
226
|
+
rows = @columns.map do |column|
|
212
227
|
column.rule.apply(json)
|
213
228
|
end
|
214
229
|
end
|
@@ -226,7 +241,7 @@ module DJC
|
|
226
241
|
|
227
242
|
out = CSV.generate do |csv|
|
228
243
|
csv << builder.header
|
229
|
-
builder.build(json).each do |row|
|
244
|
+
builder.build(json).cross.each do |row|
|
230
245
|
csv << row
|
231
246
|
end
|
232
247
|
end
|