hook 0.0.2 → 0.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.
@@ -1,32 +1,56 @@
1
+ require 'method_decorators'
2
+
1
3
  module Hook
2
4
  def self.included(base)
3
5
  base.extend ::Hook::ClassMethods
6
+ base.extend ::MethodDecorators
7
+ end
8
+
9
+ class HookDecorator < MethodDecorator
10
+ def call(wrapped, *args, &block)
11
+ method_name = wrapped.name.to_s
12
+ receiver = wrapped.receiver
13
+ owner = wrapped.owner
14
+
15
+ run_hooks owner.around_hooks[method_name], receiver
16
+ run_hooks owner.before_hooks[method_name], receiver
17
+ return_value = wrapped.call *args, &block
18
+ run_hooks owner.after_hooks[method_name], receiver
19
+ run_hooks owner.around_hooks[method_name], receiver
20
+
21
+ return_value
22
+ end
23
+
24
+ private
25
+ def run_hooks(hooks, object)
26
+ hooks.each {|hook| hook.call object}
27
+ end
4
28
  end
5
29
 
6
30
  module ClassMethods
7
- def hook(method)
8
- self.instance_eval <<-HOOKS
9
- def before_#{method}(&block)
10
- before_hooks["#{method}"] << block
11
- end
31
+ def hook
32
+ ::Hook::HookDecorator
33
+ end
12
34
 
13
- def after_#{method}(&block)
14
- after_hooks["#{method}"] << block
15
- end
35
+ def hook_before method, &block
36
+ before_hooks[method.to_s] << block
37
+ end
16
38
 
17
- def around_#{method}(&block)
18
- around_hooks["#{method}"] << block
19
- end
39
+ def hook_after method, &block
40
+ after_hooks[method.to_s] << block
41
+ end
20
42
 
21
- def remove_#{method}_callbacks!
22
- [before_hooks, after_hooks, around_hooks].each do |hooks|
23
- hooks["#{method}"] = []
24
- end
25
- end
26
- HOOKS
43
+ def hook_around method, &block
44
+ around_hooks[method.to_s] << block
27
45
  end
28
46
 
29
- def remove_callbacks!
47
+ def remove_callbacks! method
48
+ [before_hooks, after_hooks, around_hooks].each do |hooks|
49
+ hooks[method.to_s] = []
50
+ end
51
+ end
52
+
53
+ def remove_all_callbacks!
30
54
  @before_hooks = nil
31
55
  @after_hooks = nil
32
56
  @around_hooks = nil
@@ -49,21 +73,4 @@ module Hook
49
73
  Hash.new {|h,k| h[k] = []}
50
74
  end
51
75
  end
52
-
53
- private
54
- def with_hooks(method)
55
- method = method.to_s
56
-
57
- run_hooks self.class.around_hooks[method]
58
- run_hooks self.class.before_hooks[method]
59
- yield
60
- run_hooks self.class.after_hooks[method]
61
- run_hooks self.class.around_hooks[method]
62
-
63
- nil
64
- end
65
-
66
- def run_hooks(hooks)
67
- hooks.each {|hook| hook.call self}
68
- end
69
76
  end
@@ -1,5 +1,7 @@
1
1
  # Hook
2
2
 
