singleton_callbacks 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c831310d1ee03dfe4d5bdee303383299645fd984
4
+ data.tar.gz: 73ecaf7316ad7eba0457dddc9d830f4dd219a90d
5
+ SHA512:
6
+ metadata.gz: e6fbc7eb3ee9ab085331cb3f6c62d742d8525bfa05b5e86a87435a00101196767480b4513014396c43b357fed8c4148803a99cc71efba39824fe7cbeb6b1c2dc
7
+ data.tar.gz: 9ed79fd2d321e7778a530b5a36dbb29209fbda9bbaab71cc9415de5b2770c59e753fcd6d441ce1d77ceb04d747143d000f741f4a25907e3918593fcad4a93961
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.project
2
+ /.bundle
3
+ *.sqlite3
4
+ log/*
5
+ tmp/*
6
+ *~
7
+ *.lock
8
+ *.DS_Store
9
+ *.swp
10
+ *.out
11
+ .idea/*
12
+ coverage/*
13
+ .keep
14
+ .gitkeep
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.3.0@singleton_callbacks
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
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,3 @@
1
+ = Singleton Callbacks
2
+
3
+ See the full test example here[https://github.com/r4z3c/singleton_callbacks/blob/master/spec/lib/singleton_callbacks_spec.rb]
data/Rakefile ADDED
@@ -0,0 +1,32 @@
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 |doc|
20
+ doc.main = 'README.rdoc'
21
+ doc.title = 'Rake -- Ruby Make'
22
+ doc.rdoc_files = FileList.new %w[lib MIT-LICENSE doc/**/*.rdoc *.rdoc]
23
+ doc.rdoc_dir = 'html'
24
+ end
25
+
26
+ require 'rspec/core/rake_task'
27
+
28
+ RSpec::Core::RakeTask.new(:spec) do |t|
29
+ t.rspec_opts = %w(--color)
30
+ end
31
+
32
+ task :default => :spec
@@ -0,0 +1,94 @@
1
+ module SingletonCallbacks extend ActiveSupport::Concern
2
+
3
+ SUBSTITUTE_METHOD_PREFIX = 'complex_'
4
+ ORIGINAL_METHOD_PREFIX = 'original_'
5
+
6
+ included do |base|
7
+ base.class_eval do
8
+
9
+ @callbacks = { before: [], after: [] }
10
+
11
+ class << self
12
+
13
+ def before(method_sym, callback_sym, options=nil)
14
+ setup_callback :before, method_sym, callback_sym, options
15
+ end
16
+
17
+ def after(method_sym, callback_sym, options=nil)
18
+ setup_callback :after, method_sym, callback_sym, options
19
+ end
20
+
21
+ private
22
+
23
+ def setup_callback(callback_type, method_sym, callback_sym, options)
24
+ add_to_callback_list callback_type, method_sym, callback_sym, options
25
+ replace_original_method method_sym
26
+ end
27
+
28
+ def add_to_callback_list(callback_type, method_sym, callback_sym, options)
29
+ @callbacks[callback_type] << {
30
+ method_sym: method_sym,
31
+ callback_sym: callback_sym,
32
+ options: options
33
+ }
34
+ end
35
+
36
+ def replace_original_method(method_sym)
37
+ substitute_method_name = substitute_method_name_for method_sym
38
+ return if substitute_method_already_exists? substitute_method_name
39
+
40
+ save_original_method method_sym
41
+ create_substitute_method method_sym, substitute_method_name
42
+ class_eval { alias_method method_sym, substitute_method_name }
43
+ end
44
+
45
+ def substitute_method_name_for(method_sym)
46
+ "#{SingletonCallbacks::SUBSTITUTE_METHOD_PREFIX}#{method_sym}"
47
+ end
48
+
49
+ def substitute_method_already_exists?(substitute_method_name)
50
+ already_exists = true
51
+ class_eval { already_exists = method_defined? substitute_method_name }
52
+ already_exists
53
+ end
54
+
55
+ def save_original_method(method_sym)
56
+ original_method_sym = original_method_name_for(method_sym).to_sym
57
+ class_eval { alias_method original_method_sym, method_sym }
58
+ end
59
+
60
+ def original_method_name_for(method_sym)
61
+ "#{SingletonCallbacks::ORIGINAL_METHOD_PREFIX}#{method_sym}"
62
+ end
63
+
64
+ def create_substitute_method(method_sym, substitute_method_name)
65
+ class_eval do
66
+ define_method substitute_method_name do
67
+ execute_callbacks_for :before, method_sym
68
+ r = send(eval_as_singleton { original_method_name_for(method_sym) })
69
+ execute_callbacks_for :after, method_sym
70
+ r
71
+ end
72
+ end
73
+ end
74
+
75
+ end
76
+
77
+ def execute_callbacks_for(callback_type, method_sym)
78
+ callbacks = get_callbacks_for(callback_type, method_sym)
79
+ callbacks.each { |c| send c[:callback_sym] }
80
+ end
81
+
82
+ def get_callbacks_for(callback_type, method_sym)
83
+ callbacks = self.singleton_class.instance_variable_get(:@callbacks)
84
+ callbacks[callback_type].select{ |c| c[:method_sym].eql? method_sym }
85
+ end
86
+
87
+ def eval_as_singleton
88
+ self.singleton_class.class_eval &Proc.new if block_given?
89
+ end
90
+
91
+ end
92
+ end
93
+
94
+ end
@@ -0,0 +1,3 @@
1
+ module SingletonCallbacks
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,27 @@
1
+ $:.push File.expand_path('../lib', __FILE__)
2
+
3
+ require 'singleton_callbacks/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'singleton_callbacks'
7
+ s.version = SingletonCallbacks::VERSION
8
+ s.authors = ['r4z3c']
9
+ s.email = ['r4z3c.43@gmail.com']
10
+ s.homepage = 'https://github.com/r4z3c/singleton_callbacks.git'
11
+ s.summary = 'Callbacks support for Ruby singleton classes'
12
+ s.description = 'Provide callbacks support for Ruby singleton 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
+
23
+ s.add_development_dependency 'sqlite3', '~>1'
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,50 @@
1
+ require 'spec_helper'
2
+
3
+ describe Object do
4
+
5
+ class Manager
6
+ class << self
7
+ include SingletonCallbacks
8
+
9
+ def validate; 'validating' end
10
+ def save; 'saving' end
11
+ def notify; 'notifying' end
12
+
13
+ before :save, :validate1
14
+ before :save, :validate2
15
+ after :save, :notify1
16
+ after :save, :notify2
17
+ end
18
+ end
19
+
20
+ let(:callbacks) { Manager.singleton_class.instance_variable_get :@callbacks }
21
+ let(:expectation) {{
22
+ before: [
23
+ { method_sym: :save, callback_sym: :validate1, options: nil },
24
+ { method_sym: :save, callback_sym: :validate2, options: nil }
25
+ ],
26
+ after: [
27
+ { method_sym: :save, callback_sym: :notify1, options: nil },
28
+ { method_sym: :save, callback_sym: :notify2, options: nil }
29
+ ]
30
+ }}
31
+
32
+ it { expect(callbacks).to eq expectation }
33
+
34
+ describe '.complex_save' do
35
+
36
+ subject { Manager.save }
37
+
38
+ before do
39
+ expect(Manager).to receive(:validate1).ordered
40
+ expect(Manager).to receive(:validate2).ordered
41
+ expect(Manager).to receive(:original_save).and_return('saving').ordered
42
+ expect(Manager).to receive(:notify1).ordered
43
+ expect(Manager).to receive(:notify2).ordered
44
+ end
45
+
46
+ it { is_expected.to eq 'saving' }
47
+
48
+ end
49
+
50
+ end
@@ -0,0 +1,18 @@
1
+ require 'active_record'
2
+ require 'simplecov'
3
+ require 'support/database_connection'
4
+ require 'model_builder'
5
+
6
+ SimpleCov.start
7
+
8
+ require 'singleton_callbacks'
9
+
10
+ RSpec.configure do |config|
11
+ config.before :all do
12
+ Spec::Support::DatabaseConnection.establish_sqlite_connection
13
+ end
14
+
15
+ config.after :each do
16
+ ModelBuilder.clean
17
+ end
18
+ end
@@ -0,0 +1,13 @@
1
+ module Spec
2
+ module Support
3
+ module DatabaseConnection
4
+
5
+ def self.establish_sqlite_connection
6
+ database = "#{File.dirname(__FILE__)}/../../tmp/singleton_callbacks-test.sqlite3"
7
+
8
+ ::ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: database
9
+ end
10
+
11
+ end
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: singleton_callbacks
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - r4z3c
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-28 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: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1'
48
+ type: :development
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 singleton 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
+ - lib/singleton_callbacks.rb
111
+ - lib/singleton_callbacks/version.rb
112
+ - singleton_callbacks.gemspec
113
+ - spec/lib/singleton_callbacks_spec.rb
114
+ - spec/spec_helper.rb
115
+ - spec/support/database_connection.rb
116
+ homepage: https://github.com/r4z3c/singleton_callbacks.git
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.5.1
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Callbacks support for Ruby singleton classes
140
+ test_files:
141
+ - spec/lib/singleton_callbacks_spec.rb
142
+ - spec/spec_helper.rb
143
+ - spec/support/database_connection.rb