bubot 0.0.8 → 0.1.0

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: 8764e8dfff1268e2c54d7248d67d3ef8d6ad96ff
4
- data.tar.gz: a928121e7306a845b1cc17b3ea3f676163047f94
3
+ metadata.gz: c1064314ec4c93d7e867557fe47933598b7a6105
4
+ data.tar.gz: ccb71b09f29ec586c3a0b37a1f2d77f7762a2ae4
5
5
  SHA512:
6
- metadata.gz: f6fde114e83d818bfe1d267fbae30dc55cfebf60b21b1833f722015aa4c76a775c48089d8c7d0def03ea94ce4646b8f9b9494a786c2876ccdfc73683d505a38b
7
- data.tar.gz: 62d5d55a37d2653d7ea238e6a418bf6c2421a817c4f56d466b46e4541fc2190fad3763302e86aec833c94b7b1ec6357f0dbf88ad0e07b0da20b75542dc9878b6
6
+ metadata.gz: eb56d4dda32148fc88d8e8bc8ff7f38d1775cadcb1309eda0ad348680cc4fbe2c27bf18d00f77d2503e72cca85f0e68244c6b876a02406753935f3f39972897f
7
+ data.tar.gz: c5c90448fb55e80795fc06905f7d42df5d18192a34aab138efc5970538685adc9e879f9a59a0acf506cb55ca2a43254f3be82a66e1d1222f5767490d594a1c79
data/README.md CHANGED
@@ -23,13 +23,15 @@ Or install it yourself as:
23
23
 
24
24
  ## Usage
25
25
 
26
- Extend Bubot in your class.
26
+ Include Bubot in your class.
27
27
 
28
28
  This gives you the class method `.watch(:method_name, options)`.
29
29
 
30
30
  If a watched method takes longer than options[:timeout], the block will execute.
31
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)
32
+ block will always execute (like an after callback).
33
+
34
+ Also, as a bonus, you get `.bubot`, which is syntactic sugar for active support callbacks.
33
35
 
34
36
  ### Example
35
37
 
@@ -47,6 +49,21 @@ class WebAPI
47
49
  sleep 3
48
50
  "body"
49
51
  end
52
+
53
+ # Syntactic sugar for active support callbacks
54
+ bubot :before, :save, :udpate
55
+ bubot :around, :save, ->(instance, &block) { puts "Before"; block.call; puts "All Done" }
56
+ bubot :after, :save do
57
+ puts "Another thing to do after"
58
+ end
59
+
60
+ def save
61
+ puts "Saving..."
62
+ end
63
+
64
+ def update
65
+ puts "Updating..."
66
+ end
50
67
  end
51
68
  ```
52
69
 
@@ -8,20 +8,17 @@ module Bubot
8
8
  include ActiveSupport::Callbacks
9
9
 
10
10
  module ClassMethods
11
+ def bubot(before_after_around, method_name, callback=nil, &block)
12
+ configure_bubot method_name
13
+ set_callback(method_name, before_after_around, (callback || block))
14
+ end
15
+
11
16
  def watch(method_name, timeout: 0, with: nil, &block)
12
- define_callbacks method_name
17
+ configure_bubot method_name
13
18
 
14
19
  past_time_block = with || (block if block_given?)
15
20
 
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)
19
- end
20
- end
21
-
22
- alias_method_chain_or_register_for_chaining method_name
23
-
24
- set_callback method_name, :around, ->(r, &block) do
21
+ set_callback method_name, :around, ->(instance, &block) do
25
22
  start_time = Time.now
26
23
 
27
24
  method_return_value = block.call
@@ -34,6 +31,21 @@ module Bubot
34
31
 
35
32
  private
36
33
 
34
+ def configure_bubot(method_name)
35
+ method_name_with_bubot = "#{method_name}_with_bubot".to_sym
36
+ return if instance_methods.include? method_name_with_bubot
37
+
38
+ define_callbacks method_name
39
+
40
+ define_method(method_name_with_bubot) do |*args, &block|
41
+ run_callbacks method_name do
42
+ send("#{method_name}_without_bubot".to_sym, *args, &block)
43
+ end
44
+ end
45
+
46
+ alias_method_chain_or_register_for_chaining method_name
47
+ end
48
+
37
49
  def alias_method_chain_or_register_for_chaining(method_name)
38
50
  if method_defined?(method_name)
39
51
  alias_method_chain(method_name, :bubot)
@@ -1,3 +1,3 @@
1
1
  module Bubot
2
- VERSION = "0.0.8"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -23,7 +23,40 @@ class Qux
23
23
  end
24
24
  end
25
25
 
26
+ class CallbackTester
27
+ include Bubot
28
+
29
+ bubot :before, :save, :update
30
+ bubot :around, :save, ->(instance, &block) { instance.do_other_stuff; block.call }
31
+ bubot(:after, :save) do
32
+ Baz.buz
33
+ end
34
+
35
+ def update; end
36
+ def save; end
37
+ def do_other_stuff; end
38
+ end
39
+
26
40
  describe Bubot do
41
+ describe ".bubot" do
42
+ subject(:callbacks) { CallbackTester.new }
43
+ it "allows a method to be executed before another method" do
44
+ expect(callbacks).to receive(:update).once
45
+ callbacks.save
46
+ end
47
+
48
+ it "allows a block to be executed as a callback" do
49
+ expect(Baz).to receive(:buz).once
50
+ callbacks.save
51
+ end
52
+
53
+ it "allows a lambda to be executed as a callback" do
54
+ expect(callbacks).to receive(:do_other_stuff).once
55
+ callbacks.save
56
+ end
57
+
58
+ end
59
+
27
60
  describe ".watch" do
28
61
  it "calls the strategy(block) when the time exceeds the max time" do
29
62
  Baz.should_receive(:buz).once
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.8
4
+ version: 0.1.0
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: 2014-03-28 00:00:00.000000000 Z
12
+ date: 2014-03-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler