method_hooks 1.0.2 → 1.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: f7e454d3f08fdcc2ee62ede53b2b570decdc8e93
4
- data.tar.gz: cb9bf2fc3b04f9b10c65df6b2a3cde887cdf392d
3
+ metadata.gz: adfd40e46dd90ceb8436d74af7ec395b53cf8790
4
+ data.tar.gz: e80731f8f389e4a7b0c6d6dd571a68ccc1e33806
5
5
  SHA512:
6
- metadata.gz: 18014051b5fc06b6cbd3e2181b32f46c2902a26b30da7c7309135dc235c20185b39a73650a2517f551ca9aecc7a52c88d37d10cceb438e05ac23a790c8b3505c
7
- data.tar.gz: ed644fb295f63dbe1d819c686dd68d6d74d0d6d5e77eb7ccfa5a06a042d9543328ecd25a43193c7fe85a5b71960a4357ba5c4d6c4a07686c2888ae22f9d569fc
6
+ metadata.gz: b2f438613ca27a08f988ee1a68dca37aa879fd16cc347ced66273ac2bee6ceb6c7295eee63a8a05f3927978806106ed359279069beb6cd62c3af5395c0b4f6f3
7
+ data.tar.gz: 4feebd5a3d2b36190a1396f7a300b45092cac6e47041203ca8ea92201baf12812e59b8d2959ae0a8dd03428517a8ddc928401a1740e2d6afab17bfee1fba9b03
data/README.md CHANGED
@@ -69,6 +69,41 @@ after
69
69
  =end
70
70
  ```
71
71
 
72
+ You can also specify a method to be called instead of using a block:
73
+
74
+ ```ruby
75
+ class Model
76
+ extend MethodHooks
77
+
78
+ before :save, :my_method
79
+
80
+ def save
81
+ puts 'save'
82
+ end
83
+
84
+ private
85
+
86
+ def my_method
87
+ puts 'my_method'
88
+ end
89
+
90
+ end
91
+
92
+ model = Model.new
93
+ model.save
94
+
95
+ =begin
96
+
97
+ Outputs the following:
98
+
99
+ my_method
100
+ save
101
+
102
+ =end
103
+
104
+ ```
105
+
106
+
72
107
  ## Contributing
73
108
 
74
109
  1. Fork it ( https://github.com/[my-github-username]/method_hooks/fork )
@@ -53,6 +53,11 @@ module MethodHooks
53
53
  end
54
54
 
55
55
  def before(*method_names, &block)
56
+ if block.nil?
57
+ callback_method = method_names.pop
58
+ block = proc { method(callback_method).call }
59
+ end
60
+
56
61
  method_names.each do |method_name|
57
62
  before_callbacks[method_name] << block
58
63
  end
@@ -63,6 +68,11 @@ module MethodHooks
63
68
  end
64
69
 
65
70
  def around(*method_names, &block)
71
+ if block.nil?
72
+ callback_method = method_names.pop
73
+ block = proc { |method| method(callback_method).call(method) }
74
+ end
75
+
66
76
  method_names.each do |method_name|
67
77
  around_callbacks[method_name] << block
68
78
  end
@@ -73,6 +83,11 @@ module MethodHooks
73
83
  end
74
84
 
75
85
  def after(*method_names, &block)
86
+ if block.nil?
87
+ callback_method = method_names.pop
88
+ block = proc { method(callback_method).call }
89
+ end
90
+
76
91
  method_names.each do |method_name|
77
92
  after_callbacks[method_name] << block
78
93
  end
@@ -1,3 +1,3 @@
1
1
  module MethodHooks
2
- VERSION = "1.0.2"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -58,6 +58,22 @@ describe MethodHooks do
58
58
  expect(base.events).to eq(['first before', 'second before', 'save'])
59
59
  end
60
60
 
61
+ it 'should call a method (by name) if no block is given' do
62
+ Base.instance_eval do
63
+ before(:save, :my_method)
64
+ end
65
+
66
+ Base.class_eval do
67
+ def my_method
68
+ @events << 'my_method'
69
+ end
70
+ end
71
+
72
+ base = Base.new
73
+ base.save
74
+
75
+ expect(base.events).to eq(['my_method', 'save'])
76
+ end
61
77
  end
62
78
 
63
79
  describe '::around' do
@@ -77,6 +93,24 @@ describe MethodHooks do
77
93
  expect(base.events).to eq(['before_around', 'save', 'after_around'])
78
94
  end
79
95
 
96
+ it 'should call a method (by name) if no block is given' do
97
+ Base.instance_eval do
98
+ around(:save, :my_method)
99
+ end
100
+
101
+ Base.class_eval do
102
+ def my_method(method)
103
+ @events << 'before my_method'
104
+ method.call
105
+ @events << 'after my_method'
106
+ end
107
+ end
108
+
109
+ base = Base.new
110
+ base.save
111
+
112
+ expect(base.events).to eq(['before my_method', 'save', 'after my_method'])
113
+ end
80
114
  end
81
115
 
82
116
  describe '::after' do
@@ -92,6 +126,22 @@ describe MethodHooks do
92
126
  expect(base.events).to eq(['save', 'after'])
93
127
  end
94
128
 
129
+ it 'should call a method (by name) if no block is given' do
130
+ Base.instance_eval do
131
+ after(:save, :my_method)
132
+ end
133
+
134
+ Base.class_eval do
135
+ def my_method
136
+ @events << 'my_method'
137
+ end
138
+ end
139
+
140
+ base = Base.new
141
+ base.save
142
+
143
+ expect(base.events).to eq(['save', 'my_method'])
144
+ end
95
145
  end
96
146
 
97
147
  it 'does not override existing .method_added hook' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: method_hooks
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frank Bonetti