hook 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/hook.rb +57 -0
- data/readme.markdown +94 -0
- data/specdown/readme.specdown +47 -0
- data/specdown/support/env.rb +3 -0
- metadata +47 -12
data/lib/hook.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
module Hook
|
2
|
+
def self.included(base)
|
3
|
+
base.extend ::Hook::ClassMethods
|
4
|
+
end
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def hook(method)
|
8
|
+
self.instance_eval <<-HOOKS
|
9
|
+
def before_#{method}(&block)
|
10
|
+
before_hooks["#{method}"] << block
|
11
|
+
end
|
12
|
+
|
13
|
+
def after_#{method}(&block)
|
14
|
+
after_hooks["#{method}"] << block
|
15
|
+
end
|
16
|
+
|
17
|
+
def around_#{method}(&block)
|
18
|
+
around_hooks["#{method}"] << block
|
19
|
+
end
|
20
|
+
HOOKS
|
21
|
+
end
|
22
|
+
|
23
|
+
def before_hooks
|
24
|
+
@before_hooks ||= scoped_callbacks
|
25
|
+
end
|
26
|
+
|
27
|
+
def after_hooks
|
28
|
+
@after_hooks ||= scoped_callbacks
|
29
|
+
end
|
30
|
+
|
31
|
+
def around_hooks
|
32
|
+
@around_hooks ||= scoped_callbacks
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
def scoped_callbacks
|
37
|
+
Hash.new {|h,k| h[k] = []}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def with_hooks(method)
|
43
|
+
method = method.to_s
|
44
|
+
|
45
|
+
run_hooks self.class.around_hooks[method]
|
46
|
+
run_hooks self.class.before_hooks[method]
|
47
|
+
yield
|
48
|
+
run_hooks self.class.after_hooks[method]
|
49
|
+
run_hooks self.class.around_hooks[method]
|
50
|
+
|
51
|
+
nil
|
52
|
+
end
|
53
|
+
|
54
|
+
def run_hooks(hooks)
|
55
|
+
hooks.each {|hook| hook.call self}
|
56
|
+
end
|
57
|
+
end
|
data/readme.markdown
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# Hook
|
2
|
+
|
3
|
+
A simple API for adding before, after, and around callbacks to methods.
|
4
|
+
|
5
|
+
Think of it as `ActiveSupport::Callbacks`Lite.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
It's a gem. Install it:
|
10
|
+
|
11
|
+
$ gem install hook
|
12
|
+
|
13
|
+
Or add it to your Gemfile:
|
14
|
+
|
15
|
+
gem "hook"
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
Imagine you've created a testing framework, and it includes a `Test`
|
20
|
+
class with an `execute` method:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
class Test
|
24
|
+
#...
|
25
|
+
def execute
|
26
|
+
#...
|
27
|
+
end
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
You'll likely want to offer hooks into the execution lifecycle of your
|
32
|
+
tests. First, include `Hook` into your class, then call `hook :execute`:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
class Test
|
36
|
+
include ::Hook
|
37
|
+
hook :execute
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
Next, you'll need to wrap the body of the `execute` method with a call
|
42
|
+
to `with_hooks(:execute)`:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
class Test
|
46
|
+
include ::Hook
|
47
|
+
hook :execute
|
48
|
+
|
49
|
+
def execute
|
50
|
+
with_hooks :execute do
|
51
|
+
#...
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
Now, **anyone can hook into the lifecycle of a test**:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
Test.before_execute do |test|
|
61
|
+
#...
|
62
|
+
end
|
63
|
+
|
64
|
+
Test.after_execute do |test|
|
65
|
+
#...
|
66
|
+
end
|
67
|
+
|
68
|
+
Test.around_execute do |test|
|
69
|
+
#...
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
Notice that our blocks accepted a `test` parameter. This is an instance
|
74
|
+
of the `Test` object that `execute` was called on.
|
75
|
+
|
76
|
+
Basically, whenever anyone calls the `execute` method on a test
|
77
|
+
instance, here's what happens:
|
78
|
+
|
79
|
+
around execute hooks run
|
80
|
+
before execute hooks run
|
81
|
+
execute method runs
|
82
|
+
after execute hooks run
|
83
|
+
around execute hooks run
|
84
|
+
|
85
|
+
|
86
|
+
##LICENSE
|
87
|
+
|
88
|
+
This software is public domain. Since I've relinquished copyright, you can do anything with it. GO WILD.
|
89
|
+
|
90
|
+
##Contributing
|
91
|
+
|
92
|
+
Submit a pull request. Tests required. Also, by submitting a pull
|
93
|
+
request, you are relinquishing your copyright over your contribution and
|
94
|
+
submitting it to the public domain.
|
@@ -0,0 +1,47 @@
|
|
1
|
+
Hook Details
|
2
|
+
============================================
|
3
|
+
|
4
|
+
|
5
|
+
anyone can hook into the lifecycle of a test
|
6
|
+
--------------------------------------------
|
7
|
+
|
8
|
+
You can create before hooks that execute before a test is executed:
|
9
|
+
|
10
|
+
before = false
|
11
|
+
|
12
|
+
Test.before_execute do |test|
|
13
|
+
before = true
|
14
|
+
end
|
15
|
+
|
16
|
+
Test.new.execute
|
17
|
+
|
18
|
+
before.should be(true)
|
19
|
+
|
20
|
+
You can create after hooks that execute after a test is execute:
|
21
|
+
|
22
|
+
after = false
|
23
|
+
|
24
|
+
Test.after_execute do |test|
|
25
|
+
after = true
|
26
|
+
end
|
27
|
+
|
28
|
+
Test.new.execute
|
29
|
+
|
30
|
+
after.should be(true)
|
31
|
+
|
32
|
+
And lastly, you can create around hooks that execute before _and_ after
|
33
|
+
a test executes:
|
34
|
+
|
35
|
+
around = nil
|
36
|
+
|
37
|
+
Test.around_execute do |test|
|
38
|
+
if around.nil?
|
39
|
+
around = "before"
|
40
|
+
else
|
41
|
+
around = "after"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
Test.new.execute
|
46
|
+
|
47
|
+
around.should == "after"
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hook
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Matt Parker
|
@@ -15,10 +15,41 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
19
|
-
dependencies:
|
20
|
-
|
21
|
-
|
18
|
+
date: 2012-01-29 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: specdown
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: -726738688
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
- 4
|
32
|
+
- 0
|
33
|
+
- beta
|
34
|
+
- 1
|
35
|
+
version: 0.4.0.beta.1
|
36
|
+
type: :development
|
37
|
+
version_requirements: *id001
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: rspec
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 3
|
47
|
+
segments:
|
48
|
+
- 0
|
49
|
+
version: "0"
|
50
|
+
type: :development
|
51
|
+
version_requirements: *id002
|
52
|
+
description:
|
22
53
|
email: moonmaster9000@gmail.com
|
23
54
|
executables: []
|
24
55
|
|
@@ -26,8 +57,11 @@ extensions: []
|
|
26
57
|
|
27
58
|
extra_rdoc_files: []
|
28
59
|
|
29
|
-
files:
|
30
|
-
|
60
|
+
files:
|
61
|
+
- lib/hook.rb
|
62
|
+
- readme.markdown
|
63
|
+
- specdown/readme.specdown
|
64
|
+
- specdown/support/env.rb
|
31
65
|
homepage: http://github.com/moonmaster9000/hook
|
32
66
|
licenses: []
|
33
67
|
|
@@ -60,6 +94,7 @@ rubyforge_project:
|
|
60
94
|
rubygems_version: 1.8.10
|
61
95
|
signing_key:
|
62
96
|
specification_version: 3
|
63
|
-
summary:
|
64
|
-
test_files:
|
65
|
-
|
97
|
+
summary: A simple, lightweight system for adding lifecycle callbacks to your objects.
|
98
|
+
test_files:
|
99
|
+
- specdown/readme.specdown
|
100
|
+
- specdown/support/env.rb
|