callmeback 1.1.0 → 1.2.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.
@@ -17,10 +17,10 @@ Or add it to your gem file:
17
17
  gem 'callmeback'
18
18
 
19
19
  == Usage
20
- Add +include Callmeback+ after **all** your module inclusions to be sure the gem works.
21
20
 
21
+ === Basic
22
+ Add +include Callmeback+ after *all* your module inclusions to be sure the gem works.
22
23
  class Example
23
- include Mongoid::Document
24
24
  include Callmeback
25
25
 
26
26
  before :foo => :bar
@@ -38,32 +38,52 @@ Add +include Callmeback+ after **all** your module inclusions to be sure the gem
38
38
  example.foo #=> ['bar','foo']
39
39
 
40
40
  Because this extension overrides the +.initialize+ method, if you need to redefine it, please add +callback_binding+ to your +.initialize+ method. Here is the same example as above but implemeting the +.initialize+ method:
41
- class Example
42
- include Mongoid::Document
43
- include Callmeback
44
41
 
45
- after :foo => :bar
42
+ You can use +before+, +after+ and +around+, as if you use the suggested method in the +ActiveSupport::Callbacks+ documentation.
43
+ To perform the +around+ callbacks, define them like:
44
+ around :foo => :wrapped_foo
46
45
 
47
- def initialize
48
- @result = []
49
- callback_binding
50
- end
46
+ def wrapped_foo
47
+ bar
48
+ yield
49
+ bar
50
+ end
51
51
 
52
- def foo
53
- @result << 'foo'
54
- end
52
+ === Array of callbacks
53
+ You can pass an array of callback method symbol:
54
+ before :foo => [:bar, :bar]
55
+ # => ['bar', 'bar', 'foo']
55
56
 
56
- def bar
57
- @result << 'bar'
58
- end
57
+ === Blocks
58
+ You also can pass blocks:
59
+ before :foo do
60
+ bar
59
61
  end
60
62
 
61
- example = Example.new
62
- example.foo #=> ['foo','bar']
63
+ after :foo do
64
+ bar
65
+ end
63
66
 
64
- You can use +before+, +after+ and +around+, as if you use the suggested method in the +ActiveSupport::Callbacks+ documentation.
67
+ around :foo do |&block|
68
+ bar
69
+ block.call
70
+ bar
71
+ end
72
+
73
+ === Combinations
74
+ You can combine them with methods:
75
+ before :foo => [:bar, :bar]
76
+ after :foo do
77
+ bar
78
+ end
79
+
80
+ === Array of binded methods
81
+ You can pass an array of the methods you want:
82
+ before [:foo, :bar] => [:other_method]
65
83
 
66
- Note that Mongoid is used in these examples but it *should* also work with ActiveRecord.
84
+ === Regex
85
+ You can run a callback for every method that matches a given regex:
86
+ before /^fo.$/ => [:bar, :bar]
67
87
 
68
88
  == How is it work?
69
89
  First, this gem module defines the class methods +.before+, +.after+, +.around+ in your class. Then when you instanciate your class, the gem overrides your binded methods and wraps them to bind your custom callbacks.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 1.2.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "callmeback"
8
- s.version = "1.1.0"
8
+ s.version = "1.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Sebastien Azimi"]
12
- s.date = "2013-01-15"
12
+ s.date = "2013-01-16"
13
13
  s.description = ""
14
14
  s.email = "sebstp@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -21,29 +21,40 @@ module Callmeback
21
21
  def callback_binding
22
22
  self.class.callmeback_methods.each do |callback_prefix, callback_array|
23
23
  callback_array.each do |callback_hash|
24
- callback_hash.each do |binded, callbacks|
25
- [callbacks].flatten.each do |callback|
24
+ callback_hash.each do |callback_key, callbacks|
26
25
 
27
- binded_suffix = "method_#{self.class.callmeback_method_index}"
28
- self.class.callmeback_method_index += 1
26
+ callbacks = [callbacks].flatten
29
27
 
30
- prefixed_wrapped_binded = "callmeback_wrapped_#{binded_suffix}"
31
- prefixed_unwrapped_binded = "callmeback_unwrapped_#{binded_suffix}"
28
+ if callback_key.is_a?(Regexp)
29
+ binded_methods = methods.select{|m| m =~ callback_key}
30
+ else
31
+ binded_methods = [callback_key].flatten
32
+ end
32
33
 
33
- class_eval do
34
- define_method prefixed_wrapped_binded do
35
- class_eval do
36
- define_callbacks prefixed_wrapped_binded
37
- set_callback prefixed_wrapped_binded, callback_prefix, callback
38
- end
34
+ binded_methods.each do |binded|
35
+ callbacks.each do |callback|
36
+
37
+ binded_suffix = "method_#{self.class.callmeback_method_index}"
38
+ self.class.callmeback_method_index += 1
39
+
40
+ prefixed_wrapped_binded = "callmeback_wrapped_#{binded_suffix}"
41
+ prefixed_unwrapped_binded = "callmeback_unwrapped_#{binded_suffix}"
39
42
 
40
- run_callbacks prefixed_wrapped_binded do
41
- send prefixed_unwrapped_binded
43
+ class_eval do
44
+ define_method prefixed_wrapped_binded do
45
+ class_eval do
46
+ define_callbacks prefixed_wrapped_binded
47
+ set_callback prefixed_wrapped_binded, callback_prefix, callback
48
+ end
49
+
50
+ run_callbacks prefixed_wrapped_binded do
51
+ send prefixed_unwrapped_binded
52
+ end
42
53
  end
43
- end
44
54
 
45
- alias_method prefixed_unwrapped_binded, binded
46
- alias_method binded, prefixed_wrapped_binded
55
+ alias_method prefixed_unwrapped_binded, binded
56
+ alias_method binded, prefixed_wrapped_binded
57
+ end
47
58
  end
