object_callbacks 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +73 -0
- data/Rakefile +2 -0
- data/examples/basic_callback.rb +41 -0
- data/lib/object_callbacks/version.rb +3 -0
- data/lib/object_callbacks.rb +85 -0
- data/object_callbacks.gemspec +24 -0
- data/spec/before_call_spec.rb +52 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 141083ea54befed6f748fbf30271568b5f1e739d
|
4
|
+
data.tar.gz: 7ba93510faeb5aee04922674b22f97fc35f17c3d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2c00d96843bd513c295cce0491323f16c805ea423f915a2b233ce297dfe87544c32dd9338fa98eed4a288c6b005de04f39e6adf44b1f33b190bf1524c05e2c6e
|
7
|
+
data.tar.gz: 44769f575bfceb961b917b6d4658d83008ce8ffe461970c532ae24fb10496bab06191be9a17d1718c7abb26622644a89a9a279557a24e9de6aefbe4a97756d0b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Raza Ali
|
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,73 @@
|
|
1
|
+
# ObjectCallbacks
|
2
|
+
|
3
|
+
I need something similar to BeforeFilters [**https://github.com/IDme/before_filters**] but more than that.
|
4
|
+
|
5
|
+
This library allow you to bind before_call and after_call to a Ruby class method, similar to ActiveRecord's callbacks.
|
6
|
+
|
7
|
+
Similar to ActiveRecord, if you need to termiate execution in before_call just return false.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'object_callbacks'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install object_callbacks
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class MyClass
|
29
|
+
extend ObjectCallbacks
|
30
|
+
|
31
|
+
before_call :sit_down
|
32
|
+
before_call :drink_one_glass, only: [:sleep]
|
33
|
+
after_call :drink_four_glasses, only: [:wake_up]
|
34
|
+
|
35
|
+
def sleep
|
36
|
+
puts 'Go to sleep'
|
37
|
+
end
|
38
|
+
|
39
|
+
def wake_up
|
40
|
+
puts 'Get up now'
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def drink_one_glass
|
46
|
+
puts 'Drink one glass'
|
47
|
+
end
|
48
|
+
|
49
|
+
def drink_four_glasses
|
50
|
+
puts 'Drink four glasses'
|
51
|
+
end
|
52
|
+
|
53
|
+
def sit_down
|
54
|
+
puts 'Please sit down'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
my_class = MyClass.new
|
59
|
+
|
60
|
+
my_class.sleep
|
61
|
+
=> Please sit down
|
62
|
+
=> Drink one glass
|
63
|
+
=> Go to sleep
|
64
|
+
|
65
|
+
```
|
66
|
+
|
67
|
+
## Contributing
|
68
|
+
|
69
|
+
1. Fork it ( https://github.com/raza/object_callbacks/fork )
|
70
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
71
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
72
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
73
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'object_callbacks'
|
2
|
+
|
3
|
+
class MyClass
|
4
|
+
extend ObjectCallbacks
|
5
|
+
|
6
|
+
before_call :before_callback_1, only: [:action1]
|
7
|
+
before_call :before_callback_2, only: [:action1]
|
8
|
+
before_call :before_callback_3, only: [:action1]
|
9
|
+
after_call :after_call_1, only: [:action1]
|
10
|
+
|
11
|
+
def action1
|
12
|
+
puts 'action1 called'
|
13
|
+
end
|
14
|
+
|
15
|
+
def action2
|
16
|
+
puts 'action2 called'
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def before_callback_1
|
22
|
+
puts 'before_callback_1 called'
|
23
|
+
end
|
24
|
+
|
25
|
+
def before_callback_2
|
26
|
+
puts 'before_callback_2 called with termination'
|
27
|
+
# false
|
28
|
+
end
|
29
|
+
|
30
|
+
def before_callback_3
|
31
|
+
puts 'before_callback_3 called'
|
32
|
+
end
|
33
|
+
|
34
|
+
def after_call_1
|
35
|
+
puts 'after_call_1 called'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
my_class = MyClass.new
|
40
|
+
my_class.action1
|
41
|
+
my_class.action2
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require "object_callbacks/version"
|
2
|
+
|
3
|
+
module ObjectCallbacks
|
4
|
+
def method_added(action_name)
|
5
|
+
if actions.include?(action_name) || before_calls.key?(action_name) || after_calls.key?(action_name)
|
6
|
+
return
|
7
|
+
end
|
8
|
+
|
9
|
+
apply_callback_actions(action_name)
|
10
|
+
end
|
11
|
+
|
12
|
+
def before_call(action_name, options = {})
|
13
|
+
before_calls[action_name] = options
|
14
|
+
end
|
15
|
+
|
16
|
+
def after_call(action_name, options = {})
|
17
|
+
after_calls[action_name] = options
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def before_calls
|
23
|
+
@before_calls ||= {}
|
24
|
+
end
|
25
|
+
|
26
|
+
def after_calls
|
27
|
+
@after_calls ||= {}
|
28
|
+
end
|
29
|
+
|
30
|
+
def actions
|
31
|
+
@actions ||= []
|
32
|
+
end
|
33
|
+
|
34
|
+
def apply_callback_actions(action_name)
|
35
|
+
actions << action_name
|
36
|
+
|
37
|
+
original_method = instance_method(action_name)
|
38
|
+
|
39
|
+
define_method(action_name) do |*args, &block|
|
40
|
+
val = self.class.send(:execute_before_calls, action_name, self)
|
41
|
+
return false if val == false
|
42
|
+
result = original_method.bind(self).call(*args, &block)
|
43
|
+
self.class.send(:execute_after_calls, action_name, self)
|
44
|
+
|
45
|
+
result
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def execute_before_calls(action_name, instance)
|
50
|
+
callbacks_for_method(action_name, :before).each do |before_call_method|
|
51
|
+
val = instance.send(before_call_method)
|
52
|
+
return false if val == false
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def execute_after_calls(action_name, instance)
|
57
|
+
callbacks_for_method(action_name, :after).each do |after_call_method|
|
58
|
+
instance.send(after_call_method)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def callbacks_for_method(action_name, callback_type)
|
63
|
+
callbacks = send("#{callback_type}_calls")
|
64
|
+
|
65
|
+
callbacks_for_all(callbacks) +
|
66
|
+
only_callbacks(callbacks, action_name) +
|
67
|
+
except_callbacks(callbacks, action_name)
|
68
|
+
end
|
69
|
+
|
70
|
+
def callbacks_for_all(callbacks)
|
71
|
+
callbacks.select { |_key, value| value.empty? }.keys
|
72
|
+
end
|
73
|
+
|
74
|
+
def only_callbacks(callbacks, action_name)
|
75
|
+
callbacks.select do |_key, value|
|
76
|
+
!value[:only].nil? && value[:only].include?(action_name)
|
77
|
+
end.keys
|
78
|
+
end
|
79
|
+
|
80
|
+
def except_callbacks(callbacks, action_name)
|
81
|
+
callbacks.select do |_key, value|
|
82
|
+
!value[:except].nil? && !value[:except].include?(action_name)
|
83
|
+
end.keys
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'object_callbacks/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "object_callbacks"
|
8
|
+
spec.version = ObjectCallbacks::VERSION
|
9
|
+
spec.authors = ["Raza Ali"]
|
10
|
+
spec.email = ["tauraz@gmail.com"]
|
11
|
+
spec.summary = 'Ruby object method callbacks'
|
12
|
+
spec.description = 'Callbacks to execute before or after object method call'
|
13
|
+
spec.homepage = "https://github.com/raza/object_callbacks"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0") + Dir['lib/**/*.rb']
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 2.9"
|
24
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'object_callbacks'
|
2
|
+
require 'pry'
|
3
|
+
|
4
|
+
class MyClass
|
5
|
+
extend ObjectCallbacks
|
6
|
+
|
7
|
+
before_call :sit_down
|
8
|
+
before_call :drink_one_glass, only: [:sleep]
|
9
|
+
after_call :drink_four_glasses, only: [:wake_up]
|
10
|
+
|
11
|
+
def sleep
|
12
|
+
'Go to sleep'
|
13
|
+
end
|
14
|
+
|
15
|
+
def wake_up
|
16
|
+
'Get up now'
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def drink_one_glass
|
22
|
+
'Drink one glass'
|
23
|
+
end
|
24
|
+
|
25
|
+
def drink_four_glasses
|
26
|
+
'Drink four glasses'
|
27
|
+
end
|
28
|
+
|
29
|
+
def sit_down
|
30
|
+
'Please sit down'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe ObjectCallbacks do
|
35
|
+
subject { MyClass.new }
|
36
|
+
describe '#sleep' do
|
37
|
+
it 'calls sit_down' do
|
38
|
+
subject.should_receive(:sit_down)
|
39
|
+
subject.sleep
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'drinks one glass' do
|
43
|
+
subject.should_receive(:drink_one_glass)
|
44
|
+
subject.sleep
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should not drink four glasses' do
|
48
|
+
subject.should_not_receive(:drink_four_glasses)
|
49
|
+
subject.sleep
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: object_callbacks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Raza Ali
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.9'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.9'
|
55
|
+
description: Callbacks to execute before or after object method call
|
56
|
+
email:
|
57
|
+
- tauraz@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- examples/basic_callback.rb
|
68
|
+
- lib/object_callbacks.rb
|
69
|
+
- lib/object_callbacks/version.rb
|
70
|
+
- object_callbacks.gemspec
|
71
|
+
- spec/before_call_spec.rb
|
72
|
+
homepage: https://github.com/raza/object_callbacks
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.4.5
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Ruby object method callbacks
|
96
|
+
test_files:
|
97
|
+
- spec/before_call_spec.rb
|