uberhook 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Uberhook
2
2
 
3
+ [![Build Status](https://secure.travis-ci.org/mjio/uberhook.png)](http://travis-ci.org/mjio/uberhook)
4
+
3
5
  The Uberhook gem allows you to define `before` and `after` hooks for your classes and models.
4
6
 
5
7
  ## Installation
data/lib/uberhook.rb CHANGED
@@ -48,9 +48,16 @@ module Uberhook
48
48
 
49
49
  def define_hook(type, target, callback)
50
50
  alias_method "#{target}_without_callbacks", target
51
- define_method target do
52
- send callback
53
- send "#{target}_without_callbacks"
51
+ if type == :pre
52
+ define_method target do
53
+ send callback
54
+ send "#{target}_without_callbacks"
55
+ end
56
+ elsif type == :post
57
+ define_method target do
58
+ send "#{target}_without_callbacks"
59
+ send callback
60
+ end
54
61
  end
55
62
  end
56
63
 
@@ -3,9 +3,10 @@ require 'test_helper'
3
3
  class UberhookTest < Uberhook::TestCase
4
4
  def setup
5
5
  @car = Class.new do
6
+ attr_reader :status
6
7
  include Uberhook
7
- def drive; end
8
- def start; end
8
+ def drive; @status = :drive; end
9
+ def start; @status = :start; end
9
10
  def stop; end
10
11
  end
11
12
  end
@@ -16,7 +17,7 @@ class UberhookTest < Uberhook::TestCase
16
17
  before :drive, :start
17
18
  end
18
19
  end
19
-
20
+
20
21
  test 'should register :post hook' do
21
22
  @car.expects(:register_hook).with(:post, :drive, :stop).once
22
23
  @car.class_eval do
@@ -32,41 +33,39 @@ class UberhookTest < Uberhook::TestCase
32
33
  def two; end
33
34
  end
34
35
  end
35
-
36
+
36
37
  test 'should not define :pre hook when target is not defined' do
37
38
  @car.expects(:define_hook).times(0)
38
39
  @car.class_eval do
39
40
  register_hook :pre, :two, :one
40
41
  end
41
42
  end
42
-
43
+
43
44
  test 'should not define :post hook when target is not defined' do
44
45
  @car.expects(:define_hook).times(0)
45
46
  @car.class_eval do
46
47
  register_hook :post, :one, :two
47
48
  end
48
49
  end
49
-
50
+
50
51
  test 'should run callback before the hook method' do
51
52
  @car.class_eval do
52
53
  register_hook :pre, :drive, :start
53
54
  end
54
55
  beatle = @car.new
55
- # TODO how to test before call
56
56
  beatle.expects(:start).once
57
57
  beatle.drive
58
58
  end
59
-
59
+
60
60
  test 'should run callback after the hook method' do
61
61
  @car.class_eval do
62
- register_hook :post, :drive, :start
62
+ register_hook :post, :start, :drive
63
63
  end
64
64
  beatle = @car.new
65
- # TODO how to test after call
66
- beatle.expects(:start).once
67
- beatle.drive
65
+ beatle.expects(:drive).once
66
+ beatle.start
68
67
  end
69
-
68
+
70
69
  test 'should run :pre callback when hook method gets defined' do
71
70
  @car.class_eval do
72
71
  register_hook :pre, :two, :one
@@ -74,11 +73,21 @@ class UberhookTest < Uberhook::TestCase
74
73
  def two; end
75
74
  end
76
75
  beatle = @car.new
77
- # TODO how to test before call
78
76
  beatle.expects(:one).once
79
77
  beatle.two
80
78
  end
81
-
79
+
80
+ test 'should run :pre callback after hook method gets defined' do
81
+ @car.class_eval do
82
+ register_hook :pre, :two, :one
83
+ def one; @status = :one; end
84
+ def two; @status = :two; end
85
+ end
86
+ beatle = @car.new
87
+ beatle.two
88
+ assert_equal :two, beatle.status
89
+ end
90
+
82
91
  test 'should run :post callback when hook method gets defined' do
83
92
  @car.class_eval do
84
93
  register_hook :post, :one, :two
@@ -86,8 +95,18 @@ class UberhookTest < Uberhook::TestCase
86
95
  def two; end
87
96
  end
88
97
  beatle = @car.new
89
- # TODO how to test after call
90
98
  beatle.expects(:two).once
91
99
  beatle.one
92
100
  end
101
+
102
+ test 'should run :post callback after hook method gets defined' do
103
+ @car.class_eval do
104
+ register_hook :post, :one, :two
105
+ def one; @status = :one; end
106
+ def two; @status = :two; end
107
+ end
108
+ beatle = @car.new
109
+ beatle.one
110
+ assert_equal :two, beatle.status
111
+ end
93
112
  end
data/uberhook.gemspec CHANGED
@@ -1,14 +1,14 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = 'uberhook'
3
- gem.version = '0.0.1'
3
+ gem.version = '0.0.2'
4
4
  gem.authors = ['Martin Jagusch']
5
5
  gem.email = ['_@mj.io']
6
6
  gem.description = 'Allows you to define :before and :after hooks for your classes and models.'
7
7
  gem.summary = ':before and :after hooks'
8
8
  gem.homepage = 'https://github.com/mjio/uberhook'
9
9
 
10
- gem.add_development_dependency 'rake', '~> 0.9.2.2'
11
- gem.add_development_dependency 'mocha', '~> 0.12.1'
10
+ gem.add_development_dependency 'rake'
11
+ gem.add_development_dependency 'mocha'
12
12
 
13
13
  gem.files = `git ls-files`.split($/)
14
14
  gem.test_files = gem.files.grep(%r{^test/})
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uberhook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,40 +9,40 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-02 00:00:00.000000000 Z
12
+ date: 2012-08-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ~>
19
+ - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 0.9.2.2
21
+ version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - ~>
27
+ - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: 0.9.2.2
29
+ version: '0'
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: mocha
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
- - - ~>
35
+ - - ! '>='
36
36
  - !ruby/object:Gem::Version
37
- version: 0.12.1
37
+ version: '0'
38
38
  type: :development
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - ~>
43
+ - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
- version: 0.12.1
45
+ version: '0'
46
46
  description: Allows you to define :before and :after hooks for your classes and models.
47
47
  email:
48
48
  - _@mj.io