48
59
  end
49
60
  end
@@ -55,13 +66,11 @@ module Callmeback
55
66
  attr_accessor :callmeback_methods
56
67
  attr_accessor :callmeback_method_index
57
68
 
58
- class_eval do
59
- [:before, :after, :around].each do |callback_prefix|
60
- define_method callback_prefix do |arg, &block|
61
- self.callmeback_methods[callback_prefix] ||= []
62
- arg = {:"#{arg}" => block} if block.is_a?(Proc)
63
- self.callmeback_methods[callback_prefix] << arg
64
- end
69
+ [:before, :after, :around].each do |callback_prefix|
70
+ define_method callback_prefix do |arg, &block|
71
+ self.callmeback_methods[callback_prefix] ||= []
72
+ arg = {arg => block} if block.is_a?(Proc)
73
+ self.callmeback_methods[callback_prefix] << arg
65
74
  end
66
75
  end
67
76
  end
@@ -13,6 +13,8 @@ class Example
13
13
  around :complex_foo => [:around_bar, :around_bar]
14
14
  after :complex_foo => [:bar, :bar]
15
15
 
16
+ before( /^regex_before_.+$/ => :bar )
17
+
16
18
  before :block_before_foo do
17
19
  bar
18
20
  end
@@ -50,12 +52,57 @@ class Example
50
52
  bar
51
53
  end
52
54
 
55
+ before(/^regex_block_before.+$/) do
56
+ bar
57
+ end
58
+
59
+ before(/^regex_block_method_before.+$/) do
60
+ bar
61
+ end
62
+ after(/^regex_block_method_before.+$/ => :bar)
63
+
64
+ around [:array_around_method1_foo, :array_around_method2_foo] => :around_bar
65
+
66
+ before %w{array_block_before_method1_foo array_block_before_method2_foo} do
67
+ bar
68
+ end
69
+
70
+ before %w{array_block_method_before_method1_foo array_block_method_before_method2_foo} do
71
+ bar
72
+ end
73
+ after %w{array_block_method_before_method1_foo array_block_method_before_method2_foo} => :bar
74
+
75
+
53
76
  def initialize
54
77
  @result = []
55
78
  callback_binding
56
79
  end
57
80
 
58
- %w{before after around multiple_before multiple_after multiple_around complex block_before block_after block_around complex_with_blocks complex_with_methods_and_blocks}.each do |prefix|
81
+ %w{
82
+ before after
83
+ around
84
+ multiple_before
85
+ multiple_after
86
+ multiple_around
87
+ complex
88
+ block_before
89
+ block_after
90
+ block_around
91
+ complex_with_blocks
92
+ complex_with_methods_and_blocks
93
+ regex_before_method1
94
+ regex_before_method2
95
+ regex_block_before_method1
96
+ regex_block_before_method2
97
+ regex_block_method_before_method1
98
+ regex_block_method_before_method2
99
+ array_around_method1
100
+ array_around_method2
101
+ array_block_before_method1
102
+ array_block_before_method2
103
+ array_block_method_before_method1
104
+ array_block_method_before_method2
105
+ }.each do |prefix|
59
106
  define_method "#{prefix}_foo" do
60
107
  foo
61
108
  end
@@ -81,4 +81,44 @@ describe Callmeback do
81
81
  example.complex_with_methods_and_blocks_foo.should == %w{bar bar bar foo bar bar}
82
82
  end
83
83
  end
84
+
85
+ context "the gem should handle regex as callback argument" do
86
+ it "should fire the callback for all the given methods that match the given regex" do
87
+ example.regex_before_method1_foo.should == %w{bar foo}
88
+ example.result.clear
89
+ example.regex_before_method2_foo.should == %w{bar foo}
90
+ end
91
+
92
+ it "should fire the callback for all the given blocks that match the given regex" do
93
+ example.regex_block_before_method1_foo.should == %w{bar foo}
94
+ example.result.clear
95
+ example.regex_block_before_method2_foo.should == %w{bar foo}
96
+ end
97
+
98
+ it "should fire the callback for all the given methods and blocks that match the given regex" do
99
+ example.regex_block_method_before_method1_foo.should == %w{bar foo bar}
100
+ example.result.clear
101
+ example.regex_block_method_before_method2_foo.should == %w{bar foo bar}
102
+ end
103
+ end
104
+
105
+ context "the gem should handle array as callback argument" do
106
+ it "should fire the callback for all the given methods that are included in the given array" do
107
+ example.array_around_method1_foo.should == %w{bar foo bar}
108
+ example.result.clear
109
+ example.array_around_method2_foo.should == %w{bar foo bar}
110
+ end
111
+
112
+ it "should fire the callback for all the given blocks that are included in the given array" do
113
+ example.array_block_before_method1_foo.should == %w{bar foo}
114
+ example.result.clear
115
+ example.array_block_before_method2_foo.should == %w{bar foo}
116
+ end
117
+
118
+ it "should fire the callback for all the given methods and blocks that are included in the given array" do
119
+ example.array_block_method_before_method1_foo.should == %w{bar foo bar}
120
+ example.result.clear
121
+ example.array_block_method_before_method2_foo.should == %w{bar foo bar}
122
+ end
123
+ end
84
124
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: callmeback
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.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: 2013-01-15 00:00:00.000000000 Z
12
+ date: 2013-01-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -192,7 +192,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
192
192
  version: '0'
193
193
  segments:
194
194
  - 0
195
- hash: 2462921630949056169
195
+ hash: -529320161042203170
196
196
  required_rubygems_version: !ruby/object:Gem::Requirement
197
197
  none: false
198
198
  requirements: