simple_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.
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/simple_callbacks.rb +7 -0
- data/lib/simple_callbacks/abstract_base.rb +58 -0
- data/lib/simple_callbacks/validations.rb +25 -0
- data/lib/simple_callbacks/version.rb +3 -0
- data/simple_callbacks.gemspec +24 -0
- data/spec/simple_callbacks/callback_spec.rb +115 -0
- data/spec/spec_helper.rb +23 -0
- data/spec/support/aws_init.rb.example +4 -0
- metadata +126 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jeremy Green
|
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,29 @@
|
|
1
|
+
# SimpleCallbacks
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'simple_callbacks'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install simple_callbacks
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'aws/record/abstract_base'
|
2
|
+
require 'active_support/callbacks'
|
3
|
+
require 'active_model/callbacks'
|
4
|
+
require 'active_model/validations/callbacks'
|
5
|
+
module AWS
|
6
|
+
module Record
|
7
|
+
module AbstractBase
|
8
|
+
|
9
|
+
def self.extended base
|
10
|
+
# This first bit is straight from AWS::Record::AbstractBase
|
11
|
+
base.send(:extend, ClassMethods)
|
12
|
+
base.send(:include, InstanceMethods)
|
13
|
+
base.send(:include, DirtyTracking)
|
14
|
+
base.send(:extend, Validations)
|
15
|
+
|
16
|
+
# these 3 modules are for rails 3+ active model compatability
|
17
|
+
base.send(:extend, Naming)
|
18
|
+
base.send(:include, Naming)
|
19
|
+
base.send(:include, Conversion)
|
20
|
+
|
21
|
+
# This is the new stuff.
|
22
|
+
# These modules provide the callback framework
|
23
|
+
base.send(:include,::ActiveSupport::Callbacks)
|
24
|
+
base.send(:extend,::ActiveModel::Callbacks)
|
25
|
+
base.send(:include,::ActiveModel::Validations::Callbacks)
|
26
|
+
|
27
|
+
# set up some callbacks
|
28
|
+
base.send(:define_model_callbacks, :save, :create, :update, :destroy)
|
29
|
+
end
|
30
|
+
|
31
|
+
module InstanceMethods
|
32
|
+
# Here we transparently wrap the original methods in a
|
33
|
+
# version that runs a run_calbacks block and then delegates
|
34
|
+
# to the origin method.
|
35
|
+
update_without_callbacks = instance_method(:update)
|
36
|
+
define_method :update do
|
37
|
+
run_callbacks(:update){ update_without_callbacks.bind(self).() }
|
38
|
+
end
|
39
|
+
|
40
|
+
create_without_callbacks = instance_method(:create)
|
41
|
+
define_method :create do
|
42
|
+
run_callbacks(:create){ create_without_callbacks.bind(self).() }
|
43
|
+
end
|
44
|
+
|
45
|
+
save_without_callbacks = instance_method(:save)
|
46
|
+
define_method :save do
|
47
|
+
run_callbacks(:save){ save_without_callbacks.bind(self).() }
|
48
|
+
end
|
49
|
+
|
50
|
+
destroy_without_callbacks = instance_method(:destroy)
|
51
|
+
define_method :destroy do
|
52
|
+
run_callbacks(:destroy){ destroy_without_callbacks.bind(self).() }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module AWS
|
2
|
+
module Record
|
3
|
+
module Validations
|
4
|
+
|
5
|
+
def self.extended base
|
6
|
+
# This is the original declaration of :run_validations from
|
7
|
+
# AWS::Record::Validations. The name has just been changed.
|
8
|
+
base.send(:define_method, :run_validations_without_callbacks) do
|
9
|
+
errors.clear!
|
10
|
+
self.class.send(:validators).each do |validator|
|
11
|
+
validator.validate(self)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
base.send(:private, :run_validations_without_callbacks)
|
15
|
+
|
16
|
+
# This just wraps the other validation method in a run_callbacks block.
|
17
|
+
base.send(:define_method, :run_validations) do
|
18
|
+
run_callbacks(:validation){ run_validations_without_callbacks }
|
19
|
+
end
|
20
|
+
base.send(:private, :run_validations)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'simple_callbacks/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "simple_callbacks"
|
8
|
+
gem.version = SimpleCallbacks::VERSION
|
9
|
+
gem.authors = ["Jeremy Green"]
|
10
|
+
gem.email = ["jeremy@octolabs.com"]
|
11
|
+
gem.description = %q{Create, update and validate callbacks for AWS::Record::Model.}
|
12
|
+
gem.summary = %q{Create, update and validate callbacks for AWS::Record::Model.}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency "aws-sdk", "~> 1.8.0"
|
21
|
+
gem.add_dependency "activesupport", "~> 3.2.0"
|
22
|
+
gem.add_dependency "activemodel", "~> 3.2.0"
|
23
|
+
gem.add_development_dependency "rspec", ">= 2.0.0"
|
24
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe 'model validations' do
|
5
|
+
|
6
|
+
let(:klass) { Class.new(AWS::Record::Base) }
|
7
|
+
|
8
|
+
before(:each) do
|
9
|
+
klass.string_attr :name
|
10
|
+
klass.create_domain
|
11
|
+
klass.each{|u| u.destroy }
|
12
|
+
#sleep(2)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe 'validation callbacks' do
|
16
|
+
describe 'before_validation' do
|
17
|
+
it 'should fire' do
|
18
|
+
klass.before_validation :test_callback
|
19
|
+
obj = klass.new :name => "test"
|
20
|
+
obj.should_receive(:test_callback)
|
21
|
+
obj.valid?
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe 'after_validation' do
|
26
|
+
it 'should fire' do
|
27
|
+
klass.after_validation :test_callback
|
28
|
+
obj = klass.new :name => "test"
|
29
|
+
obj.should_receive(:test_callback)
|
30
|
+
obj.valid?
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe 'before_create' do
|
35
|
+
it 'should fire' do
|
36
|
+
klass.before_create :test_callback
|
37
|
+
obj = klass.new :name => "test"
|
38
|
+
obj.should_receive(:test_callback)
|
39
|
+
obj.save
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe 'after_create' do
|
44
|
+
it 'should fire' do
|
45
|
+
klass.after_create :test_callback
|
46
|
+
obj = klass.new :name => "test"
|
47
|
+
obj.should_receive(:test_callback)
|
48
|
+
obj.save
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe 'before_save' do
|
53
|
+
it 'should fire' do
|
54
|
+
klass.before_save :test_callback
|
55
|
+
obj = klass.new :name => "test"
|
56
|
+
obj.should_receive(:test_callback)
|
57
|
+
obj.save
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe 'after_save' do
|
62
|
+
it 'should fire' do
|
63
|
+
klass.after_save :test_callback
|
64
|
+
obj = klass.new :name => "test"
|
65
|
+
obj.should_receive(:test_callback)
|
66
|
+
obj.save
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe 'before_update' do
|
71
|
+
it 'should fire' do
|
72
|
+
klass.before_update :test_callback
|
73
|
+
obj = klass.new :name => "test"
|
74
|
+
obj.should_receive(:test_callback)
|
75
|
+
obj.save
|
76
|
+
obj.name = "test2"
|
77
|
+
obj.save
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe 'after_update' do
|
82
|
+
it 'should fire' do
|
83
|
+
klass.after_update :test_callback
|
84
|
+
obj = klass.new :name => "test"
|
85
|
+
obj.should_receive(:test_callback)
|
86
|
+
obj.save
|
87
|
+
obj.name = "test2"
|
88
|
+
obj.save
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
describe 'before_destroy' do
|
94
|
+
it 'should fire' do
|
95
|
+
klass.before_destroy :test_callback
|
96
|
+
obj = klass.new :name => "test"
|
97
|
+
obj.should_receive(:test_callback)
|
98
|
+
obj.save
|
99
|
+
obj.destroy
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe 'after_destroy' do
|
104
|
+
it 'should fire' do
|
105
|
+
klass.after_destroy :test_callback
|
106
|
+
obj = klass.new :name => "test"
|
107
|
+
obj.should_receive(:test_callback)
|
108
|
+
obj.save
|
109
|
+
obj.destroy
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
require 'aws-sdk'
|
9
|
+
require 'support/aws_init'
|
10
|
+
|
11
|
+
require "#{File.dirname(__FILE__)}/../lib/simple_callbacks"
|
12
|
+
|
13
|
+
RSpec.configure do |config|
|
14
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
15
|
+
config.run_all_when_everything_filtered = true
|
16
|
+
config.filter_run :focus
|
17
|
+
|
18
|
+
# Run specs in random order to surface order dependencies. If you find an
|
19
|
+
# order dependency and want to debug it, you can fix the order by providing
|
20
|
+
# the seed, which is printed after each run.
|
21
|
+
# --seed 1234
|
22
|
+
config.order = 'random'
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_callbacks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jeremy Green
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: aws-sdk
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.8.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.8.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activesupport
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 3.2.0
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 3.2.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: activemodel
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 3.2.0
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.2.0
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 2.0.0
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 2.0.0
|
78
|
+
description: Create, update and validate callbacks for AWS::Record::Model.
|
79
|
+
email:
|
80
|
+
- jeremy@octolabs.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .rspec
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- lib/simple_callbacks.rb
|
92
|
+
- lib/simple_callbacks/abstract_base.rb
|
93
|
+
- lib/simple_callbacks/validations.rb
|
94
|
+
- lib/simple_callbacks/version.rb
|
95
|
+
- simple_callbacks.gemspec
|
96
|
+
- spec/simple_callbacks/callback_spec.rb
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
- spec/support/aws_init.rb.example
|
99
|
+
homepage: ''
|
100
|
+
licenses: []
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
|
+
none: false
|
113
|
+
requirements:
|
114
|
+
- - ! '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
requirements: []
|
118
|
+
rubyforge_project:
|
119
|
+
rubygems_version: 1.8.24
|
120
|
+
signing_key:
|
121
|
+
specification_version: 3
|
122
|
+
summary: Create, update and validate callbacks for AWS::Record::Model.
|
123
|
+
test_files:
|
124
|
+
- spec/simple_callbacks/callback_spec.rb
|
125
|
+
- spec/spec_helper.rb
|
126
|
+
- spec/support/aws_init.rb.example
|