method_hooks 1.0.2 → 1.1.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.
- checksums.yaml +4 -4
- data/README.md +35 -0
- data/lib/method_hooks.rb +15 -0
- data/lib/method_hooks/version.rb +1 -1
- data/spec/lib/method_hooks_spec.rb +50 -0
- 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: adfd40e46dd90ceb8436d74af7ec395b53cf8790
|
|
4
|
+
data.tar.gz: e80731f8f389e4a7b0c6d6dd571a68ccc1e33806
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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 )
|
data/lib/method_hooks.rb
CHANGED
|
@@ -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
|
data/lib/method_hooks/version.rb
CHANGED
|
@@ -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
|