big_spoon 0.2.0 → 0.2.1
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.
- data/CHANGES.md +11 -0
- data/VERSION +1 -1
- data/big_spoon.gemspec +2 -2
- data/lib/big_spoon.rb +19 -10
- data/lib/big_spoon/hook.rb +33 -33
- data/spec/lib/big_spoon_spec.rb +2 -6
- metadata +3 -3
data/CHANGES.md
CHANGED
@@ -1,4 +1,15 @@
|
|
1
1
|
# CHANGELOG
|
2
|
+
## 0.2.1
|
3
|
+
### `hooks` now aliased as `spoon` to avoid namespace conflicts
|
4
|
+
- Fixes bugs w/Railties generators and other libraries that use
|
5
|
+
a class method named `hooks`
|
6
|
+
|
7
|
+
## 0.2.0
|
8
|
+
### Multiple bugs fixed and test for decent backtraces
|
9
|
+
- before and after filters no longer assume argument-less-methods
|
10
|
+
- `before` and `after` are now in method_missing so they won't
|
11
|
+
conflict with other libraries
|
12
|
+
|
2
13
|
## 0.1.1
|
3
14
|
### :if, :unless, and tested in production!
|
4
15
|
- :if => :method and :unless => lambda { false } are now supported.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
data/big_spoon.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "big_spoon"
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Flip Sasser"]
|
12
|
-
s.date = "2012-08-
|
12
|
+
s.date = "2012-08-20"
|
13
13
|
s.description = "\n BigSpoon will add a hooks method to every class. Call that method with a block and add all kinds of fun hooks before and after your methods.\n "
|
14
14
|
s.email = "flip@x451.com"
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/big_spoon.rb
CHANGED
@@ -2,10 +2,10 @@ require 'big_spoon/hook'
|
|
2
2
|
|
3
3
|
module BigSpoon
|
4
4
|
module ClassMethods
|
5
|
-
def
|
5
|
+
def spoon(options = {}, &block)
|
6
6
|
@hooks ||= Hook.for(self)
|
7
7
|
@hooks.instance_eval(&block) if block_given?
|
8
|
-
|
8
|
+
|
9
9
|
unless respond_to?(:_big_spoon_original_method_added)
|
10
10
|
class << self
|
11
11
|
alias :_big_spoon_original_method_added :method_added
|
@@ -18,24 +18,33 @@ module BigSpoon
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
@hooks
|
21
|
-
end # `
|
21
|
+
end # `big_spoon` method
|
22
22
|
|
23
23
|
private
|
24
|
-
def
|
24
|
+
def method_missing_with_big_spoon(method_name, *args, &block)
|
25
25
|
case method_name.to_s
|
26
|
+
when 'hooks'
|
27
|
+
spoon *args, &block
|
26
28
|
when 'after'
|
27
|
-
hooks.after *args
|
29
|
+
hooks.after *args, &block
|
28
30
|
when /^after_(.+)$/
|
29
|
-
hooks.after $1, *args
|
31
|
+
hooks.after $1, *args, &block
|
30
32
|
when 'before'
|
31
|
-
hooks.before *args
|
33
|
+
hooks.before *args, &block
|
32
34
|
when /^before_(.+)$/
|
33
|
-
hooks.before $1, *args
|
35
|
+
hooks.before $1, *args, &block
|
34
36
|
else
|
35
|
-
|
37
|
+
method_missing_without_big_spoon(method_name, *args)
|
36
38
|
end
|
37
39
|
end
|
38
40
|
end # `ClassMethods` module
|
39
41
|
end # `BigSpoon` module
|
40
42
|
|
41
|
-
Object.
|
43
|
+
Object.class_eval do
|
44
|
+
extend BigSpoon::ClassMethods
|
45
|
+
class << self
|
46
|
+
alias :method_missing_without_big_spoon :method_missing
|
47
|
+
alias :method_missing :method_missing_with_big_spoon
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
data/lib/big_spoon/hook.rb
CHANGED
@@ -15,12 +15,12 @@ module BigSpoon
|
|
15
15
|
|
16
16
|
# Define a method to execute after another
|
17
17
|
def after(method_to_hook, method_to_call = nil, options = {}, &block)
|
18
|
-
|
18
|
+
add(:after, method_to_hook, method_to_call, options, &block)
|
19
19
|
end
|
20
20
|
|
21
21
|
# Define a method to execute before another
|
22
22
|
def before(method_to_hook, method_to_call = nil, options = {}, &block)
|
23
|
-
|
23
|
+
add(:before, method_to_hook, method_to_call, options, &block)
|
24
24
|
end
|
25
25
|
|
26
26
|
# def clear!
|
@@ -65,17 +65,39 @@ module BigSpoon
|
|
65
65
|
methods[method_to_hook.to_sym] && hookable?(method_to_hook) && !hooked?(method_to_hook)
|
66
66
|
end
|
67
67
|
|
68
|
-
def unhook!(method_to_hook)
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
end
|
68
|
+
# def unhook!(method_to_hook)
|
69
|
+
# hooked_method = hooked_method(method_to_hook)
|
70
|
+
# original_method = original_method(method_to_hook)
|
71
|
+
# line = __LINE__; alias_these_hooks = <<-hooks
|
72
|
+
# alias :#{method_to_hook} #{original_method}
|
73
|
+
# remove_method #{hooked_method}
|
74
|
+
# hooks
|
75
|
+
# klass.class_eval alias_these_hooks, __FILE__, line.succ
|
76
|
+
# end
|
77
77
|
|
78
78
|
private
|
79
|
+
def add(before_or_after, method_to_hook, method_to_call = nil, options = {}, &block)
|
80
|
+
method_to_hook = method_to_hook.to_sym
|
81
|
+
if method_to_call.is_a? Hash
|
82
|
+
options = method_to_call
|
83
|
+
method_to_call = nil
|
84
|
+
end
|
85
|
+
if block_given?
|
86
|
+
method_to_call = block
|
87
|
+
else
|
88
|
+
method_to_call = method_to_call.to_sym
|
89
|
+
end
|
90
|
+
|
91
|
+
methods[method_to_hook] ||= {}
|
92
|
+
methods[method_to_hook][before_or_after] ||= []
|
93
|
+
method_definition = options.merge({:method => method_to_call})
|
94
|
+
methods[method_to_hook][before_or_after].push(method_definition) unless methods[method_to_hook][before_or_after].any? do |other_method_definition|
|
95
|
+
other_method_definition[:method] == method_to_call
|
96
|
+
end
|
97
|
+
|
98
|
+
hook!(method_to_hook) if should_hook?(method_to_hook)
|
99
|
+
end
|
100
|
+
|
79
101
|
def execute(before_or_after, method_to_hook, instance)
|
80
102
|
methods[method_to_hook] ||= {}
|
81
103
|
methods[method_to_hook][before_or_after] ||= []
|
@@ -102,28 +124,6 @@ module BigSpoon
|
|
102
124
|
end
|
103
125
|
end
|
104
126
|
|
105
|
-
def hook(before_or_after, method_to_hook, method_to_call = nil, options = {}, &block)
|
106
|
-
method_to_hook = method_to_hook.to_sym
|
107
|
-
if method_to_call.is_a? Hash
|
108
|
-
options = method_to_call
|
109
|
-
method_to_call = nil
|
110
|
-
end
|
111
|
-
if block_given?
|
112
|
-
method_to_call = block
|
113
|
-
else
|
114
|
-
method_to_call = method_to_call.to_sym
|
115
|
-
end
|
116
|
-
|
117
|
-
methods[method_to_hook] ||= {}
|
118
|
-
methods[method_to_hook][before_or_after] ||= []
|
119
|
-
method_definition = options.merge({:method => method_to_call})
|
120
|
-
methods[method_to_hook][before_or_after].push(method_definition) unless methods[method_to_hook][before_or_after].any? do |other_method_definition|
|
121
|
-
other_method_definition[:method] == method_to_call
|
122
|
-
end
|
123
|
-
|
124
|
-
hook!(method_to_hook) if should_hook?(method_to_hook)
|
125
|
-
end
|
126
|
-
|
127
127
|
def hookable?(method_to_hook)
|
128
128
|
klass.method_defined?(method_to_hook)
|
129
129
|
end
|
data/spec/lib/big_spoon_spec.rb
CHANGED
@@ -48,10 +48,6 @@ describe BigSpoon do
|
|
48
48
|
@big_spoon_test = BigSpoonTest.new
|
49
49
|
end
|
50
50
|
|
51
|
-
it "should monkeypatch Object" do
|
52
|
-
Object.should respond_to(:hooks)
|
53
|
-
end
|
54
|
-
|
55
51
|
it "should allow me to add a hook before an object's methods BEFORE THEY EXIST" do
|
56
52
|
@big_spoon_test.should_receive(:hook_1)
|
57
53
|
@big_spoon_test.foo!
|
@@ -145,7 +141,7 @@ describe BigSpoon do
|
|
145
141
|
@big_spoon_test.i_accept_many("ohai", "i'll be an array", "someday")
|
146
142
|
end
|
147
143
|
|
148
|
-
it "should handle an
|
144
|
+
it "should handle an optional argument" do
|
149
145
|
BigSpoonTest.after :i_might_accept_one, :hook_11
|
150
146
|
@big_spoon_test.should_receive(:puts).with(nil)
|
151
147
|
@big_spoon_test.i_might_accept_one
|
@@ -155,7 +151,7 @@ describe BigSpoon do
|
|
155
151
|
BigSpoonTest.after :raise_sommat, :hook_12
|
156
152
|
begin
|
157
153
|
@big_spoon_test.raise_sommat
|
158
|
-
rescue
|
154
|
+
rescue
|
159
155
|
$!.backtrace.first.should =~ /:\d+\:in `raise_sommat'/
|
160
156
|
end
|
161
157
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: big_spoon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
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: 2012-08-
|
12
|
+
date: 2012-08-20 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! "\n BigSpoon will add a hooks method to every class. Call that
|
15
15
|
method with a block and add all kinds of fun hooks before and after your methods.\n
|
@@ -49,7 +49,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
49
49
|
version: '0'
|
50
50
|
segments:
|
51
51
|
- 0
|
52
|
-
hash:
|
52
|
+
hash: 2773944303666380351
|
53
53
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
54
|
none: false
|
55
55
|
requirements:
|