hook_me_up 0.0.1

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.
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/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## v0.0.1
2
+
3
+ * initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hook_me_up.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Chen Fisher
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,118 @@
1
+
2
+ # HookMeUp
3
+
4
+ This gem lets you create hook methods for any method of any class.
5
+ You can create a `:before` and `:after` hooks for any method you like.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'hook_me_up'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install hook_me_up
20
+
21
+ ## Usage
22
+
23
+ 1. Include HookMeUp in your class
24
+ 2. call `hook_me_up` with a method name or array of methods and specify `:before` hook, `:after` hook or both.
25
+
26
+ NOTE: `hook_me_up` call **must** come after your method definitions
27
+
28
+
29
+ class SomeClass
30
+ include HookMeUp
31
+
32
+ def some_method
33
+ end
34
+
35
+ def some_other_method
36
+ end
37
+
38
+ def before_hook
39
+ end
40
+
41
+ def after_hook
42
+ end
43
+
44
+ hook_me_up [:some_method, :some_other_method], :before => :before_hook, :after => :after_hook
45
+ end
46
+
47
+
48
+ ###You can pass lambda to the hooks instead of methods
49
+
50
+ hook_me_up :some_method, :before => lambda{ |sender, *args| sender.do_something(args) },
51
+ :after => lambda{ |sender, *args, result| sender.do_something_else(result) }
52
+
53
+ ## Arguments
54
+
55
+ ### When called with method names
56
+ The `:before` hook is passed with an optional `*args`, which are the arguments passed to the original method:
57
+
58
+ def before(*args)
59
+ end
60
+
61
+ The `:after` hook is passed with optional two arguments:
62
+
63
+ def after(*args, result)
64
+ end
65
+
66
+ Where `result` is the result of the original method
67
+
68
+ ### When called with lamdba
69
+ There is an additional, mandatory, argument: **sender**, which is the class instance of the original method:
70
+
71
+ hook_me_up :some_method, :before => lambda{ |sender, *args| ... },
72
+ :after => lambda{ |sender, @args, result| ... }
73
+
74
+
75
+ ## Examples (Controller)
76
+ The following two examples show how to use `hook_me_up` in a controller,
77
+ which actually does the same as `before_filter`; this is just for the sake of demonstration and can be done with models, 'regular' classes and so on...
78
+
79
+
80
+ ### With lambda
81
+ class HomeController < ApplicationController
82
+ include HookMeUp
83
+
84
+ def index
85
+ render :text => "hello there #{params[:name]}"
86
+ end
87
+
88
+ hook_me_up :index,
89
+ :before => lambda{ |controller, *args| controller.params[:name] = controller.current_user.name }
90
+ end
91
+
92
+
93
+ ### With method name
94
+ class HomeController < ApplicationController
95
+ include HookMeUp
96
+
97
+ def index
98
+ render :text => "hello there #{params[:name]}"
99
+ end
100
+
101
+ hook_me_up :index,
102
+ :before => :do_this_before
103
+
104
+ private
105
+
106
+ def do_this_before
107
+ params[:name] = current_user.name
108
+ end
109
+ end
110
+
111
+
112
+ ## Contributing
113
+
114
+ 1. Fork it
115
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
116
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
117
+ 4. Push to the branch (`git push origin my-new-feature`)
118
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hook_me_up/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "hook_me_up"
8
+ gem.version = HookMeUp::VERSION
9
+ gem.authors = ["Chen Fisher"]
10
+ gem.email = ["chen.fisher@gmail.com"]
11
+ gem.description = %q{Lets you hook any method class with :before and :after hooks}
12
+ gem.summary = %q{This gem lets you hook and method in any class with a before and after hooks}
13
+ gem.homepage = ""
14
+
15
+ gem.add_development_dependency "rspec"
16
+
17
+ gem.files = `git ls-files`.split($/)
18
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
+ gem.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,3 @@
1
+ module HookMeUp
2
+ VERSION = "0.0.1"
3
+ end
data/lib/hook_me_up.rb ADDED
@@ -0,0 +1,45 @@
1
+ require "hook_me_up/version"
2
+
3
+ module HookMeUp
4
+ module ClassMethods
5
+ def hook_me_up(*args)
6
+ hooks = args.last.is_a?(::Hash) ? args.pop : {}
7
+
8
+ args.each do |method|
9
+ original_method = self.instance_method(method)
10
+
11
+ self.send(:define_method, method) do |*a|
12
+ if hooks[:before]
13
+ if hooks[:before].is_a? Proc
14
+ hooks[:before].call(self, *a)
15
+ else
16
+ self.send(hooks[:before], *a)
17
+ end
18
+ end
19
+
20
+ result = original_method.bind(self).call(*a)
21
+
22
+
23
+ if hooks[:after]
24
+ if hooks[:after].is_a? Proc
25
+ hooks[:after].call(self, *a, result)
26
+ else
27
+ self.send(hooks[:after], *a, result)
28
+ end
29
+ end
30
+
31
+ result
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ module InstanceMethods
38
+
39
+ end
40
+
41
+ def self.included(receiver)
42
+ receiver.extend ClassMethods
43
+ receiver.send :include, InstanceMethods
44
+ end
45
+ end
@@ -0,0 +1,9 @@
1
+ class SampleClass
2
+ def method_one(value)
3
+ value
4
+ end
5
+
6
+ def method_two
7
+ 'method_two'
8
+ end
9
+ end
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+ require 'hook_me_up/sample_class'
3
+
4
+ describe HookMeUp do
5
+ before(:each) do
6
+ @sample = SampleClass.new
7
+ end
8
+
9
+ it "hooks a before and after methods" do
10
+ class << @sample
11
+ include HookMeUp
12
+
13
+ hook_me_up :method_one, :before => :before_hook,
14
+ :after => :after_hook
15
+
16
+ def before_hook(*args)
17
+ end
18
+
19
+ def after_hook(*args, result)
20
+ end
21
+ end
22
+
23
+ @sample.should_receive(:before_hook).with('args')
24
+ @sample.should_receive(:after_hook).with('args', 'args')
25
+
26
+ @sample.method_one('args').should eq 'args'
27
+ end
28
+
29
+ it "can receive a block as a before or after hooks" do
30
+ class << @sample
31
+ include HookMeUp
32
+
33
+ attr_accessor :before, :after
34
+
35
+ hook_me_up :method_one, :before => lambda{ |sender, *args| sender.before = true },
36
+ :after => lambda{ |sender, *args, result| sender.after = true }
37
+ end
38
+
39
+ @sample.method_one('args').should eq 'args'
40
+
41
+ @sample.before.should eq true
42
+ @sample.after.should eq true
43
+ end
44
+
45
+
46
+ it "can receive multiple methods to hook" do
47
+ class << @sample
48
+ include HookMeUp
49
+
50
+ attr_accessor :before, :after
51
+
52
+ hook_me_up :method_one, :method_two, :before => :before_hook, :after => :after_hook
53
+
54
+ def before_hook(*args)
55
+ end
56
+
57
+ def after_hook(*args, result)
58
+ end
59
+ end
60
+
61
+ @sample.should_receive(:before_hook).exactly(2)
62
+ @sample.should_receive(:after_hook).exactly(2)
63
+
64
+ @sample.method_one('args').should eq 'args'
65
+ @sample.method_two.should eq 'method_two'
66
+ end
67
+
68
+ it "should not call before or after if not defined" do
69
+ class << @sample
70
+ include HookMeUp
71
+
72
+ hook_me_up :method_one, :before => :before_hook
73
+
74
+ def before_hook(*args)
75
+ end
76
+
77
+ def after_hook(*args, result)
78
+ end
79
+ end
80
+
81
+ @sample.should_receive(:before_hook).with('args')
82
+ @sample.should_not_receive(:after_hook)
83
+
84
+ @sample.method_one('args').should eq 'args'
85
+ end
86
+ end
@@ -0,0 +1 @@
1
+ require 'hook_me_up'
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hook_me_up
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chen Fisher
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
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'
30
+ description: Lets you hook any method class with :before and :after hooks
31
+ email:
32
+ - chen.fisher@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - .rspec
39
+ - CHANGELOG.md
40
+ - Gemfile
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - hook_me_up.gemspec
45
+ - lib/hook_me_up.rb
46
+ - lib/hook_me_up/version.rb
47
+ - spec/hook_me_up/sample_class.rb
48
+ - spec/hook_me_up_spec.rb
49
+ - spec/spec_helper.rb
50
+ homepage: ''
51
+ licenses: []
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 1.8.24
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: This gem lets you hook and method in any class with a before and after hooks
74
+ test_files:
75
+ - spec/hook_me_up/sample_class.rb
76
+ - spec/hook_me_up_spec.rb
77
+ - spec/spec_helper.rb