hooks 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,8 @@
1
+ h2. 0.2.0
2
+
3
+ h3. Changes
4
+ * Callback blocks are now executed on the instance using @instance_exec@. If you need to access the class (former context) use @self.class@.
5
+
1
6
  h2. 0.1.4
2
7
 
3
8
  h3. Bugfixes
data/Gemfile CHANGED
@@ -1 +1,3 @@
1
+ source :rubygems
2
+
1
3
  gemspec
@@ -5,54 +5,79 @@
5
5
 
6
6
  == Introduction
7
7
 
8
- _Hooks_ lets you define hooks declaratively in your ruby class. You can add callbacks to your hook, which will be
9
- run as soon as _you_ run the hook!
8
+ _Hooks_ lets you define hooks declaratively in your ruby class. You can add callbacks to your hook, which will be run as soon as _you_ run the hook!
10
9
 
11
10
  It's almost like ActiveSupport::Callbacks but 76,6% less complex. Instead, it is not more than 60 lines of code, one method compilation, no +method_missing+ and no magic.
12
11
 
12
+ Also, you may pass additional arguments to your callbacks when invoking a hook.
13
13
 
14
14
  == Example
15
15
 
16
16
  Let's take... a cat.
17
+
18
+ require 'hooks'
17
19
 
18
20
  class Cat
19
21
  include Hooks
20
-
22
+
21
23
  define_hook :after_dinner
22
24
 
23
25
  Now you can add callbacks to your hook declaratively in your class.
24
26
 
25
- after_dinner do
26
- puts "Ice cream!"
27
+ after_dinner do
28
+ puts "Ice cream for #{self}!"
27
29
  end
28
-
30
+
29
31
  after_dinner :have_a_desert # => refers to Cat#have_a_desert
30
-
31
-
32
+
32
33
  def have_a_desert
33
34
  puts "Hell, yeah!"
34
35
  end
35
-
36
- Running the callbacks happens on instances. It will run the block and #have_a_desert from above.
36
+
37
+ This will run the block and <tt>#have_a_desert</tt> from above.
37
38
 
38
39
  cat.run_hook :after_dinner
39
- # => Ice cream!
40
+ # => Ice cream for #<Cat:0x8df9d84>!
41
+ Hell, yeah!
42
+
43
+ Callback blocks and methods will be executed with instance context. Note how +self+ in the block refers to the Cat instance.
44
+
45
+
46
+ == Inheritance
47
+
48
+ Hooks are inherited, here's a complete example to put it all together.
49
+
50
+ class Garfield < Cat
51
+
52
+ after_dinner :want_some_more
53
+
54
+ def want_some_more
55
+ puts "Is that all?"
56
+ end
57
+ end
58
+
59
+
60
+ Garfield.new.run_hook :after_dinner
61
+ # => Ice cream for #<Cat:0x8df9d84>!
40
62
  Hell, yeah!
63
+ Is that all?
64
+
65
+ Note how the callbacks are invoked in the order they were inherited.
41
66
 
42
67
 
43
- == Options
68
+ == Options for Callbacks
44
69
 
45
- You're free to pass any number of arguments to #run_callback.
70
+ You're free to pass any number of arguments to #run_callback, those will be passed to the callbacks.
46
71
 
47
72
  cat.run_hook :before_dinner, cat, Time.now
48
73
 
49
74
  The callbacks should be ready for receiving parameters.
50
-
75
+
51
76
  before_dinner :wash_pawns
52
77
  before_dinner do |who, when|
53
78
  ...
54
79
  end
55
-
80
+
56
81
  def wash_pawns(who, when)
57
82
 
58
83
 
@@ -64,13 +89,6 @@ Not sure why a cat should have ice cream for dinner. Beside that, I was tempted
64
89
  gem install hooks
65
90
 
66
91
 
67
- == Dependencies
68
-
69
- The current gem requires
70
-
71
- * active_support 2.3.x
72
-
73
-
74
92
  == Anybody using it?
