sycsvpro 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/bin/sycsvpro +7 -2
- data/html/Object.html +1 -1
- data/html/Sycsvpro/Allocator.html +1 -1
- data/html/Sycsvpro/Calculator.html +19 -4
- data/html/created.rid +5 -5
- data/lib/sycsvpro/allocator.rb +1 -1
- data/lib/sycsvpro/calculator.rb +17 -3
- data/lib/sycsvpro/version.rb +1 -1
- data/spec/sycsvpro/calculator_spec.rb +86 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7823aeea07dda43deb692fdf13a8f57559bbb917
|
4
|
+
data.tar.gz: 4e3da29ada80c6c4a5eb282facce537e4750f0e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 203400b5c9187269c3fe336f940f25427eecfd5fe25a56fbc3fc982f7c47d059a6aefc5eb74b5ab5769d5a0b318ebf2ee72a231db64127cb24ca767ee8f5915f
|
7
|
+
data.tar.gz: deb1cfff428e6e83f1770a2f67a7b4e6fcf5cc642ce6c96cd50ba748ecfb8e44aaa39b53b88bc08083e434857d2ab778a7d086f1e61d9de3b3d7c1568a9ce343
|
data/Gemfile.lock
CHANGED
data/bin/sycsvpro
CHANGED
@@ -342,7 +342,12 @@ command :calc do |c|
|
|
342
342
|
|
343
343
|
c.desc 'Column to do calculations on'
|
344
344
|
c.arg_name 'COL1:*2,COL2:-C3,COL3:*2+(4+C5),COL6:NEW_COL=C1+5'
|
345
|
-
c.flag [:c, :col], :must_match => /\d+:(?:[\*\/\+\-]|\w+=[\d|(]*)[\*\/\+\-\dc()]*(?:,\d+:(?:[\*\/\+\-]|\w+=[\d|(]*)[\*\/\+\-\dc()]*)*/
|
345
|
+
#c.flag [:c, :col], :must_match => /\d+:(?:[\*\/\+\-]|\w+=[\d|(]*)[\*\/\+\-\dc()]*(?:,\d+:(?:[\*\/\+\-]|\w+=[\d|(]*)[\*\/\+\-\dc()]*)*/
|
346
|
+
c.flag [:c, :col], :must_match => /\d+:(?:[\*\+\-\/\d\w=\[\],\.:()]*)/
|
347
|
+
|
348
|
+
c.desc 'Date format of date columns'
|
349
|
+
c.arg_name '%d.%m.%Y|%Y-%m-%d|...'
|
350
|
+
c.flag [:df]
|
346
351
|
|
347
352
|
c.desc 'Indicate to add a sum row'
|
348
353
|
c.switch [:s, :sum]
|
@@ -353,7 +358,7 @@ command :calc do |c|
|
|
353
358
|
print "Calculating..."
|
354
359
|
calculator = Sycsvpro::Calculator.new(infile: global_options[:f], outfile: global_options[:o],
|
355
360
|
header: options[:h], rows: options[:r], cols: options[:c],
|
356
|
-
sum: options[:s])
|
361
|
+
sum: options[:s], df: options[:df])
|
357
362
|
calculator.execute
|
358
363
|
puts "done"
|
359
364
|
end
|
data/html/Object.html
CHANGED
@@ -154,7 +154,7 @@ of the input file</p>
|
|
154
154
|
|
155
155
|
|
156
156
|
<div class="method-source-code" id="set_max_row-source">
|
157
|
-
<pre><span class="ruby-comment"># File bin/sycsvpro, line
|
157
|
+
<pre><span class="ruby-comment"># File bin/sycsvpro, line 459</span>
|
158
158
|
<span class="ruby-keyword">def</span> <span class="ruby-identifier">set_max_row</span>(<span class="ruby-identifier">options</span>, <span class="ruby-identifier">max_row</span>)
|
159
159
|
<span class="ruby-identifier">options</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">option</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
|
160
160
|
<span class="ruby-keyword">case</span> <span class="ruby-identifier">option</span>
|
@@ -257,7 +257,7 @@ to allocate to key</p>
|
|
257
257
|
<span class="ruby-identifier">allocation</span> = {}
|
258
258
|
<span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">infile</span>).<span class="ruby-identifier">each_with_index</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">line</span>, <span class="ruby-identifier">index</span><span class="ruby-operator">|</span>
|
259
259
|
<span class="ruby-identifier">row</span> = <span class="ruby-identifier">row_filter</span>.<span class="ruby-identifier">process</span>(<span class="ruby-identifier">line</span>, <span class="ruby-identifier">row</span><span class="ruby-operator">:</span> <span class="ruby-identifier">index</span>)
|
260
|
-
<span class="ruby-keyword">next</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">row</span>.<span class="ruby-identifier">nil?</span>
|
260
|
+
<span class="ruby-keyword">next</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">row</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-keyword">or</span> <span class="ruby-identifier">row</span>.<span class="ruby-identifier">empty?</span>
|
261
261
|
<span class="ruby-identifier">key</span> = <span class="ruby-identifier">key_filter</span>.<span class="ruby-identifier">process</span>(<span class="ruby-identifier">row</span>)
|
262
262
|
<span class="ruby-identifier">allocation</span>[<span class="ruby-identifier">key</span>] = [] <span class="ruby-keyword">if</span> <span class="ruby-identifier">allocation</span>[<span class="ruby-identifier">key</span>].<span class="ruby-identifier">nil?</span>
|
263
263
|
<span class="ruby-identifier">allocation</span>[<span class="ruby-identifier">key</span>] <span class="ruby-operator"><<</span> <span class="ruby-identifier">col_filter</span>.<span class="ruby-identifier">process</span>(<span class="ruby-identifier">row</span>).<span class="ruby-identifier">split</span>(<span class="ruby-string">';'</span>)
|
@@ -153,6 +153,19 @@ the value of column 1 with 2.</p>
|
|
153
153
|
</div>
|
154
154
|
</div>
|
155
155
|
|
156
|
+
<div id="attribute-i-date_format" class="method-detail">
|
157
|
+
<div class="method-heading attribute-method-heading">
|
158
|
+
<span class="method-name">date_format</span><span
|
159
|
+
class="attribute-access-type">[R]</span>
|
160
|
+
</div>
|
161
|
+
|
162
|
+
<div class="method-description">
|
163
|
+
|
164
|
+
<p>date format for date operations</p>
|
165
|
+
|
166
|
+
</div>
|
167
|
+
</div>
|
168
|
+
|
156
169
|
<div id="attribute-i-formulae" class="method-detail">
|
157
170
|
<div class="method-heading attribute-method-heading">
|
158
171
|
<span class="method-name">formulae</span><span
|
@@ -250,10 +263,11 @@ generated due to a arithmetic operation that creates new columns</p>
|
|
250
263
|
|
251
264
|
|
252
265
|
<div class="method-source-code" id="new-source">
|
253
|
-
<pre><span class="ruby-comment"># File lib/sycsvpro/calculator.rb, line
|
266
|
+
<pre><span class="ruby-comment"># File lib/sycsvpro/calculator.rb, line 36</span>
|
254
267
|
<span class="ruby-keyword">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">options</span>={})
|
255
268
|
<span class="ruby-ivar">@infile</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:infile</span>]
|
256
269
|
<span class="ruby-ivar">@outfile</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:outfile</span>]
|
270
|
+
<span class="ruby-ivar">@date_format</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:df</span>] <span class="ruby-operator">||</span> <span class="ruby-string">"%Y-%m-%d"</span>
|
257
271
|
<span class="ruby-ivar">@row_filter</span> = <span class="ruby-constant">RowFilter</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:rows</span>])
|
258
272
|
<span class="ruby-ivar">@header</span> = <span class="ruby-constant">Header</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:header</span>])
|
259
273
|
<span class="ruby-ivar">@sum_row</span> = []
|
@@ -298,7 +312,7 @@ generated due to a arithmetic operation that creates new columns</p>
|
|
298
312
|
|
299
313
|
|
300
314
|
<div class="method-source-code" id="execute-source">
|
301
|
-
<pre><span class="ruby-comment"># File lib/sycsvpro/calculator.rb, line
|
315
|
+
<pre><span class="ruby-comment"># File lib/sycsvpro/calculator.rb, line 55</span>
|
302
316
|
<span class="ruby-keyword">def</span> <span class="ruby-identifier">execute</span>
|
303
317
|
<span class="ruby-identifier">processed_header</span> = <span class="ruby-keyword">false</span>
|
304
318
|
|
@@ -364,9 +378,10 @@ generated due to a arithmetic operation that creates new columns</p>
|
|
364
378
|
|
365
379
|
|
366
380
|
<div class="method-source-code" id="method_missing-source">
|
367
|
-
<pre><span class="ruby-comment"># File lib/sycsvpro/calculator.rb, line
|
381
|
+
<pre><span class="ruby-comment"># File lib/sycsvpro/calculator.rb, line 49</span>
|
368
382
|
<span class="ruby-keyword">def</span> <span class="ruby-identifier">method_missing</span>(<span class="ruby-identifier">id</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>)
|
369
|
-
<span class="ruby-identifier">to_number</span>(<span class="ruby-identifier">columns</span>[<span class="ruby-node">$1</span>.<span class="ruby-identifier">to_i</span>]) <span class="ruby-keyword">if</span> <span class="ruby-identifier">id</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp">/c(\d+)/</span>
|
383
|
+
<span class="ruby-keyword">return</span> <span class="ruby-identifier">to_number</span>(<span class="ruby-identifier">columns</span>[<span class="ruby-node">$1</span>.<span class="ruby-identifier">to_i</span>]) <span class="ruby-keyword">if</span> <span class="ruby-identifier">id</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp">/c(\d+)/</span>
|
384
|
+
<span class="ruby-keyword">return</span> <span class="ruby-identifier">to_date</span>(<span class="ruby-identifier">columns</span>[<span class="ruby-node">$1</span>.<span class="ruby-identifier">to_i</span>]) <span class="ruby-keyword">if</span> <span class="ruby-identifier">id</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp">/d(\d+)/</span>
|
370
385
|
<span class="ruby-keyword">end</span></pre>
|
371
386
|
</div>
|
372
387
|
|
data/html/created.rid
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
|
1
|
+
Wed, 12 Mar 2014 23:51:45 +0100
|
2
2
|
README.rdoc Thu, 06 Mar 2014 18:59:37 +0100
|
3
3
|
lib/sycsvpro.rb Tue, 04 Mar 2014 21:03:44 +0100
|
4
4
|
lib/sycsvpro/aggregator.rb Tue, 04 Mar 2014 21:40:52 +0100
|
5
|
-
lib/sycsvpro/allocator.rb
|
5
|
+
lib/sycsvpro/allocator.rb Fri, 07 Mar 2014 22:30:55 +0100
|
6
6
|
lib/sycsvpro/analyzer.rb Tue, 18 Feb 2014 19:57:28 +0100
|
7
|
-
lib/sycsvpro/calculator.rb
|
7
|
+
lib/sycsvpro/calculator.rb Wed, 12 Mar 2014 23:47:35 +0100
|
8
8
|
lib/sycsvpro/collector.rb Sun, 16 Feb 2014 20:59:27 +0100
|
9
9
|
lib/sycsvpro/column_filter.rb Fri, 28 Feb 2014 08:24:47 +0100
|
10
10
|
lib/sycsvpro/column_type_filter.rb Fri, 28 Feb 2014 18:17:46 +0100
|
@@ -20,5 +20,5 @@ lib/sycsvpro/row_filter.rb Tue, 18 Feb 2014 19:45:25 +0100
|
|
20
20
|
lib/sycsvpro/script_creator.rb Sun, 23 Feb 2014 20:35:21 +0100
|
21
21
|
lib/sycsvpro/script_list.rb Sun, 23 Feb 2014 21:03:22 +0100
|
22
22
|
lib/sycsvpro/sorter.rb Sun, 02 Mar 2014 21:07:30 +0100
|
23
|
-
lib/sycsvpro/version.rb
|
24
|
-
bin/sycsvpro
|
23
|
+
lib/sycsvpro/version.rb Wed, 12 Mar 2014 21:37:56 +0100
|
24
|
+
bin/sycsvpro Wed, 12 Mar 2014 23:41:41 +0100
|
data/lib/sycsvpro/allocator.rb
CHANGED
@@ -29,7 +29,7 @@ module Sycsvpro
|
|
29
29
|
allocation = {}
|
30
30
|
File.open(infile).each_with_index do |line, index|
|
31
31
|
row = row_filter.process(line, row: index)
|
32
|
-
next if row.nil?
|
32
|
+
next if row.nil? or row.empty?
|
33
33
|
key = key_filter.process(row)
|
34
34
|
allocation[key] = [] if allocation[key].nil?
|
35
35
|
allocation[key] << col_filter.process(row).split(';')
|
data/lib/sycsvpro/calculator.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require_relative 'row_filter'
|
2
2
|
require_relative 'header'
|
3
3
|
require_relative 'dsl'
|
4
|
+
require 'date'
|
4
5
|
|
5
6
|
# Operating csv files
|
6
7
|
module Sycsvpro
|
@@ -18,6 +19,8 @@ module Sycsvpro
|
|
18
19
|
attr_reader :outfile
|
19
20
|
# filter that is used for rows
|
20
21
|
attr_reader :row_filter
|
22
|
+
# date format for date operations
|
23
|
+
attr_reader :date_format
|
21
24
|
# the operations on columns
|
22
25
|
attr_reader :formulae
|
23
26
|
# header of the outfile
|
@@ -33,6 +36,7 @@ module Sycsvpro
|
|
33
36
|
def initialize(options={})
|
34
37
|
@infile = options[:infile]
|
35
38
|
@outfile = options[:outfile]
|
39
|
+
@date_format = options[:df] || "%Y-%m-%d"
|
36
40
|
@row_filter = RowFilter.new(options[:rows])
|
37
41
|
@header = Header.new(options[:header])
|
38
42
|
@sum_row = []
|
@@ -43,7 +47,8 @@ module Sycsvpro
|
|
43
47
|
|
44
48
|
# Retrieves the values from a row as the result of a arithmetic operation
|
45
49
|
def method_missing(id, *args, &block)
|
46
|
-
to_number(columns[$1.to_i]) if id =~ /c(\d+)/
|
50
|
+
return to_number(columns[$1.to_i]) if id =~ /c(\d+)/
|
51
|
+
return to_date(columns[$1.to_i]) if id =~ /d(\d+)/
|
47
52
|
end
|
48
53
|
|
49
54
|
# Executes the calculator
|
@@ -95,9 +100,9 @@ module Sycsvpro
|
|
95
100
|
# 4:c1+1 means create a new column and assign to it the result of the sum of the value of
|
96
101
|
# column 1 + 1 c[4] = c[1] + 1
|
97
102
|
def create_calculator(code)
|
98
|
-
code.split(
|
103
|
+
code.split(/,(?=\d+:)/).each do |operation|
|
99
104
|
col, term = operation.split(':')
|
100
|
-
term = "c#{col}#{term}" unless term =~ /^c\d
|
105
|
+
term = "c#{col}#{term}" unless term =~ /^c\d+|^\[/
|
101
106
|
formulae[col] = term
|
102
107
|
end
|
103
108
|
end
|
@@ -108,6 +113,15 @@ module Sycsvpro
|
|
108
113
|
return value.to_f if value =~ /\./
|
109
114
|
end
|
110
115
|
|
116
|
+
# Casts a string to a date
|
117
|
+
def to_date(value)
|
118
|
+
if value.nil? or value.strip.empty?
|
119
|
+
nil
|
120
|
+
else
|
121
|
+
Date.strptime(value, date_format)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
111
125
|
end
|
112
126
|
|
113
127
|
end
|
data/lib/sycsvpro/version.rb
CHANGED
@@ -6,6 +6,8 @@ module Sycsvpro
|
|
6
6
|
|
7
7
|
before do
|
8
8
|
@in_file = File.join(File.dirname(__FILE__), "files/machines.csv")
|
9
|
+
@in_date_file = File.join(File.dirname(__FILE__), "files/machine-delivery.csv")
|
10
|
+
@in_number_file = File.join(File.dirname(__FILE__), "files/machine-count.csv")
|
9
11
|
@out_file = File.join(File.dirname(__FILE__), "files/machines_out.csv")
|
10
12
|
end
|
11
13
|
|
@@ -62,6 +64,90 @@ module Sycsvpro
|
|
62
64
|
end
|
63
65
|
end
|
64
66
|
|
67
|
+
it "should find maximum of specified date rows" do
|
68
|
+
header = "*,Max Date"
|
69
|
+
cols = "3:[d1,d2].compact.max"
|
70
|
+
rows = "1-8"
|
71
|
+
df = "%d.%m.%Y"
|
72
|
+
|
73
|
+
calculator = Calculator.new(infile: @in_date_file, outfile: @out_file,
|
74
|
+
header: header, rows: rows, cols: cols, df: df)
|
75
|
+
calculator.execute
|
76
|
+
|
77
|
+
result = ["customer;delivery;registration;Max Date",
|
78
|
+
"Fink;1.10.2014;30.9.2013;2014-10-01",
|
79
|
+
"Haas;3.3.2012;10.10.2013;2013-10-10",
|
80
|
+
"Gent;8.5.1995;11.2.1999;1999-02-11",
|
81
|
+
"Rank;;1.3.2002;2002-03-01" ]
|
82
|
+
|
83
|
+
File.new(@out_file, 'r').each_with_index do |line, index|
|
84
|
+
expect(line.chomp).to eq result[index]
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should find minimum of specified date rows" do
|
89
|
+
header = "*,Min_Date"
|
90
|
+
cols = "3:Min_Date=[d1,d2].compact.min"
|
91
|
+
rows = "1-8"
|
92
|
+
df = "%d.%m.%Y"
|
93
|
+
|
94
|
+
calculator = Calculator.new(infile: @in_date_file, outfile: @out_file,
|
95
|
+
header: header, rows: rows, cols: cols, df: df)
|
96
|
+
calculator.execute
|
97
|
+
|
98
|
+
result = ["customer;delivery;registration;Min_Date",
|
99
|
+
"Fink;1.10.2014;30.9.2013;2013-09-30",
|
100
|
+
"Haas;3.3.2012;10.10.2013;2012-03-03",
|
101
|
+
"Gent;8.5.1995;11.2.1999;1995-05-08",
|
102
|
+
"Rank;;1.3.2002;2002-03-01" ]
|
103
|
+
|
104
|
+
File.new(@out_file, 'r').each_with_index do |line, index|
|
105
|
+
expect(line.chomp).to eq result[index]
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should find maximum of specified number rows" do
|
110
|
+
header = "*,Max Number"
|
111
|
+
cols = "4:[c1,c2,c3].max"
|
112
|
+
rows = "1-8"
|
113
|
+
df = "%d.%m.%Y"
|
114
|
+
|
115
|
+
calculator = Calculator.new(infile: @in_number_file, outfile: @out_file,
|
116
|
+
header: header, rows: rows, cols: cols, df: df)
|
117
|
+
calculator.execute
|
118
|
+
|
119
|
+
result = ["customer;before;between;after;Max Number",
|
120
|
+
"Fink;2;3;1;3",
|
121
|
+
"Haas;3;1;6;6",
|
122
|
+
"Gent;4;4;4;4",
|
123
|
+
"Rank;5;4;1;5"]
|
124
|
+
|
125
|
+
File.new(@out_file, 'r').each_with_index do |line, index|
|
126
|
+
expect(line.chomp).to eq result[index]
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should find minimum of specified number rows" do
|
131
|
+
header = "*,Min Number"
|
132
|
+
cols = "4:[c1,c2,c3].min"
|
133
|
+
rows = "1-8"
|
134
|
+
df = "%d.%m.%Y"
|
135
|
+
|
136
|
+
calculator = Calculator.new(infile: @in_number_file, outfile: @out_file,
|
137
|
+
header: header, rows: rows, cols: cols, df: df)
|
138
|
+
calculator.execute
|
139
|
+
|
140
|
+
result = ["customer;before;between;after;Min Number",
|
141
|
+
"Fink;2;3;1;1",
|
142
|
+
"Haas;3;1;6;1",
|
143
|
+
"Gent;4;4;4;4",
|
144
|
+
"Rank;5;4;1;1"]
|
145
|
+
|
146
|
+
File.new(@out_file, 'r').each_with_index do |line, index|
|
147
|
+
expect(line.chomp).to eq result[index]
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
65
151
|
end
|
66
152
|
|
67
153
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sycsvpro
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pierre Sugar
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|