bubot 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5164f040c4ac3f870f748ebd333ffc0a755b4e5f
4
- data.tar.gz: d31db432a0ae70aedb45aeb98ec43b1f5a699581
3
+ metadata.gz: 8764e8dfff1268e2c54d7248d67d3ef8d6ad96ff
4
+ data.tar.gz: a928121e7306a845b1cc17b3ea3f676163047f94
5
5
  SHA512:
6
- metadata.gz: c5bf37ee1f8e8f37b1bd236e37dc86066c3ac341b4e3c78243abcc0e48d99a8d7a9b925214e637dacbb4be373bd65a9314171e55991ff5ccae4c64fc830860d0
7
- data.tar.gz: 23dc04269ed4d55b84d93583c49cd98cd19ac55fbec4ee1ad55752fd4de7e7596704f05b12cbda67a88cc388861a7b7dafb05782dbef54923582e2d71052381e
6
+ metadata.gz: f6fde114e83d818bfe1d267fbae30dc55cfebf60b21b1833f722015aa4c76a775c48089d8c7d0def03ea94ce4646b8f9b9494a786c2876ccdfc73683d505a38b
7
+ data.tar.gz: 62d5d55a37d2653d7ea238e6a418bf6c2421a817c4f56d466b46e4541fc2190fad3763302e86aec833c94b7b1ec6357f0dbf88ad0e07b0da20b75542dc9878b6
data/README.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # Bubot
2
2
 
3
- Take action when methods take too long
3
+ Watch a method. If it takes longer than a specified amount of time,
4
+ execute a block. It's a callback that only happens after a threshold.
5
+
6
+ ## Requirements
7
+
8
+ ruby > 2.0.0
4
9
 
5
10
  ## Installation
6
11
 
@@ -20,18 +25,53 @@ Or install it yourself as:
20
25
 
21
26
  Extend Bubot in your class.
22
27
 
23
- This gives you the class method `.watch(:method_name, threshold)`.
28
+ This gives you the class method `.watch(:method_name, options)`.
29
+
30
+ If a watched method takes longer than options[:timeout], the block will execute.
31
+ Remember, the timeout is 0 by default so if you don't pass it a timeout, the
32
+ block will always execute (like a callback)
33
+
34
+ ### Example
35
+
36
+ ```ruby
37
+ class WebAPI
38
+ include Bubot
39
+
40
+ watch(:response, timeout: 2) do |web_api_object, time_it_took, method_response|
41
+ puts web_api_object # => web_api_object instance
42
+ puts time_it_took # => 3.5 (seconds)
43
+ puts method_response # => "body"
44
+ end
24
45
 
25
- If a watched method takes longer than the specified amount of time (threshold), the block will execute.
46
+ def response
47
+ sleep 3
48
+ "body"
49
+ end
50
+ end
51
+ ```
52
+
53
+ You can also pass any object that responds to `call` by using the `:with`
54
+ option.
26
55
 
27
56
  ```ruby
28
- class Foo
29
- extend Bubot
57
+ class LoggingStrategy
58
+
59
+ def self.call(web_api_object, time_it_took, method_response)
60
+ puts web_api_object # => web_api_object instance
61
+ puts time_it_took # => 5.2 (seconds)
62
+ puts method_response # => "body"
63
+ end
64
+
65
+ end
66
+
67
+ class WebAPI
68
+ include Bubot
30
69
 
31
- watch(:bar, 1) { run_some_code }
70
+ watch :response, timeout: 3, with: LoggingStrategy
32
71
 
33
- def bar
34
- sleep 1.1
35
- end
72
+ def response
73
+ sleep 5
74
+ "body"
75
+ end
36
76
  end
37
77
  ```
data/Rakefile CHANGED
@@ -1 +1,5 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ task :default do
4
+ system "rspec"
5
+ end
@@ -21,4 +21,6 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "rake"
22
22
  spec.add_development_dependency "rspec", "~> 2.14.1"
