uberhook 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Martin Jagusch
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # Uberhook
2
+
3
+ The Uberhook gem allows you to define `before` and `after` hooks for your classes and models.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```
10
+ gem 'uberhook'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```
22
+ $ gem install uberhook
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ``` ruby
28
+ require 'uberhook'
29
+
30
+ class Car
31
+ include Uberhook
32
+
33
+ before :drive, :start_engine
34
+
35
+ def drive
36
+ puts 'Now lets drive.'
37
+ end
38
+
39
+ def start_engine
40
+ puts 'First start the engine.'
41
+ end
42
+ end
43
+
44
+ beatle = Car.new
45
+ beatle.drive
46
+
47
+ # Output:
48
+ # First start the engine.
49
+ # And now lets drive.
50
+ ```
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ task :default => :test
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'test'
8
+ t.pattern = 'test/**/*_test.rb'
9
+ t.warning = true
10
+ t.verbose = true
11
+ end
data/lib/uberhook.rb ADDED
@@ -0,0 +1,81 @@
1
+ # Include the Uberhook module to define 'before' and 'after' hooks
2
+ # for your classes and models.
3
+ #
4
+ # class Car
5
+ # include Uberhook
6
+ #
7
+ # before :drive, :start_engine
8
+ #
9
+ # def drive
10
+ # puts 'Now lets drive.'
11
+ # end
12
+ #
13
+ # def start_engine
14
+ # puts 'First start the engine.'
15
+ # end
16
+ # end
17
+ #
18
+ # beatle = Car.new
19
+ # beatle.drive
20
+ #
21
+ # Output:
22
+ # First start the engine.
23
+ # Now lets drive.
24
+ module Uberhook
25
+ def self.included(base)
26
+ base.extend ClassMethods
27
+ base.instance_variable_set :@hooks, {}
28
+ end
29
+
30
+ module ClassMethods
31
+ def before(target, callback)
32
+ register_hook :pre, target, callback
33
+ end
34
+
35
+ def after(target, callback)
36
+ register_hook :post, target, callback
37
+ end
38
+
39
+ def register_hook(type, target, callback)
40
+ if method_defined? target
41
+ define_hook(type, target, callback)
42
+ else
43
+ add_hook_to_waitinglist(type, target, callback)
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def define_hook(type, target, callback)
50
+ alias_method "#{target}_without_callbacks", target
51
+ define_method target do
52
+ send callback
53
+ send "#{target}_without_callbacks"
54
+ end
55
+ end
56
+
57
+ def add_hook_to_waitinglist(type, target, callback)
58
+ @hooks[type] = {} unless @hooks[type].kind_of? Hash
59
+ callbacks = @hooks[type][target]
60
+ @hooks[type][target] = (callbacks || []) << callback
61
+ end
62
+
63
+ def remove_hook_from_waitinglist(type, target, callback)
64
+ @hooks[type][target].delete callback
65
+ end
66
+
67
+ def perform_method_added(name)
68
+ @hooks.each do |type, hook|
69
+ next unless hook[name]
70
+ hook[name].each do |callback|
71
+ remove_hook_from_waitinglist(type, name, callback)
72
+ define_hook(type, name, callback)
73
+ end
74
+ end
75
+ end
76
+
77
+ def method_added(name)
78
+ perform_method_added(name)
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,10 @@
1
+ require 'test/unit'
2
+ require 'mocha'
3
+
4
+ require 'uberhook'
5
+
6
+ class Uberhook::TestCase < Test::Unit::TestCase
7
+ def self.test(name, &block)
8
+ define_method("test #{name.inspect}", &block)
9
+ end
10
+ end
@@ -0,0 +1,93 @@
1
+ require 'test_helper'
2
+
3
+ class UberhookTest < Uberhook::TestCase
4
+ def setup
5
+ @car = Class.new do
6
+ include Uberhook
7
+ def drive; end
8
+ def start; end
9
+ def stop; end
10
+ end
11
+ end
12
+
13
+ test 'should register :pre hook' do
14
+ @car.expects(:register_hook).with(:pre, :drive, :start).once
15
+ @car.class_eval do
16
+ before :drive, :start
17
+ end
18
+ end
19
+
20
+ test 'should register :post hook' do
21
+ @car.expects(:register_hook).with(:post, :drive, :stop).once
22
+ @car.class_eval do
23
+ after :drive, :stop
24
+ end
25
+ end
26
+
27
+ test 'should save :pre hook temporally when target is not defined' do
28
+ @car.expects(:add_hook_to_waitinglist).with(:pre, :two, :one).once
29
+ @car.class_eval do
30
+ register_hook :pre, :two, :one
31
+ def one; end
32
+ def two; end
33
+ end
34
+ end
35
+
36
+ test 'should not define :pre hook when target is not defined' do
37
+ @car.expects(:define_hook).times(0)
38
+ @car.class_eval do
39
+ register_hook :pre, :two, :one
40
+ end
41
+ end
42
+
43
+ test 'should not define :post hook when target is not defined' do
44
+ @car.expects(:define_hook).times(0)
45
+ @car.class_eval do
46
+ register_hook :post, :one, :two
47
+ end
48
+ end
49
+
50
+ test 'should run callback before the hook method' do
51
+ @car.class_eval do
52
+ register_hook :pre, :drive, :start
53
+ end
54
+ beatle = @car.new
55
+ # TODO how to test before call
56
+ beatle.expects(:start).once
57
+ beatle.drive
58
+ end
59
+
60
+ test 'should run callback after the hook method' do
61
+ @car.class_eval do
62
+ register_hook :post, :drive, :start
63
+ end
64
+ beatle = @car.new
65
+ # TODO how to test after call
66
+ beatle.expects(:start).once
67
+ beatle.drive
68
+ end
69
+
70
+ test 'should run :pre callback when hook method gets defined' do
71
+ @car.class_eval do
72
+ register_hook :pre, :two, :one
73
+ def one; end
74
+ def two; end
75
+ end
76
+ beatle = @car.new
77
+ # TODO how to test before call
78
+ beatle.expects(:one).once
79
+ beatle.two
80
+ end
81
+
82
+ test 'should run :post callback when hook method gets defined' do
83
+ @car.class_eval do
84
+ register_hook :post, :one, :two
85
+ def one; end
86
+ def two; end
87
+ end
88
+ beatle = @car.new
89
+ # TODO how to test after call
90
+ beatle.expects(:two).once
91
+ beatle.one
92
+ end
93
+ end
data/uberhook.gemspec ADDED
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = 'uberhook'
3
+ gem.version = '0.0.1'
4
+ gem.authors = ['Martin Jagusch']
5
+ gem.email = ['_@mj.io']
6
+ gem.description = 'Allows you to define :before and :after hooks for your classes and models.'
7
+ gem.summary = ':before and :after hooks'
8
+ gem.homepage = 'https://github.com/mjio/uberhook'
9
+
10
+ gem.add_development_dependency 'rake', '~> 0.9.2.2'
11
+ gem.add_development_dependency 'mocha', '~> 0.12.1'
12
+
13
+ gem.files = `git ls-files`.split($/)
14
+ gem.test_files = gem.files.grep(%r{^test/})
15
+ gem.require_paths = ['lib']
16
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uberhook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Martin Jagusch
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.9.2.2
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.9.2.2
30
+ - !ruby/object:Gem::Dependency
31
+ name: mocha
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.12.1
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.12.1
46
+ description: Allows you to define :before and :after hooks for your classes and models.
47
+ email:
48
+ - _@mj.io
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .travis.yml
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - lib/uberhook.rb
60
+ - test/test_helper.rb
61
+ - test/uberhook_test.rb
62
+ - uberhook.gemspec
63
+ homepage: https://github.com/mjio/uberhook
64
+ licenses: []
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 1.8.24
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: ':before and :after hooks'
87
+ test_files:
88
+ - test/test_helper.rb
89
+ - test/uberhook_test.rb