easy_callbacks 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.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.ruby-version +1 -0
- data/Gemfile +3 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +89 -0
- data/Rakefile +33 -0
- data/easy_callbacks.gemspec +27 -0
- data/lib/easy_callbacks/base.rb +112 -0
- data/lib/easy_callbacks/version.rb +3 -0
- data/lib/easy_callbacks.rb +17 -0
- data/spec/lib/easy_callbacks/base_spec.rb +28 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/support/some_class.rb +36 -0
- metadata +144 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cce1c2beaaac84b2bffb1e31f5050fd266a9579d
|
4
|
+
data.tar.gz: 2a110c619bdd06c0bedbf78a7fb0e2cd6b10db03
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2aa984d05872a945035ce437e0469013bf71ad60022fe88a2b5e0dc8f70aa56b80a35a4df5c84e050765fe53dc877679ca59cdb84d70739327abda2d84c1568f
|
7
|
+
data.tar.gz: b6183b1cc3fed10ee38d11e59c28a224c9edf92fa57f4c8fc843d501d9ce47a34716cce032848c1b0b313526a32add6f82e06865fab0b45bba9bc083401f7eda
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.3.0@easy_callbacks
|
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
= Easy Callbacks
|
2
|
+
|
3
|
+
Provide callbacks support for Ruby classes.
|
4
|
+
|
5
|
+
== Example
|
6
|
+
|
7
|
+
class SomeClass
|
8
|
+
|
9
|
+
def some_method(some_arg)
|
10
|
+
puts some_arg
|
11
|
+
end
|
12
|
+
|
13
|
+
include EasyCallbacks::Base
|
14
|
+
|
15
|
+
# named callbacks
|
16
|
+
before :some_method, :do_it_before
|
17
|
+
around :some_method, :do_it_around
|
18
|
+
after :some_method, :do_it_after
|
19
|
+
|
20
|
+
# anonymous callbacks
|
21
|
+
before :some_method do |some_arg, callback_details|
|
22
|
+
# ...
|
23
|
+
end
|
24
|
+
|
25
|
+
def do_it_before(some_arg, callback_details)
|
26
|
+
# ...
|
27
|
+
end
|
28
|
+
|
29
|
+
def do_it_around(some_arg, callback_details)
|
30
|
+
# ...
|
31
|
+
end
|
32
|
+
|
33
|
+
def do_it_after(some_arg, callback_details)
|
34
|
+
# ...
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
=== Callback Details
|
40
|
+
|
41
|
+
The second argument is a Hash containing some callback call details:
|
42
|
+
|
43
|
+
For named callbacks:
|
44
|
+
|
45
|
+
{
|
46
|
+
class: SomeClass,
|
47
|
+
target: :some_method,
|
48
|
+
type: :before,
|
49
|
+
callback_name: :do_it_before,
|
50
|
+
proc: nil
|
51
|
+
}
|
52
|
+
|
53
|
+
For anonymous callbacks:
|
54
|
+
|
55
|
+
{
|
56
|
+
class: SomeClass,
|
57
|
+
target: :some_method,
|
58
|
+
type: :before,
|
59
|
+
callback_name: nil,
|
60
|
+
proc: '#<Proc:0x00000002655a20@...>'
|
61
|
+
}
|
62
|
+
|
63
|
+
For around callbacks (with success):
|
64
|
+
|
65
|
+
{
|
66
|
+
class: SomeClass,
|
67
|
+
target: :some_method,
|
68
|
+
type: :around,
|
69
|
+
callback_name: :do_it_around,
|
70
|
+
proc: nil,
|
71
|
+
return_type: :success,
|
72
|
+
return_object: nil
|
73
|
+
}
|
74
|
+
|
75
|
+
For around callbacks (with error):
|
76
|
+
|
77
|
+
{
|
78
|
+
class: SomeClass,
|
79
|
+
target: :some_method,
|
80
|
+
type: :around,
|
81
|
+
callback_name: :do_it_around,
|
82
|
+
proc: nil,
|
83
|
+
return_type: :error,
|
84
|
+
return_object: '#<StandardError: StandardError>'
|
85
|
+
}
|
86
|
+
|
87
|
+
===
|
88
|
+
|
89
|
+
{<img src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif" alt="Donate" />}[https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=9KHDLTWYK9A62]
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# Rakefile for rake -*- ruby -*-
|
2
|
+
|
3
|
+
# Copyright 2003, 2004, 2005 by Jim Weirich (jim@weirichhouse.org)
|
4
|
+
# All rights reserved.
|
5
|
+
|
6
|
+
# This file may be distributed under an MIT style license. See
|
7
|
+
# MIT-LICENSE for details.
|
8
|
+
|
9
|
+
require 'bundler/gem_tasks'
|
10
|
+
require 'rake/testtask'
|
11
|
+
require 'rdoc/task'
|
12
|
+
|
13
|
+
Rake::TestTask.new(:test) do |t|
|
14
|
+
t.libs << "test"
|
15
|
+
t.verbose = true
|
16
|
+
t.test_files = FileList['test/**/test_*.rb']
|
17
|
+
end
|
18
|
+
|
19
|
+
RDoc::Task.new do |rdoc|
|
20
|
+
rdoc.rdoc_dir = 'rdoc'
|
21
|
+
rdoc.title = 'EasyCallbacks'
|
22
|
+
rdoc.options << '--line-numbers'
|
23
|
+
rdoc.rdoc_files.include('README.rdoc')
|
24
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
25
|
+
end
|
26
|
+
|
27
|
+
require 'rspec/core/rake_task'
|
28
|
+
|
29
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
30
|
+
t.rspec_opts = %w(--color)
|
31
|
+
end
|
32
|
+
|
33
|
+
task :default => :spec
|
@@ -0,0 +1,27 @@
|
|
1
|
+
$:.push File.expand_path('../lib', __FILE__)
|
2
|
+
|
3
|
+
require 'easy_callbacks/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'easy_callbacks'
|
7
|
+
s.version = EasyCallbacks::VERSION
|
8
|
+
s.authors = ['r4z3c']
|
9
|
+
s.email = ['r4z3c.43@gmail.com']
|
10
|
+
s.homepage = 'https://github.com/r4z3c/easy_callbacks.git'
|
11
|
+
s.summary = 'Set callbacks for Ruby class methods'
|
12
|
+
s.description = 'Provide callbacks support for Ruby classes'
|
13
|
+
s.licenses = %w(MIT)
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
17
|
+
|
18
|
+
s.require_paths = %w(lib)
|
19
|
+
|
20
|
+
s.add_dependency 'bundler', '~>1'
|
21
|
+
s.add_dependency 'activesupport', '~>4'
|
22
|
+
s.add_dependency 'method_decorator', '~>1'
|
23
|
+
|
24
|
+
s.add_development_dependency 'rspec', '~>3'
|
25
|
+
s.add_development_dependency 'simplecov', '~>0'
|
26
|
+
s.add_development_dependency 'model-builder', '~>2'
|
27
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'method_decorator'
|
2
|
+
|
3
|
+
module EasyCallbacks
|
4
|
+
module Base extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
|
8
|
+
include MethodDecorator
|
9
|
+
|
10
|
+
TYPES = %w(before around after)
|
11
|
+
|
12
|
+
class << self
|
13
|
+
|
14
|
+
attr_accessor :callbacks
|
15
|
+
|
16
|
+
def method_missing(method_sym, *args, &block)
|
17
|
+
if TYPES.include? method_sym.to_s
|
18
|
+
target = args.first
|
19
|
+
callback_name = args.count.eql?(1) ? nil : args.last
|
20
|
+
handle_method_missing method_sym, target, callback_name, &block
|
21
|
+
else
|
22
|
+
super method_sym, *args, &block
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def handle_method_missing(type, target, callback_name, &block)
|
29
|
+
push_callback type, target, callback_name, &block
|
30
|
+
decorate_target target
|
31
|
+
end
|
32
|
+
|
33
|
+
def push_callback(type, target, callback_name)
|
34
|
+
initialize_callbacks type
|
35
|
+
self.callbacks[type].push target: target,
|
36
|
+
callback_name: callback_name,
|
37
|
+
proc: (block_given? ? Proc.new : nil)
|
38
|
+
end
|
39
|
+
|
40
|
+
def initialize_callbacks(type)
|
41
|
+
self.callbacks ||= {}
|
42
|
+
self.callbacks[type] ||= []
|
43
|
+
end
|
44
|
+
|
45
|
+
def decorate_target(target)
|
46
|
+
decorate_method target do |*args, &block|
|
47
|
+
execute_callbacks_for :before, target, nil, *args, &block
|
48
|
+
result, error = execute_original_method target, *args, &block
|
49
|
+
|
50
|
+
additional_options = error ?
|
51
|
+
{ return_type: :error, return_object: error } :
|
52
|
+
{ return_type: :success, return_object: result }
|
53
|
+
|
54
|
+
execute_callbacks_for :around, target, additional_options, *args, &block
|
55
|
+
|
56
|
+
raise error if error
|
57
|
+
|
58
|
+
execute_callbacks_for :after, target, nil, *args, &block
|
59
|
+
|
60
|
+
result
|
61
|
+
end unless method_defined? original_method_name_for(target)
|
62
|
+
end
|
63
|
+
|
64
|
+
def respond_to_missing?(target, include_private=false)
|
65
|
+
TYPES.include?(target) ? true : super ;
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
def execute_callbacks_for(type, target, additional_options, *args, &block)
|
71
|
+
get_callbacks_for(type).select { |c| c[:target].eql? target }.each do |callback|
|
72
|
+
cn = callback[:callback_name]
|
73
|
+
proc = callback[:proc]
|
74
|
+
t = callback[:target]
|
75
|
+
arguments = args + [
|
76
|
+
class: callbacks_holder,
|
77
|
+
target: t,
|
78
|
+
type: type,
|
79
|
+
callback_name: cn,
|
80
|
+
proc: proc
|
81
|
+
];
|
82
|
+
|
83
|
+
arguments.last.merge! additional_options if additional_options
|
84
|
+
|
85
|
+
proc.nil? ? send(cn, *arguments, &block) : proc.call(*arguments, &block)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def get_callbacks_for(type)
|
90
|
+
callbacks_holder.callbacks[type]
|
91
|
+
end
|
92
|
+
|
93
|
+
def callbacks_holder
|
94
|
+
self.class.eql?(Class) ? self.singleton_class : self.class
|
95
|
+
end
|
96
|
+
|
97
|
+
def execute_original_method(target, *args, &block)
|
98
|
+
result = error = nil
|
99
|
+
|
100
|
+
begin
|
101
|
+
result = call_original_method target, *args, &block
|
102
|
+
rescue => e
|
103
|
+
error = e
|
104
|
+
end
|
105
|
+
|
106
|
+
return result, error
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module EasyCallbacks
|
2
|
+
|
3
|
+
ROOT = File.expand_path(File.join(File.dirname(__FILE__),'..'))
|
4
|
+
|
5
|
+
class << self
|
6
|
+
|
7
|
+
def load_easy_callbacks
|
8
|
+
Dir[File.expand_path(File.join(ROOT,'lib','easy_callbacks','**/*.rb'))].each do |file|
|
9
|
+
require file
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
load_easy_callbacks
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'support/some_class'
|
3
|
+
|
4
|
+
describe EasyCallbacks::Base do
|
5
|
+
|
6
|
+
let(:instance) { SomeClass.new }
|
7
|
+
|
8
|
+
it do
|
9
|
+
expect_instance_for_puts_with('before_1: some_arg')
|
10
|
+
expect_class_for_puts_with('before_2: some_arg')
|
11
|
+
expect_instance_for_puts_with('some_method: some_arg')
|
12
|
+
expect_instance_for_puts_with('around_1: some_arg')
|
13
|
+
expect_class_for_puts_with('around_2: some_arg')
|
14
|
+
expect_instance_for_puts_with('after_1: some_arg')
|
15
|
+
expect_class_for_puts_with('after_2: some_arg')
|
16
|
+
|
17
|
+
instance.some_method(:some_arg)
|
18
|
+
end
|
19
|
+
|
20
|
+
def expect_instance_for_puts_with(expected)
|
21
|
+
expect(instance).to receive(:puts).with(expected).ordered
|
22
|
+
end
|
23
|
+
|
24
|
+
def expect_class_for_puts_with(expected)
|
25
|
+
expect(SomeClass).to receive(:puts).with(expected).ordered
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
class SomeClass
|
2
|
+
|
3
|
+
def some_method(some_arg)
|
4
|
+
puts "some_method: #{some_arg}"
|
5
|
+
end
|
6
|
+
|
7
|
+
include EasyCallbacks::Base
|
8
|
+
|
9
|
+
before :some_method, :before_1
|
10
|
+
before :some_method do |some_arg, callback_details|
|
11
|
+
puts "before_2: #{some_arg}"
|
12
|
+
end
|
13
|
+
|
14
|
+
around :some_method, :around_1
|
15
|
+
around :some_method do |some_arg, callback_details|
|
16
|
+
puts "around_2: #{some_arg}"
|
17
|
+
end
|
18
|
+
|
19
|
+
after :some_method, :after_1
|
20
|
+
after :some_method do |some_arg, callback_details|
|
21
|
+
puts "after_2: #{some_arg}"
|
22
|
+
end
|
23
|
+
|
24
|
+
def before_1(some_arg, callback_details)
|
25
|
+
puts "before_1: #{some_arg}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def around_1(some_arg, callback_details)
|
29
|
+
puts "around_1: #{some_arg}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def after_1(some_arg, callback_details)
|
33
|
+
puts "after_1: #{some_arg}"
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_callbacks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- r4z3c
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-19 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'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: method_decorator
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: model-builder
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '2'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2'
|
97
|
+
description: Provide callbacks support for Ruby classes
|
98
|
+
email:
|
99
|
+
- r4z3c.43@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".ruby-version"
|
106
|
+
- Gemfile
|
107
|
+
- MIT-LICENSE
|
108
|
+
- README.rdoc
|
109
|
+
- Rakefile
|
110
|
+
- easy_callbacks.gemspec
|
111
|
+
- lib/easy_callbacks.rb
|
112
|
+
- lib/easy_callbacks/base.rb
|
113
|
+
- lib/easy_callbacks/version.rb
|
114
|
+
- spec/lib/easy_callbacks/base_spec.rb
|
115
|
+
- spec/spec_helper.rb
|
116
|
+
- spec/support/some_class.rb
|
117
|
+
homepage: https://github.com/r4z3c/easy_callbacks.git
|
118
|
+
licenses:
|
119
|
+
- MIT
|
120
|
+
metadata: {}
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 2.5.1
|
138
|
+
signing_key:
|
139
|
+
specification_version: 4
|
140
|
+
summary: Set callbacks for Ruby class methods
|
141
|
+
test_files:
|
142
|
+
- spec/lib/easy_callbacks/base_spec.rb
|
143
|
+
- spec/spec_helper.rb
|
144
|
+
- spec/support/some_class.rb
|