masticate 0.4.2 → 0.5.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/masticate.rb +7 -17
- data/lib/masticate/include.rb +43 -0
- data/lib/masticate/myoptparse.rb +4 -0
- data/lib/masticate/version.rb +1 -1
- data/spec/data/include_input.csv +6 -0
- data/spec/data/include_results.csv +3 -0
- data/spec/lib/include_spec.rb +15 -0
- metadata +11 -4
data/lib/masticate.rb
CHANGED
@@ -1,23 +1,9 @@
|
|
1
1
|
require "open-uri"
|
2
2
|
require "csv"
|
3
3
|
|
4
|
-
|
5
|
-
require_relative "masticate
|
6
|
-
|
7
|
-
|
8
|
-
require_relative "masticate/sniffer"
|
9
|
-
require_relative "masticate/mender"
|
10
|
-
require_relative "masticate/csvify"
|
11
|
-
require_relative "masticate/plucker"
|
12
|
-
require_relative "masticate/datify"
|
13
|
-
require_relative "masticate/gsubber"
|
14
|
-
require_relative "masticate/max_rows"
|
15
|
-
require_relative "masticate/concat"
|
16
|
-
require_relative "masticate/relabel"
|
17
|
-
require_relative "masticate/exclude"
|
18
|
-
require_relative "masticate/transform"
|
19
|
-
|
20
|
-
require_relative "masticate/cook"
|
4
|
+
%w{version base myoptparse sniffer mender csvify plucker datify gsubber max_rows concat relabel exclude transform include cook}.each do |f|
|
5
|
+
require_relative "masticate/#{f}"
|
6
|
+
end
|
21
7
|
|
22
8
|
module Masticate
|
23
9
|
def self.sniff(filename, opts = {})
|
@@ -60,6 +46,10 @@ module Masticate
|
|
60
46
|
Exclude.new(filename).exclude(opts)
|
61
47
|
end
|
62
48
|
|
49
|
+
def self.include(filename, opts)
|
50
|
+
Include.new(filename).exclude(opts)
|
51
|
+
end
|
52
|
+
|
63
53
|
def self.cook(filename, opts)
|
64
54
|
Cook.new(filename).cook(opts)
|
65
55
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# exclude rows based on field = value
|
2
|
+
|
3
|
+
class Masticate::Include < Masticate::Base
|
4
|
+
def configure(opts)
|
5
|
+
standard_options(opts)
|
6
|
+
|
7
|
+
@field = opts[:field] or raise "missing field to include"
|
8
|
+
@value = opts[:value] or raise "missing value to include"
|
9
|
+
|
10
|
+
# row-loading automatically strips leading & trailing whitespace and converts blanks to nils,
|
11
|
+
# so when looking for blanks need to compare to nil instead of ''
|
12
|
+
@value = nil if @value.empty?
|
13
|
+
end
|
14
|
+
|
15
|
+
def exclude(opts)
|
16
|
+
execute(opts)
|
17
|
+
end
|
18
|
+
|
19
|
+
def crunch(row)
|
20
|
+
if !@headers
|
21
|
+
@headers = row
|
22
|
+
f = @field
|
23
|
+
@index =
|
24
|
+
case f
|
25
|
+
when Fixnum, /^\d+$/
|
26
|
+
f = f.to_i
|
27
|
+
if f > row.count
|
28
|
+
raise "Cannot pluck column #{f}, there are only #{row.count} fields"
|
29
|
+
else
|
30
|
+
f-1
|
31
|
+
end
|
32
|
+
else
|
33
|
+
row.index(f) or raise "Unable to find column '#{f}' in headers"
|
34
|
+
end
|
35
|
+
row
|
36
|
+
elsif row
|
37
|
+
if row[@index] == @value
|
38
|
+
# include only these matches
|
39
|
+
row
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/masticate/myoptparse.rb
CHANGED
@@ -165,6 +165,10 @@ EOT
|
|
165
165
|
results = Masticate.exclude(filename, options)
|
166
166
|
logmessage(command, options, results)
|
167
167
|
|
168
|
+
when 'include'
|
169
|
+
results = Masticate.include(filename, options)
|
170
|
+
logmessage(command, options, results)
|
171
|
+
|
168
172
|
when 'transform'
|
169
173
|
results = Masticate.transform(filename, options)
|
170
174
|
logmessage(command, options, results)
|
data/lib/masticate/version.rb
CHANGED
@@ -0,0 +1,15 @@
|
|
1
|
+
# spec for row inclusion
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe "include" do
|
6
|
+
it "should be pick only rows that match criterion" do
|
7
|
+
filename = File.dirname(__FILE__) + "/../data/include_input.csv"
|
8
|
+
tmp = Tempfile.new('include')
|
9
|
+
results = Masticate.include(filename, :output => tmp, :field => 'CRITERION', :value => 'yes')
|
10
|
+
output = File.read(tmp)
|
11
|
+
correct_output = File.read(File.dirname(__FILE__) + "/../data/include_results.csv")
|
12
|
+
|
13
|
+
output.should == correct_output
|
14
|
+
end
|
15
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: masticate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.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-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -99,6 +99,7 @@ files:
|
|
99
99
|
- lib/masticate/datify.rb
|
100
100
|
- lib/masticate/exclude.rb
|
101
101
|
- lib/masticate/gsubber.rb
|
102
|
+
- lib/masticate/include.rb
|
102
103
|
- lib/masticate/max_rows.rb
|
103
104
|
- lib/masticate/mender.rb
|
104
105
|
- lib/masticate/myoptparse.rb
|
@@ -125,6 +126,8 @@ files:
|
|
125
126
|
- spec/data/events_reduced.csv
|
126
127
|
- spec/data/exclude_input.csv
|
127
128
|
- spec/data/exclude_results.csv
|
129
|
+
- spec/data/include_input.csv
|
130
|
+
- spec/data/include_results.csv
|
128
131
|
- spec/data/inlined_headers.csv
|
129
132
|
- spec/data/inlined_headers.csv.output
|
130
133
|
- spec/data/junk_header.csv
|
@@ -147,6 +150,7 @@ files:
|
|
147
150
|
- spec/lib/datify_spec.rb
|
148
151
|
- spec/lib/exclude_spec.rb
|
149
152
|
- spec/lib/gsub_spec.rb
|
153
|
+
- spec/lib/include_spec.rb
|
150
154
|
- spec/lib/maxrow_spec.rb
|
151
155
|
- spec/lib/mender_spec.rb
|
152
156
|
- spec/lib/plucker_spec.rb
|
@@ -168,7 +172,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
168
172
|
version: '0'
|
169
173
|
segments:
|
170
174
|
- 0
|
171
|
-
hash: -
|
175
|
+
hash: -1109871130254575222
|
172
176
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
173
177
|
none: false
|
174
178
|
requirements:
|
@@ -177,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
177
181
|
version: '0'
|
178
182
|
segments:
|
179
183
|
- 0
|
180
|
-
hash: -
|
184
|
+
hash: -1109871130254575222
|
181
185
|
requirements: []
|
182
186
|
rubyforge_project: masticate
|
183
187
|
rubygems_version: 1.8.24
|
@@ -202,6 +206,8 @@ test_files:
|
|
202
206
|
- spec/data/events_reduced.csv
|
203
207
|
- spec/data/exclude_input.csv
|
204
208
|
- spec/data/exclude_results.csv
|
209
|
+
- spec/data/include_input.csv
|
210
|
+
- spec/data/include_results.csv
|
205
211
|
- spec/data/inlined_headers.csv
|
206
212
|
- spec/data/inlined_headers.csv.output
|
207
213
|
- spec/data/junk_header.csv
|
@@ -224,6 +230,7 @@ test_files:
|
|
224
230
|
- spec/lib/datify_spec.rb
|
225
231
|
- spec/lib/exclude_spec.rb
|
226
232
|
- spec/lib/gsub_spec.rb
|
233
|
+
- spec/lib/include_spec.rb
|
227
234
|
- spec/lib/maxrow_spec.rb
|
228
235
|
- spec/lib/mender_spec.rb
|
229
236
|
- spec/lib/plucker_spec.rb
|