djc 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/djc.rb +66 -22
- metadata +2 -2
data/lib/djc.rb
CHANGED
@@ -12,10 +12,6 @@ module DJC
|
|
12
12
|
|
13
13
|
class ::Array
|
14
14
|
|
15
|
-
def valfor(obj)
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
15
|
def walk(obj)
|
20
16
|
path = self.dup
|
21
17
|
key = path.shift.to_s
|
@@ -31,16 +27,16 @@ module DJC
|
|
31
27
|
path.clear
|
32
28
|
sub
|
33
29
|
end
|
34
|
-
elsif /^[\d
|
35
|
-
selected =
|
36
|
-
|
37
|
-
|
38
|
-
range =
|
39
|
-
|
40
|
-
else
|
41
|
-
obj[dex.to_i]
|
30
|
+
elsif /^[\d,\+-\.]+$/.match(key)
|
31
|
+
selected = nil
|
32
|
+
key.split('|').each do |option|
|
33
|
+
locators = option.split(',').map do |dex|
|
34
|
+
range = /(?<start>\d+)(?:-|\+|\.\.(?<exclusive>\.)?)?(?<end>-?\d+)/.match(dex)
|
35
|
+
range ? Range.new(range['start'].to_i, (range['end'] || -1).to_i, range['exclusive']) : dex.to_i
|
42
36
|
end
|
43
|
-
|
37
|
+
selected = obj.values_at(*locators)
|
38
|
+
break if selected
|
39
|
+
end
|
44
40
|
selected.size == 1 ? selected.first : selected
|
45
41
|
end
|
46
42
|
elsif obj.is_a? Hash
|
@@ -61,6 +57,28 @@ module DJC
|
|
61
57
|
path.empty? ? val : path.walk(val)
|
62
58
|
end
|
63
59
|
|
60
|
+
def collate
|
61
|
+
collated = [[]]
|
62
|
+
fill = {}
|
63
|
+
each_with_index do |obj, index|
|
64
|
+
if obj.is_a?(Array)
|
65
|
+
obj.each_with_index do |item, row|
|
66
|
+
collated[row] ||= []
|
67
|
+
collated[row][index] = item
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
collated.each do |row|
|
72
|
+
each_with_index do |obj, index|
|
73
|
+
unless obj.is_a?(Array)
|
74
|
+
row[index] = obj
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
collated.size == 1 ? collated.first : collated
|
79
|
+
end
|
80
|
+
|
81
|
+
|
64
82
|
def cross
|
65
83
|
crossed = [[]]
|
66
84
|
|
@@ -85,8 +103,13 @@ module DJC
|
|
85
103
|
|
86
104
|
class Rule
|
87
105
|
def parse(paths)
|
88
|
-
|
89
|
-
|
106
|
+
|
107
|
+
regex = /\/[^\/]+\//
|
108
|
+
lookup = /\<[^\<]\>/
|
109
|
+
indexes = /(?:-?\d+(?:(?:\.\.\.?|-|\+)(?:-?\d+)?)?,?)+/
|
110
|
+
node = /[^\[\]\{\}\|\.]+/
|
111
|
+
rules = paths.split('||').map do |path|
|
112
|
+
path.scan(/#{regex}|#{lookup}|#{indexes}|#{node}/)
|
90
113
|
end
|
91
114
|
|
92
115
|
rules
|
@@ -101,6 +124,10 @@ module DJC
|
|
101
124
|
end
|
102
125
|
end
|
103
126
|
|
127
|
+
def to_s
|
128
|
+
"#{paths}:#{type}:#{block}"
|
129
|
+
end
|
130
|
+
|
104
131
|
def sum
|
105
132
|
@type, @block = 'sum', proc { |array| array.map(&:to_i).inject(0, :+) if array }
|
106
133
|
self
|
@@ -121,8 +148,27 @@ module DJC
|
|
121
148
|
self
|
122
149
|
end
|
123
150
|
|
151
|
+
def sort(&sort_block)
|
152
|
+
@type, @block = 'sort', proc { |sort| sort.is_a?(Array) ? sort.compact.sort(&sort_block) : sort.sort(&sort_block) }
|
153
|
+
self
|
154
|
+
end
|
155
|
+
|
124
156
|
def match(matcher)
|
125
|
-
@type, @block = 'match', proc
|
157
|
+
@type, @block = 'match', proc do |val|
|
158
|
+
if val
|
159
|
+
if val.is_a?(Array)
|
160
|
+
val.map do |v|
|
161
|
+
if v
|
162
|
+
match = v.scan(matcher).flatten
|
163
|
+
match.size == 1 ? match.first : match
|
164
|
+
end
|
165
|
+
end
|
166
|
+
else
|
167
|
+
match = val.scan(matcher).flatten
|
168
|
+
match.size == 1 ? match.first : match
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
126
172
|
self
|
127
173
|
end
|
128
174
|
|
@@ -218,12 +264,13 @@ module DJC
|
|
218
264
|
rows = []
|
219
265
|
if json.is_a? Array
|
220
266
|
json.each do |row|
|
221
|
-
|
267
|
+
row = @columns.map do |column|
|
222
268
|
column.rule.apply(row)
|
223
269
|
end
|
270
|
+
rows << row
|
224
271
|
end
|
225
272
|
else
|
226
|
-
rows
|
273
|
+
rows << @columns.map do |column|
|
227
274
|
column.rule.apply(json)
|
228
275
|
end
|
229
276
|
end
|
@@ -241,15 +288,12 @@ module DJC
|
|
241
288
|
|
242
289
|
out = CSV.generate do |csv|
|
243
290
|
csv << builder.header
|
244
|
-
builder.build(json).
|
291
|
+
builder.build(json).each do |row|
|
245
292
|
csv << row
|
246
293
|
end
|
247
294
|
end
|
248
295
|
out
|
249
296
|
end
|
250
|
-
|
251
297
|
end
|
252
298
|
|
253
|
-
|
254
|
-
|
255
299
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: djc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-10-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|