benschwarz-smoke 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +1 -1
- data/lib/smoke/origin.rb +24 -21
- data/spec/smoke/origin_spec.rb +56 -68
- metadata +2 -2
data/VERSION.yml
CHANGED
data/lib/smoke/origin.rb
CHANGED
@@ -110,8 +110,6 @@ module Smoke
|
|
110
110
|
@items.each do |item|
|
111
111
|
item[key] = value
|
112
112
|
end
|
113
|
-
|
114
|
-
return self
|
115
113
|
end
|
116
114
|
|
117
115
|
# Prepare is used when you'd like to provision for
|
@@ -147,77 +145,82 @@ module Smoke
|
|
147
145
|
end
|
148
146
|
|
149
147
|
# Re-sort items by a particular key
|
148
|
+
# Sort must be used inside an `emit` block.
|
150
149
|
def sort(key)
|
151
150
|
@items = @items.sort_by{|i| i[key] }
|
152
151
|
rescue NoMethodError => e
|
153
152
|
Smoke.log.info "You're trying to sort by \"#{key}\" but it does not exist in your item set"
|
154
|
-
ensure
|
155
|
-
return self
|
156
153
|
end
|
157
154
|
|
158
155
|
# Reverse the order of the items
|
159
156
|
#
|
160
157
|
# Usage
|
161
|
-
# Smoke[:ruby].
|
158
|
+
# Smoke[:ruby].output
|
162
159
|
# Returns [{:header => "Platypus"}, {:header => "Kangaroo"}]
|
163
|
-
# Smoke
|
160
|
+
# Smoke.yql(:ruby) do
|
161
|
+
# ... Define your source
|
162
|
+
# emit do
|
163
|
+
# reverse
|
164
|
+
# end
|
165
|
+
# end
|
164
166
|
# Returns [{:header => "Kangaroo"}, {:header => "Platypus"}]
|
167
|
+
# Reverse must be used inside an `emit` block.
|
165
168
|
def reverse
|
166
169
|
@items.reverse!
|
167
|
-
return self
|
168
170
|
end
|
169
171
|
|
170
172
|
# Keep items that match the regex
|
171
173
|
#
|
172
|
-
# Usage (chained):
|
173
|
-
# Smoke[:ruby].keep(:title, /tuesday/i)
|
174
174
|
# Usage (block, during initialization):
|
175
175
|
# Smoke.yql(:ruby) do
|
176
176
|
# ...
|
177
|
-
#
|
177
|
+
# emit do
|
178
|
+
# keep(:title, /tuesday/i)
|
179
|
+
# end
|
178
180
|
# end
|
181
|
+
# Keep must be used inside an `emit` block.
|
179
182
|
def keep(key, matcher)
|
180
183
|
@items.reject! {|i| (i[key] =~ matcher) ? false : true }
|
181
|
-
return self
|
182
184
|
end
|
183
185
|
|
184
186
|
# Discard items that do not match the regex
|
185
187
|
#
|
186
|
-
# Usage (chained):
|
187
|
-
# Smoke[:ruby].discard(:title, /tuesday/i)
|
188
188
|
# Usage (block, during initialization):
|
189
189
|
# Smoke.yql(:ruby) do
|
190
190
|
# ...
|
191
|
-
#
|
191
|
+
# emit do
|
192
|
+
# discard(:title, /tuesday/i)
|
193
|
+
# end
|
192
194
|
# end
|
195
|
+
# Discard must be used inside an `emit` block.
|
193
196
|
def discard(key, matcher)
|
194
197
|
@items.reject! {|i| (i[key] =~ matcher) ? true : false }
|
195
|
-
return self
|
196
198
|
end
|
197
199
|
|
198
200
|
# Rename one or many keys at a time
|
199
|
-
# Suggested that you run it within an each block, but it makes no difference
|
200
|
-
# other than readability
|
201
201
|
#
|
202
202
|
# Usage
|
203
203
|
# # Renames all items with a key of href to link
|
204
204
|
# rename(:href => :link)
|
205
205
|
# or
|
206
206
|
# rename(:href => :link, :description => :excerpt)
|
207
|
+
# Rename must be used inside an `emit` block.
|
207
208
|
def rename(*args)
|
208
209
|
@items.each {|item| item.rename(*args) }
|
209
|
-
return self
|
210
210
|
end
|
211
211
|
|
212
212
|
# Truncate your result set to this many objects
|
213
213
|
#
|
214
214
|
# Usage
|
215
|
-
#
|
216
|
-
#
|
215
|
+
# Smoke.yql(:ruby) do
|
216
|
+
# ...
|
217
|
+
# truncate(3)
|
218
|
+
# end
|
219
|
+
# Smoke[:ruby].output
|
217
220
|
# => [{title => "Canon"}, {:title => "Nikon"}, {:title => "Pentax"}]
|
221
|
+
# Truncate must be used inside an `emit` block.
|
218
222
|
def truncate(length)
|
219
223
|
@items = @items[0..(length - 1)]
|
220
|
-
return self
|
221
224
|
end
|
222
225
|
|
223
226
|
private
|
data/spec/smoke/origin_spec.rb
CHANGED
@@ -6,6 +6,8 @@ describe Smoke::Origin do
|
|
6
6
|
emit do
|
7
7
|
rename(:head => :title)
|
8
8
|
sort(:title)
|
9
|
+
reverse
|
10
|
+
truncate 1
|
9
11
|
|
10
12
|
transform :title, :name do |title|
|
11
13
|
title.gsub(/Animal: /, '')
|
@@ -13,53 +15,47 @@ describe Smoke::Origin do
|
|
13
15
|
end
|
14
16
|
end
|
15
17
|
end
|
16
|
-
|
17
|
-
describe "after emit, object output" do
|
18
|
-
it "should not have a key of head" do
|
19
|
-
@origin.output.first.should_not have_key(:head)
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should be ordered by title" do
|
23
|
-
@origin.output.first[:title].should == "Kangaroo"
|
24
|
-
end
|
25
18
|
|
26
|
-
it "should output a single hash rather than a hash in an array when there is one item" do
|
27
|
-
Smoke[:test].truncate(1).output.should == [{:title => "Kangaroo", :name => "Kelly"}]
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
19
|
describe "transformations" do
|
32
|
-
it "should
|
33
|
-
Smoke[:test].
|
20
|
+
it "should have renamed properties" do
|
21
|
+
Smoke[:test].output.first.should have_key(:title)
|
34
22
|
end
|
35
23
|
|
36
24
|
it "should sort by a given property" do
|
37
|
-
Smoke[:test].
|
25
|
+
Smoke[:test].output.first[:title].should == "Platypus"
|
38
26
|
end
|
39
27
|
|
40
28
|
it "should reverse the results" do
|
41
|
-
Smoke[:test].
|
29
|
+
Smoke[:test].output.should == [{:title => "Platypus", :name => "Peter"}]
|
42
30
|
end
|
43
31
|
|
44
32
|
it "should truncate results given a length" do
|
45
|
-
Smoke[:test].
|
33
|
+
Smoke[:test].output.size.should == 1
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should output" do
|
37
|
+
Smoke[:test].output.should be_an_instance_of(Array)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should output json" do
|
41
|
+
Smoke[:test].output(:json).should =~ /^\[\{/
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should output yml" do
|
45
|
+
Smoke[:test].output(:yaml).should =~ /^--- \n- :/
|
46
46
|
end
|
47
47
|
|
48
48
|
describe "filtering" do
|
49
|
-
before do
|
49
|
+
before :all do
|
50
50
|
TestSource.source(:keep) do
|
51
51
|
emit do
|
52
|
-
|
53
|
-
head.gsub(/Animal: /, '')
|
54
|
-
end
|
52
|
+
keep(:head, /roo/)
|
55
53
|
end
|
56
54
|
end
|
57
55
|
|
58
56
|
TestSource.source(:discard) do
|
59
57
|
emit do
|
60
|
-
|
61
|
-
head.gsub(/Animal: /, '')
|
62
|
-
end
|
58
|
+
discard(:head, /roo/)
|
63
59
|
end
|
64
60
|
end
|
65
61
|
end
|
@@ -69,7 +65,7 @@ describe Smoke::Origin do
|
|
69
65
|
end
|
70
66
|
|
71
67
|
it "should only contain items that match" do
|
72
|
-
Smoke[:keep].
|
68
|
+
Smoke[:keep].output.should == [{:head => "Animal: Kangaroo", :name => "Kelly"}]
|
73
69
|
end
|
74
70
|
|
75
71
|
it "should discard items" do
|
@@ -77,28 +73,12 @@ describe Smoke::Origin do
|
|
77
73
|
end
|
78
74
|
|
79
75
|
it "should not contain items that match" do
|
80
|
-
Smoke[:discard].
|
76
|
+
Smoke[:discard].output.should == [{:head => "Animal: Platypus", :name => "Peter"}]
|
81
77
|
end
|
82
78
|
end
|
83
79
|
end
|
84
80
|
|
85
|
-
describe "
|
86
|
-
it "should output" do
|
87
|
-
@origin.output.should be_an_instance_of(Array)
|
88
|
-
end
|
89
|
-
|
90
|
-
it "should output two items" do
|
91
|
-
@origin.output.size.should == 2
|
92
|
-
end
|
93
|
-
|
94
|
-
it "should output json" do
|
95
|
-
@origin.output(:json).should =~ /^\[\{/
|
96
|
-
end
|
97
|
-
|
98
|
-
it "should output yml" do
|
99
|
-
@origin.output(:yaml).should =~ /^--- \n- :/
|
100
|
-
end
|
101
|
-
|
81
|
+
describe "dispatching" do
|
102
82
|
it "should dispatch when output is called" do
|
103
83
|
TestSource.source(:no_dispatch)
|
104
84
|
Smoke[:no_dispatch].should_not_receive(:dispatch)
|
@@ -109,21 +89,6 @@ describe Smoke::Origin do
|
|
109
89
|
end
|
110
90
|
end
|
111
91
|
|
112
|
-
it "method chaining" do
|
113
|
-
TestSource.source(:chain) do
|
114
|
-
emit do
|
115
|
-
transform :head do |head|
|
116
|
-
head.gsub(/Animal: /, '')
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
120
|
-
Smoke[:chain].rename(:head => :header).sort(:header).output.should == [{:header => "Kangaroo", :name => "Kelly"}, {:header => "Platypus", :name => "Peter"}]
|
121
|
-
end
|
122
|
-
|
123
|
-
it "should softly error when attempting to sort on a key that doesn't exist" do
|
124
|
-
Smoke[:chain].sort(:header).should_not raise_error("NoMethodError")
|
125
|
-
end
|
126
|
-
|
127
92
|
describe "preperation" do
|
128
93
|
before :all do
|
129
94
|
@source = TestSource.source(:preperation)
|
@@ -210,23 +175,46 @@ describe Smoke::Origin do
|
|
210
175
|
end
|
211
176
|
|
212
177
|
describe "key insertion" do
|
178
|
+
before do
|
179
|
+
TestSource.source(:insertion) do
|
180
|
+
emit do
|
181
|
+
insert(:source, "twitter")
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
213
186
|
it "should respond to insert" do
|
214
|
-
Smoke[:
|
187
|
+
Smoke[:insertion].should respond_to(:insert)
|
215
188
|
end
|
216
189
|
|
217
190
|
it "should insert values into each key" do
|
218
|
-
Smoke[:
|
219
|
-
Smoke[:
|
191
|
+
Smoke[:insertion].output.first.should have_key :source
|
192
|
+
Smoke[:insertion].output.first[:source].should == "twitter"
|
220
193
|
end
|
221
|
-
|
222
|
-
|
223
|
-
|
194
|
+
end
|
195
|
+
|
196
|
+
describe "sorting" do
|
197
|
+
before :all do
|
198
|
+
TestSource.source(:sorting) do
|
224
199
|
emit do
|
225
|
-
|
200
|
+
sort :header
|
226
201
|
end
|
227
202
|
end
|
228
203
|
|
229
|
-
|
204
|
+
TestSource.source(:reversed) do
|
205
|
+
emit do
|
206
|
+
sort :header
|
207
|
+
reverse
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
it "should softly error when attempting to sort on a key that doesn't exist" do
|
213
|
+
Smoke[:sorting].output.should_not raise_error("NoMethodError")
|
214
|
+
end
|
215
|
+
|
216
|
+
it "should be reversed" do
|
217
|
+
Smoke[:reversed].output.should == Smoke[:sorting].output.reverse
|
230
218
|
end
|
231
219
|
end
|
232
220
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: benschwarz-smoke
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Schwarz
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-08-06 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|