23
23
  spec.add_development_dependency "pry"
24
+
25
+ spec.add_dependency "activesupport"
24
26
  end
@@ -1,48 +1,51 @@
1
1
  require "bubot/version"
2
+ require "active_support/concern"
3
+ require "active_support/callbacks"
4
+ require "active_support/core_ext/module/aliasing"
2
5
 
3
6
  module Bubot
7
+ extend ActiveSupport::Concern
8
+ include ActiveSupport::Callbacks
4
9
 
5
- def watch(method_name, options={})
6
- defaults = { timeout: 0 }
7
- defaults.merge!(options)
8
- define_method("#{method_name}_with_feature") do |*args, &block|
9
- start_time = Time.now
10
+ module ClassMethods
11
+ def watch(method_name, timeout: 0, with: nil, &block)
12
+ define_callbacks method_name
10
13
 
11
- method_return_value = send("#{method_name}_without_feature".to_sym, *args, &block)
14
+ past_time_block = with || (block if block_given?)
12
15
 
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)
16
+ define_method("#{method_name}_with_bubot") do |*args, &block|
17
+ run_callbacks method_name do
18
+ send("#{method_name}_without_bubot".to_sym, *args, &block)
18
19
  end
19
20
  end
20
21
 
21
- method_return_value
22
- end
22
+ alias_method_chain_or_register_for_chaining method_name
23
23
 
24
- alias_method_chain_or_register(method_name)
25
- end
24
+ set_callback method_name, :around, ->(r, &block) do
25
+ start_time = Time.now
26
26
 
27
- private
27
+ method_return_value = block.call
28
28
 
29
- def alias_method_chain_or_register(method_name)
30
- if method_defined?(method_name)
31
- alias_method_chain(method_name)
32
- else
33
- (@method_names ||= []).push(method_name)
29
+ if (total_time = Time.now - start_time) >= timeout
30
+ past_time_block.call(self, total_time, method_return_value)
31
+ end
32
+ end
34
33
  end
35
- end
36
34
 
37
- def method_added(method_name)
38
- if (@method_names ||= []).delete(method_name)
39
- alias_method_chain(method_name)
35
+ private
36
+
37
+ def alias_method_chain_or_register_for_chaining(method_name)
38
+ if method_defined?(method_name)
39
+ alias_method_chain(method_name, :bubot)
40
+ else
41
+ (@method_names ||= []).push(method_name)
42
+ end
40
43
  end
41
- end
42
44
 
43
- def alias_method_chain(method_name)
44
- alias_method "#{method_name}_without_feature".to_sym, method_name
45
- alias_method method_name, "#{method_name}_with_feature".to_sym
45
+ def method_added(method_name)
46
+ if (@method_names ||= []).delete(method_name)
47
+ alias_method_chain(method_name, :bubot)
48
+ end
49
+ end
46
50
  end
47
-
48
51
  end
@@ -1,3 +1,3 @@
1
1
  module Bubot
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -12,7 +12,7 @@ end
12
12
 
13
13
  class Qux
14
14
  include Foo
15
- extend Bubot
15
+ include Bubot
16
16
 
17
17
  watch :not_too_slow, timeout: 0.005 do
18
18
  Baz.buz
@@ -39,7 +39,7 @@ describe Bubot do
39
39
 
40
40
  it "watch is before the method" do
41
41
  class Before
42
- extend Bubot
42
+ include Bubot
43
43
  watch(:next_method, timeout: 0.001) { Baz.buz }
44
44
  def next_method; sleep 0.002; end
45
45
  end
@@ -50,7 +50,7 @@ describe Bubot do
50
50
 
51
51
  it "watch is after the method" do
52
52
  class After
53
- extend Bubot
53
+ include Bubot
54
54
  def previous_method; sleep 0.002; end