3
+ [![Build Status](https://secure.travis-ci.org/moonmaster9000/hook.png)](http://travis-ci.org/moonmaster9000/hook)
4
+
3
5
  A simple API for adding before, after, and around callbacks to methods.
4
6
 
5
7
  Think of it as `ActiveSupport::Callbacks`Lite.
@@ -17,40 +19,17 @@ Or add it to your Gemfile:
17
19
  ## Usage
18
20
 
19
21
  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
- ```
22
+ class with an `execute` method.
30
23
 
31
24
  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`:
25
+ tests. First, include `Hook` into your class, then decorate any methods you wish to hook with `+hook`:
33
26
 
34
27
  ```ruby
35
28
  class Test
36
- include ::Hook
37
- hook :execute
38
- end
39
- ```
29
+ include Hook
40
30
 
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
31
+ +hook
32
+ def execute; end
54
33
  end
55
34
  ```
56
35
 
metadata CHANGED
@@ -1,100 +1,68 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: hook
3
- version: !ruby/object:Gem::Version
4
- hash: 27
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 2
10
- version: 0.0.2
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Matt Parker
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
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
12
+ date: 2012-06-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: method_decorators
16
+ requirement: &70329024742560 !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
18
+ requirements:
26
19
  - - ~>
27
- - !ruby/object:Gem::Version
28
- hash: -2344376328
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
20
+ - !ruby/object:Gem::Version
21
+ version: 0.9.0
22
+ type: :runtime
40
23
  prerelease: false
41
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *70329024742560
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70329024742120 !ruby/object:Gem::Requirement
42
28
  none: false
43
- requirements:
44
- - - ">="
45
- - !ruby/object:Gem::Version
46
- hash: 3
47
- segments:
48
- - 0
49
- version: "0"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
50
33
  type: :development
51
- version_requirements: *id002
34
+ prerelease: false
35
+ version_requirements: *70329024742120
52
36
  description:
53
37
  email: moonmaster9000@gmail.com
54
38
  executables: []
55
-
56
39
  extensions: []
57
-
58
40
  extra_rdoc_files: []
59
-
60
- files:
41
+ files:
61
42
  - lib/hook.rb
62
43
  - readme.markdown
63
- - specdown/readme.specdown
64
- - specdown/support/env.rb
65
44
  homepage: http://github.com/moonmaster9000/hook
66
45
  licenses: []
67
-
68
46
  post_install_message:
69
47
  rdoc_options: []
70
-
71
- require_paths:
48
+ require_paths:
72
49
  - lib
73
- required_ruby_version: !ruby/object:Gem::Requirement
50
+ required_ruby_version: !ruby/object:Gem::Requirement
74
51
  none: false
75
- requirements:
76
- - - ">="
77
- - !ruby/object:Gem::Version
78
- hash: 3
79
- segments:
80
- - 0
81
- version: "0"
82
- required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
57
  none: false
84
- requirements:
85
- - - ">="
86
- - !ruby/object:Gem::Version
87
- hash: 3
88
- segments:
89
- - 0
90
- version: "0"
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
91
62
  requirements: []
92
-
93
63
  rubyforge_project:
94
- rubygems_version: 1.8.10
64
+ rubygems_version: 1.8.17
95
65
  signing_key:
96
66
  specification_version: 3
97
67
  summary: A simple, lightweight system for adding lifecycle callbacks to your objects.
98
- test_files:
99
- - specdown/readme.specdown
100
- - specdown/support/env.rb
68
+ test_files: []
@@ -1,78 +0,0 @@
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"
48
-
49
-
50
-
51
- remove all before, after, and around execute callbacks
52
- ------------------------------------------------------
53
-
54
- Test.around_hooks["execute"].should_not be_empty
55
- Test.before_hooks["execute"].should_not be_empty
56
- Test.after_hooks["execute"].should_not be_empty
57
-
58
- Test.remove_execute_callbacks!
59
-
60
- Test.around_hooks["execute"].should be_empty
61
- Test.before_hooks["execute"].should be_empty
62
- Test.after_hooks["execute"].should be_empty
63
-
64
-
65
- remove all configured callbacks for all hooked methods
66
- ------------------------------------------------------
67
-
68
- Test.around_hooks["execute"].should_not be_empty
69
- Test.before_hooks["execute"].should_not be_empty
70
- Test.after_hooks["execute"].should_not be_empty
71
-
72
- Test.remove_callbacks!
73
-
74
- Test.around_hooks["execute"].should be_empty
75
- Test.before_hooks["execute"].should be_empty
76
- Test.after_hooks["execute"].should be_empty
77
-
78
-
@@ -1,3 +0,0 @@
1
- $LOAD_PATH.unshift './lib'
2
-
3
- require 'hook'