activerecord-reactor 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.md +4 -0
- data/Rakefile +22 -0
- data/lib/active_record/active_record.rb +12 -0
- data/lib/active_record/reactor/callbacks.rb +40 -0
- data/lib/active_record/reactor/version.rb +5 -0
- data/lib/active_record/reactor.rb +28 -0
- data/lib/activerecord-reactor.rb +8 -0
- data/test/cases/reactor_test.rb +29 -0
- data/test/models/banana.rb +5 -0
- data/test/models/cherry.rb +3 -0
- data/test/models/fruit.rb +15 -0
- data/test/models/fruit_reactor.rb +21 -0
- data/test/models/peel_reactor.rb +21 -0
- data/test/schema.rb +11 -0
- data/test/support/custom_assertions.rb +68 -0
- data/test/test_helper.rb +46 -0
- metadata +150 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2012 onrooby GmbH
|
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.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rdoc/task'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Run all tests'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.test_files = FileList['test/cases/*_test.rb']
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'ActiveModel::Reactor'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
|
3
|
+
class Reactor
|
4
|
+
|
5
|
+
module Callbacks
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.extend ClassMethods
|
9
|
+
base.class_attribute :reactor_callbacks
|
10
|
+
base.reactor_callbacks = ActiveRecord::Base::CALLBACKS.dup
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
|
15
|
+
def define_reactor_callbacks(*args)
|
16
|
+
options = args.extract_options!
|
17
|
+
options[:only] ||= [:before, :after] # TODO: implement around callbacks
|
18
|
+
types = Array.wrap(options[:only])
|
19
|
+
define_model_callbacks(*(args << options))
|
20
|
+
def args.combine(other, &block)
|
21
|
+
return product(other) unless block_given?
|
22
|
+
product(other).inject([]) { |result, ab| result << yield(ab.first, ab.last) }
|
23
|
+
end
|
24
|
+
self.reactor_callbacks = (reactor_callbacks + args.combine(types) { |arg, type| :"#{type}_#{arg}" }).uniq
|
25
|
+
end
|
26
|
+
|
27
|
+
def reactor(klass_or_name)
|
28
|
+
reactor = klass_or_name.is_a?(Class) ? klass_or_name : "#{klass_or_name.to_s.camelize}Reactor".constantize
|
29
|
+
(reactor.callbacks & self.reactor_callbacks).each do |callback|
|
30
|
+
send(callback) { |record| reactor.instance.send(callback, record) unless reactor.scrammed?; true }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
|
5
|
+
class Reactor
|
6
|
+
|
7
|
+
include Singleton
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def callbacks
|
11
|
+
(self.public_instance_methods - ActiveRecord::Reactor.public_instance_methods).grep(/\A(before|around|after)_.+/)
|
12
|
+
end
|
13
|
+
|
14
|
+
def scrammed?
|
15
|
+
!!@scrammed
|
16
|
+
end
|
17
|
+
|
18
|
+
def scram(&block)
|
19
|
+
previously_scrammed = @scrammed
|
20
|
+
@scrammed = true
|
21
|
+
yield
|
22
|
+
ensure
|
23
|
+
@scrammed = previously_scrammed
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require File.expand_path('../../test_helper', __FILE__)
|
4
|
+
|
5
|
+
class ReactorTest < ActiveSupport::TestCase
|
6
|
+
|
7
|
+
setup do
|
8
|
+
FruitReactor.calls.clear
|
9
|
+
PeelReactor.calls.clear
|
10
|
+
end
|
11
|
+
|
12
|
+
test 'Callbacks are invoked' do
|
13
|
+
b = Banana.create(color: 'blue')
|
14
|
+
b.peel!
|
15
|
+
c = Cherry.create(color: 'red')
|
16
|
+
c.peel!
|
17
|
+
assert_equal ['before_save blue Banana', 'after_create blue Banana', 'before_save red Cherry', 'after_create red Cherry'], FruitReactor.calls
|
18
|
+
assert_equal ['before_peel blue Banana', 'after_peel blue Banana'], PeelReactor.calls
|
19
|
+
end
|
20
|
+
|
21
|
+
test 'Scrammed reactors do not react' do
|
22
|
+
FruitReactor.scram do
|
23
|
+
b = Banana.create(color: 'blue')
|
24
|
+
c = Cherry.create(color: 'red')
|
25
|
+
end
|
26
|
+
assert_empty FruitReactor.calls
|
27
|
+
assert_equal false, FruitReactor.scrammed?
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class FruitReactor < ActiveRecord::Reactor
|
2
|
+
|
3
|
+
class << self
|
4
|
+
delegate :calls, to: :instance
|
5
|
+
end
|
6
|
+
|
7
|
+
def calls
|
8
|
+
@calls ||= []
|
9
|
+
end
|
10
|
+
|
11
|
+
def before_save(record)
|
12
|
+
puts "#{self.class.name}#before_save #{record.color} #{record.class.name}"
|
13
|
+
calls << "before_save #{record.color} #{record.class.name}"
|
14
|
+
end
|
15
|
+
|
16
|
+
def after_create(record)
|
17
|
+
puts "#{self.class.name}#after_create #{record.color} #{record.class.name}"
|
18
|
+
calls << "after_create #{record.color} #{record.class.name}"
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class PeelReactor < ActiveRecord::Reactor
|
2
|
+
|
3
|
+
class << self
|
4
|
+
delegate :calls, to: :instance
|
5
|
+
end
|
6
|
+
|
7
|
+
def calls
|
8
|
+
@calls ||= []
|
9
|
+
end
|
10
|
+
|
11
|
+
def before_peel(record)
|
12
|
+
puts "#{self.class.name}#before_peel #{record.color} #{record.class.name}"
|
13
|
+
calls << "before_peel #{record.color} #{record.class.name}"
|
14
|
+
end
|
15
|
+
|
16
|
+
def after_peel(record)
|
17
|
+
puts "#{self.class.name}#after_peel #{record.color} #{record.class.name}"
|
18
|
+
calls << "after_peel #{record.color} #{record.class.name}"
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
data/test/schema.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
module CustomAssertions
|
2
|
+
def assert_empty(collection)
|
3
|
+
fail "collection expected, got nil" if collection.nil?
|
4
|
+
assert collection.empty?
|
5
|
+
end
|
6
|
+
|
7
|
+
def assert_not_empty(collection)
|
8
|
+
fail "collection expected, got nil" if collection.nil?
|
9
|
+
assert !collection.empty?
|
10
|
+
end
|
11
|
+
|
12
|
+
def assert_float_equal(expected, actual, order = 1)
|
13
|
+
assert_in_delta expected, actual, Float::EPSILON * 10**order
|
14
|
+
end
|
15
|
+
|
16
|
+
def assert_valid(record)
|
17
|
+
fail "record expected, got nil" if record.nil?
|
18
|
+
result = record.valid?
|
19
|
+
errors = record.errors
|
20
|
+
assert result, "Expected #{record} to be valid, but had errors on #{errors.keys.map(&:to_s).to_sentence}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def assert_not_valid(record)
|
24
|
+
fail "record expected, got nil" if record.nil?
|
25
|
+
assert !record.valid?, "Expected #{record} not to be valid, but was"
|
26
|
+
end
|
27
|
+
|
28
|
+
def assert_error_on(record, attr_name, error = nil)
|
29
|
+
fail "record expected, got nil" if record.nil?
|
30
|
+
if error
|
31
|
+
errors = record.errors
|
32
|
+
#error_instances = errors[attr_name.to_s]
|
33
|
+
message = "Expected validation error of type '#{error}' on attribute '#{attr_name}'"
|
34
|
+
message << " but was on #{errors.keys.map { |k| "'#{k}'" }.to_sentence}" if errors.any? && !errors.keys.include?(attr_name.to_sym)
|
35
|
+
message << " but was valid" if errors.empty?
|
36
|
+
assert errors.added?(attr_name, error), message
|
37
|
+
else
|
38
|
+
assert record.errors[attr_name.to_sym], "Expected validation error on attribute '#{attr_name}'"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def assert_no_error_on(record, *attr_names)
|
43
|
+
fail "record expected, got nil" if record.nil?
|
44
|
+
attr_names = Array(attr_names)
|
45
|
+
if attr_names.empty?
|
46
|
+
assert record.errors.empty?, "Expected no errors on #{record}, but found #{record.errors.full_messages * ", "}"
|
47
|
+
else
|
48
|
+
attr_names.each do |attr_name|
|
49
|
+
assert record.errors[attr_name.to_sym].empty?, "Validation error on attribute #{attr_name}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def assert_save(record)
|
55
|
+
result = record.save
|
56
|
+
return assert(true) if result
|
57
|
+
assert_no_error_on record
|
58
|
+
fail "could not save record #{record.inspect}, although there were no validation errors"
|
59
|
+
end
|
60
|
+
|
61
|
+
def assert_attrs(record, attrs)
|
62
|
+
fail "record expected, got nil" if record.nil?
|
63
|
+
attrs.each do |attr_name, value|
|
64
|
+
assert_equal value, record.send(attr_name), attr_name
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
ENV["RAILS_ENV"] = "test"
|
2
|
+
|
3
|
+
require 'pathname'
|
4
|
+
|
5
|
+
if RUBY_VERSION >= '1.9'
|
6
|
+
require 'simplecov'
|
7
|
+
SimpleCov.start do
|
8
|
+
if artifacts_dir = ENV['CC_BUILD_ARTIFACTS']
|
9
|
+
coverage_dir Pathname.new(artifacts_dir).relative_path_from(Pathname.new(SimpleCov.root)).to_s
|
10
|
+
end
|
11
|
+
add_filter '/test/'
|
12
|
+
add_filter 'vendor'
|
13
|
+
end
|
14
|
+
|
15
|
+
SimpleCov.at_exit do
|
16
|
+
SimpleCov.result.format!
|
17
|
+
if result = SimpleCov.result
|
18
|
+
File.open(File.join(SimpleCov.coverage_path, 'coverage_percent.txt'), 'w') { |f| f << result.covered_percent.to_s }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'rubygems'
|
24
|
+
require 'bundler/setup'
|
25
|
+
Bundler.require(:default)
|
26
|
+
require 'test/unit'
|
27
|
+
require 'debugger'
|
28
|
+
|
29
|
+
require 'active_record'
|
30
|
+
require 'activerecord-reactor'
|
31
|
+
|
32
|
+
ActiveRecord::Base.mass_assignment_sanitizer = :strict
|
33
|
+
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
34
|
+
|
35
|
+
require File.expand_path('../schema', __FILE__)
|
36
|
+
require File.expand_path('../support/custom_assertions', __FILE__)
|
37
|
+
|
38
|
+
require File.expand_path('../models/fruit_reactor', __FILE__)
|
39
|
+
require File.expand_path('../models/peel_reactor', __FILE__)
|
40
|
+
require File.expand_path('../models/fruit', __FILE__)
|
41
|
+
require File.expand_path('../models/banana', __FILE__)
|
42
|
+
require File.expand_path('../models/cherry', __FILE__)
|
43
|
+
|
44
|
+
class ActiveSupport::TestCase
|
45
|
+
include CustomAssertions
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-reactor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matthias Grosser
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.3
|
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: 3.2.3
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: sqlite3
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: simplecov
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 0.8.7
|
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: 0.8.7
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: debugger
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: ''
|
95
|
+
email: mtgrosser@gmx.net
|
96
|
+
executables: []
|
97
|
+
extensions: []
|
98
|
+
extra_rdoc_files: []
|
99
|
+
files:
|
100
|
+
- lib/active_record/active_record.rb
|
101
|
+
- lib/active_record/reactor/callbacks.rb
|
102
|
+
- lib/active_record/reactor/version.rb
|
103
|
+
- lib/active_record/reactor.rb
|
104
|
+
- lib/activerecord-reactor.rb
|
105
|
+
- MIT-LICENSE
|
106
|
+
- README.md
|
107
|
+
- Rakefile
|
108
|
+
- test/models/banana.rb
|
109
|
+
- test/models/fruit.rb
|
110
|
+
- test/models/fruit_reactor.rb
|
111
|
+
- test/models/peel_reactor.rb
|
112
|
+
- test/models/cherry.rb
|
113
|
+
- test/cases/reactor_test.rb
|
114
|
+
- test/test_helper.rb
|
115
|
+
- test/support/custom_assertions.rb
|
116
|
+
- test/schema.rb
|
117
|
+
homepage: https://github.com/mtgrosser/activerecord-reactor
|
118
|
+
licenses: []
|
119
|
+
post_install_message:
|
120
|
+
rdoc_options: []
|
121
|
+
require_paths:
|
122
|
+
- lib
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
none: false
|
125
|
+
requirements:
|
126
|
+
- - ! '>='
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: 1.9.3
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
none: false
|
131
|
+
requirements:
|
132
|
+
- - ! '>='
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 1.8.23
|
138
|
+
signing_key:
|
139
|
+
specification_version: 3
|
140
|
+
summary: Unobtrusive observers for Rails models
|
141
|
+
test_files:
|
142
|
+
- test/models/banana.rb
|
143
|
+
- test/models/fruit.rb
|
144
|
+
- test/models/fruit_reactor.rb
|
145
|
+
- test/models/peel_reactor.rb
|
146
|
+
- test/models/cherry.rb
|
147
|
+
- test/cases/reactor_test.rb
|
148
|
+
- test/test_helper.rb
|
149
|
+
- test/support/custom_assertions.rb
|
150
|
+
- test/schema.rb
|