55
55
  watch(:previous_method, timeout: 0.001) { Baz.buz }
56
56
  end
@@ -63,7 +63,7 @@ describe Bubot do
63
63
  context "timeout is optional" do
64
64
  it "timeout is not passed" do
65
65
  class NoTimeout
66
- extend Bubot
66
+ include Bubot
67
67
  watch(:without_timeout) { Baz.buz }
68
68
  def without_timeout() end
69
69
  end
@@ -77,7 +77,7 @@ describe Bubot do
77
77
  it "does nothing and does not break" do
78
78
  expect do
79
79
  class MethodDoesNotExist
80
- extend Bubot
80
+ include Bubot
81
81
  watch(:dont_exist, timeout: 0.001) { Baz.buz }
82
82
  end
83
83
  end.not_to raise_error
@@ -93,7 +93,7 @@ describe Bubot do
93
93
  end
94
94
 
95
95
  class PassesSelf
96
- extend Bubot
96
+ include Bubot
97
97
  watch(:pass_self, timeout: 0.001) do |instance|
98
98
  ReceivesSelfStrategy.execute(instance)
99
99
  end
@@ -114,7 +114,7 @@ describe Bubot do
114
114
  end
115
115
 
116
116
  class PassesTime
117
- extend Bubot
117
+ include Bubot
118
118
  watch(:pass_time, timeout: 0.001) do |instance, time|
119
119
  ReceivesTimeStrategy.execute(instance, time)
120
120
  end
@@ -138,7 +138,7 @@ describe Bubot do
138
138
  end
139
139
 
140
140
  class PassesReturnValue
141
- extend Bubot
141
+ include Bubot
142
142
  watch(:pass_return_value, timeout: 0.001) do |instance, time, return_value|
143
143
  ReceivesReturnValueStrategy.execute(instance, time, return_value)
144
144
  end
@@ -169,7 +169,7 @@ describe Bubot do
169
169
  end
170
170
 
171
171
  class UsingWith
172
- extend Bubot
172
+ include Bubot
173
173
 
174
174
  watch :original, with: RespondingStrategy
175
175
 
@@ -191,7 +191,7 @@ describe Bubot do
191
191
  describe "the original method" do
192
192
  it "redefines the method to return the original value" do
193
193
  class OriginalMethod
194
- extend Bubot
194
+ include Bubot
195
195
 
196
196
  watch :original do
197
197
  #something
@@ -209,7 +209,7 @@ describe Bubot do
209
209
 
210
210
  it "accepts the original methods arguments" do
211
211
  class OriginalArguments
212
- extend Bubot
212
+ include Bubot
213
213
 
214
214
  watch :arguments do
215
215
  #something
@@ -221,14 +221,14 @@ describe Bubot do
221
221
  end
222
222
 
223
223
  original_class = OriginalArguments.new
224
- expect(original_class).to receive(:arguments_without_feature).with("foo", "bar")
224
+ expect(original_class).to receive(:arguments_without_bubot).with("foo", "bar")
225
225
  original_class.arguments('foo', 'bar')
226
226
 
227
227
  end
228
228
 
229
229
  it "accepts the original methods block" do
230
230
  class OriginalBlock
231
- extend Bubot
231
+ include Bubot
232
232
 
233
233
  watch :with_block do
234
234
  #something
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bubot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Cooper
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-27 00:00:00.000000000 Z
12
+ date: 2014-03-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -67,6 +67,20 @@ dependencies:
67
67
  - - '>='
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: activesupport
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
70
84
  description: Take action when methods take too long
71
85
  email:
72
86
  - mrmicahcooper@gmail.com
@@ -107,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
121
  version: '0'
108
122
  requirements: []
109
123
  rubyforge_project:
110
- rubygems_version: 2.0.0
124
+ rubygems_version: 2.1.11
111
125
  signing_key:
112
126
  specification_version: 4
113
127
  summary: Take action when methods take too long