bubot 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/bubot.rb +13 -8
- data/lib/bubot/version.rb +1 -1
- data/spec/bubot_spec.rb +37 -8
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 74f041f6e5452977767a4b2b312c88a9ccd1735b
|
4
|
+
data.tar.gz: de3f6d5b47ffae106723727bfd591f39b79bdf18
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 70539412f14584c7f9cdf7df404fa39204fc4eea735c995f4c94ac4d47caee338c8dfaa80eef6097e29868770a90e45026700be14f0279e03150219201379a4c
|
7
|
+
data.tar.gz: 1e6752083102f66d367290819c2142f171646fe14e301f3d2651d824d3248c1ee71525394590ae739e07c50627ec418b72974af043b43e93fbb613f48b072925
|
data/lib/bubot.rb
CHANGED
@@ -2,17 +2,22 @@ require "bubot/version"
|
|
2
2
|
|
3
3
|
module Bubot
|
4
4
|
|
5
|
-
def watch(method_name,
|
5
|
+
def watch(method_name, options={})
|
6
|
+
defaults = { timeout: 0 }
|
7
|
+
defaults.merge!(options)
|
6
8
|
define_method("#{method_name}_with_feature") do |*args, &block|
|
7
9
|
start_time = Time.now
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
|
11
|
+
method_return_value = send("#{method_name}_without_feature".to_sym, *args, &block)
|
12
|
+
|
13
|
+
if (total_time = Time.now - start_time) > defaults[:timeout]
|
14
|
+
if options[:with]
|
15
|
+
options[:with].call(self, total_time, method_return_value)
|
16
|
+
else
|
17
|
+
yield(self, total_time, method_return_value)
|
18
|
+
end
|
15
19
|
end
|
20
|
+
|
16
21
|
method_return_value
|
17
22
|
end
|
18
23
|
|
data/lib/bubot/version.rb
CHANGED
data/spec/bubot_spec.rb
CHANGED
@@ -14,11 +14,11 @@ class Qux
|
|
14
14
|
include Foo
|
15
15
|
extend Bubot
|
16
16
|
|
17
|
-
watch :not_too_slow, 0.005 do
|
17
|
+
watch :not_too_slow, timeout: 0.005 do
|
18
18
|
Baz.buz
|
19
19
|
end
|
20
20
|
|
21
|
-
watch :too_slow, 0.005 do
|
21
|
+
watch :too_slow, timeout: 0.005 do
|
22
22
|
Baz.buz
|
23
23
|
end
|
24
24
|
end
|
@@ -40,7 +40,7 @@ describe Bubot do
|
|
40
40
|
it "watch is before the method" do
|
41
41
|
class Before
|
42
42
|
extend Bubot
|
43
|
-
watch(:next_method, 0.001) { Baz.buz }
|
43
|
+
watch(:next_method, timeout: 0.001) { Baz.buz }
|
44
44
|
def next_method; sleep 0.002; end
|
45
45
|
end
|
46
46
|
|
@@ -52,7 +52,7 @@ describe Bubot do
|
|
52
52
|
class After
|
53
53
|
extend Bubot
|
54
54
|
def previous_method; sleep 0.002; end
|
55
|
-
watch(:previous_method, 0.001) { Baz.buz }
|
55
|
+
watch(:previous_method, timeout: 0.001) { Baz.buz }
|
56
56
|
end
|
57
57
|
|
58
58
|
Baz.should_receive(:buz).once
|
@@ -78,7 +78,7 @@ describe Bubot do
|
|
78
78
|
expect do
|
79
79
|
class MethodDoesNotExist
|
80
80
|
extend Bubot
|
81
|
-
watch(:dont_exist, 0.001) { Baz.buz }
|
81
|
+
watch(:dont_exist, timeout: 0.001) { Baz.buz }
|
82
82
|
end
|
83
83
|
end.not_to raise_error
|
84
84
|
end
|
@@ -94,7 +94,7 @@ describe Bubot do
|
|
94
94
|
|
95
95
|
class PassesSelf
|
96
96
|
extend Bubot
|
97
|
-
watch(:pass_self, 0.001) do |instance|
|
97
|
+
watch(:pass_self, timeout: 0.001) do |instance|
|
98
98
|
RecievesSelfStrategy.execute(instance)
|
99
99
|
end
|
100
100
|
def pass_self; sleep 0.002; end
|
@@ -115,7 +115,7 @@ describe Bubot do
|
|
115
115
|
|
116
116
|
class PassesTime
|
117
117
|
extend Bubot
|
118
|
-
watch(:pass_time, 0.001) do |instance, time|
|
118
|
+
watch(:pass_time, timeout: 0.001) do |instance, time|
|
119
119
|
RecievesTimeStrategy.execute(instance, time)
|
120
120
|
end
|
121
121
|
def pass_time; sleep 0.002; end
|
@@ -139,7 +139,7 @@ describe Bubot do
|
|
139
139
|
|
140
140
|
class PassesReturnValue
|
141
141
|
extend Bubot
|
142
|
-
watch(:pass_return_value, 0.001) do |instance, time, return_value|
|
142
|
+
watch(:pass_return_value, timeout: 0.001) do |instance, time, return_value|
|
143
143
|
RecievesReturnValueStrategy.execute(instance, time, return_value)
|
144
144
|
end
|
145
145
|
def pass_return_value
|
@@ -159,6 +159,35 @@ describe Bubot do
|
|
159
159
|
end
|
160
160
|
end
|
161
161
|
|
162
|
+
describe "using a `with` strategy" do
|
163
|
+
|
164
|
+
it ":with" do
|
165
|
+
class RespondingStrategy
|
166
|
+
def self.call(method_name, timeout, method_return_value)
|
167
|
+
# do something
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
class UsingWith
|
172
|
+
extend Bubot
|
173
|
+
|
174
|
+
watch :original, with: RespondingStrategy
|
175
|
+
|
176
|
+
def original
|
177
|
+
"original value"
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
original_class = UsingWith.new
|
182
|
+
expect(RespondingStrategy).to receive(:call) do |instance, time, value|
|
183
|
+
expect(instance).to be(original_class)
|
184
|
+
expect(time).to be > 0
|
185
|
+
expect(value).to eq "original value"
|
186
|
+
end
|
187
|
+
original_class.original
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
162
191
|
describe "the original method" do
|
163
192
|
it "redefines the method to return the original value" do
|
164
193
|
class OriginalMethod
|