75
93
 
76
94
  * Hooks is already used in [Apotomo:http://github.com/apotonick/apotomo], a hot widget framework for Rails. Look at +lib/apotomo/widget.rb+ for examples and into +lib/apotomo/tree_node.rb+ to learn how modules-driven code might benefit from hooks.
@@ -84,6 +102,6 @@ The current gem requires
84
102
 
85
103
  == License
86
104
 
87
- Copyright (c) 2010, Nick Sutterer
105
+ Copyright (c) 2010, Nick Sutterer
88
106
 
89
107
  Released under the MIT License.
@@ -18,4 +18,5 @@ Gem::Specification.new do |s|
18
18
  s.require_paths = ["lib"]
19
19
 
20
20
  s.add_development_dependency "shoulda"
21
+ s.add_development_dependency "rake"
21
22
  end
@@ -16,7 +16,7 @@ require "hooks/inheritable_attribute"
16
16
  #
17
17
  # cat.run_hook :after_dinner
18
18
  module Hooks
19
- VERSION = "0.1.4"
19
+ VERSION = "0.2.0"
20
20
 
21
21
  def self.included(base)
22
22
  base.extend InheritableAttribute
@@ -50,7 +50,7 @@ module Hooks
50
50
  if callback.kind_of? Symbol
51
51
  scope.send(callback, *args)
52
52
  else
53
- callback.call(*args)
53
+ scope.instance_exec(*args, &callback)
54
54
  end
55
55
  end
56
56
  end
@@ -1,15 +1,18 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class HooksTest < Test::Unit::TestCase
4
+ class TestClass
5
+ include Hooks
6
+
7
+ def executed
8
+ @executed ||= [];
9
+ end
10
+ end
11
+
12
+
4
13
  context "Hooks.define_hook" do
5
14
  setup do
6
- @klass = Class.new(Object) do
7
- include Hooks
8
-
9
- def executed
10
- @executed ||= [];
11
- end
12
- end
15
+ @klass = Class.new(TestClass)
13
16
 
14
17
  @mum = @klass.new
15
18
  @mum.class.define_hook :after_eight
@@ -72,6 +75,12 @@ class HooksTest < Test::Unit::TestCase
72
75
 
73
76
  assert_equal [2, 0], @mum.executed
74
77
  end
78
+
79
+ should "execute block callbacks in instance context" do
80
+ @mum.class.after_eight { executed << :c }
81
+ @mum.run_hook(:after_eight)
82
+ assert_equal [:c], @mum.executed
83
+ end
75
84
  end
76
85
 
77
86
  context "in class context" do
@@ -100,4 +109,33 @@ class HooksTest < Test::Unit::TestCase
100
109
  end
101
110
  end
102
111
  end
112
+
113
+ context "Deriving" do
114
+ setup do
115
+ @klass = Class.new(TestClass)
116
+
117
+ @mum = @klass.new
118
+ @mum.class.define_hook :after_eight
119
+ end
120
+
121
+ should "inherit the hook" do
122
+ @klass.class_eval do
123
+ after_eight :take_shower
124
+
125
+ def take_shower
126
+ executed << :take_shower
127
+ end
128
+ end
129
+
130
+ @kid = Class.new(@klass) do
131
+ after_eight :have_dinner
132
+
133
+ def have_dinner
134
+ executed << :have_dinner
135
+ end
136
+ end.new
137
+
138
+ assert_equal [:take_shower, :have_dinner], @kid.run_hook(:after_eight)
139
+ end
140
+ end
103
141
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 4
9
- version: 0.1.4
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Nick Sutterer
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-05-16 00:00:00 +02:00
17
+ date: 2011-10-06 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -30,6 +30,19 @@ dependencies:
30
30
  version: "0"
31
31
  type: :development
32
32
  version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :development
45
+ version_requirements: *id002
33
46
  description: Declaratively define hooks, add callbacks and run them with the options you like.
34
47
  email:
35
48
  - apotonick@gmail.com