ohm-contrib 0.0.13 → 0.0.14
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 +3 -1
- data/VERSION +1 -1
- data/lib/ohm/contrib/callbacks.rb +165 -0
- data/lib/ohm/contrib.rb +2 -1
- data/ohm-contrib.gemspec +5 -2
- data/test/test_ohm_contrib_callbacks.rb +117 -0
- metadata +6 -3
data/.gitignore
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.14
|
@@ -0,0 +1,165 @@
|
|
1
|
+
module Ohm
|
2
|
+
# Minimalist callback support for Ohm::Model.
|
3
|
+
#
|
4
|
+
# @example
|
5
|
+
#
|
6
|
+
# class Post < Ohm::Model
|
7
|
+
# include Ohm::Callbacks
|
8
|
+
#
|
9
|
+
# before :validate, :clean_decimals
|
10
|
+
#
|
11
|
+
# before :create, :timestamp!
|
12
|
+
# before :save, :recalc_votes
|
13
|
+
#
|
14
|
+
# after :create, :post_to_twitter!
|
15
|
+
# after :save, :sync_ids
|
16
|
+
#
|
17
|
+
# protected
|
18
|
+
# def clean_decimals
|
19
|
+
# # sanitize the decimal values here
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# def timestamp!
|
23
|
+
# # do timestamping code here
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# def recalc_votes
|
27
|
+
# # do something here
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# def post_to_twitter!
|
31
|
+
# # do twitter posting here
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# def sync_ids
|
35
|
+
# # do something with the ids
|
36
|
+
# end
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
|
40
|
+
module Callbacks
|
41
|
+
def self.included(base)
|
42
|
+
base.extend Macros
|
43
|
+
end
|
44
|
+
|
45
|
+
module Macros
|
46
|
+
# Use to add a before callback on `method`. Only symbols
|
47
|
+
# are allowed, no string eval, no block option also.
|
48
|
+
#
|
49
|
+
# @example
|
50
|
+
#
|
51
|
+
# class Post < Ohm::Model
|
52
|
+
# include Ohm::Callbacks
|
53
|
+
#
|
54
|
+
# before :create, :timestamp!
|
55
|
+
# before :save, :recalc_votes
|
56
|
+
#
|
57
|
+
# protected
|
58
|
+
# def timestamp!
|
59
|
+
# # do timestamping code here
|
60
|
+
# end
|
61
|
+
#
|
62
|
+
# def recalc_votes
|
63
|
+
# # do something here
|
64
|
+
# end
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# @param [Symbol] method the method type, `:validate`, `:create`, or `:save`
|
68
|
+
# @param [Symbol] callback the name of the method to execute
|
69
|
+
# @return [Array<callback>, nil] the callback wrapped in an array or nil
|
70
|
+
# if the callback exists
|
71
|
+
def before(method, callback)
|
72
|
+
unless callbacks[:before][method].include? callback
|
73
|
+
callbacks[:before][method] << callback
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# Use to add an after callback on `method`. Only symbols
|
78
|
+
# are allowed, no string eval, no block option also.
|
79
|
+
#
|
80
|
+
# @example
|
81
|
+
#
|
82
|
+
# class Post < Ohm::Model
|
83
|
+
# include Ohm::Callbacks
|
84
|
+
#
|
85
|
+
# after :create, :post_to_twitter!
|
86
|
+
# after :save, :sync_ids
|
87
|
+
#
|
88
|
+
# protected
|
89
|
+
# def post_to_twitter!
|
90
|
+
# # do twitter posting here
|
91
|
+
# end
|
92
|
+
#
|
93
|
+
# def sync_ids
|
94
|
+
# # do something with the ids
|
95
|
+
# end
|
96
|
+
# end
|
97
|
+
#
|
98
|
+
# @param [Symbol] method the method type, `:validate`, `:create`, or `:save`
|
99
|
+
# @param [Symbol] callback the name of the method to execute
|
100
|
+
# @return [Array<callback>, nil] the callback in an array or nil if the
|
101
|
+
# callback exists
|
102
|
+
def after(method, callback)
|
103
|
+
callbacks[:after][method] << callback
|
104
|
+
end
|
105
|
+
|
106
|
+
# @private internally used to maintain the state of callbacks
|
107
|
+
def callbacks
|
108
|
+
@callbacks ||= Hash.new { |h, k| h[k] = Hash.new { |h, k| h[k] = [] }}
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
# Overrides the validate method of Ohm::Model. This is a bit tricky,
|
113
|
+
# since typically you override this. Make sure you do something like:
|
114
|
+
#
|
115
|
+
# def validate
|
116
|
+
# super
|
117
|
+
#
|
118
|
+
# # do your assertions
|
119
|
+
# end
|
120
|
+
#
|
121
|
+
# This ensures that you call this method when you defined your own validate
|
122
|
+
# method.
|
123
|
+
#
|
124
|
+
# In all honesty, I don't see the value of putting this here, and I'm still
|
125
|
+
# weighing if this is _really_ needed.
|
126
|
+
def validate
|
127
|
+
execute_callback(:before, :validate)
|
128
|
+
super
|
129
|
+
execute_callback(:after, :validate)
|
130
|
+
end
|
131
|
+
|
132
|
+
# The overriden create of Ohm::Model. It checks if the
|
133
|
+
# model is valid, and executes all before :create callbacks.
|
134
|
+
#
|
135
|
+
# If the create succeeds, all after :create callbacks are
|
136
|
+
# executed.
|
137
|
+
def create
|
138
|
+
execute_callback(:before, :create) if valid?
|
139
|
+
|
140
|
+
super.tap do |is_created|
|
141
|
+
execute_callback(:after, :create) if is_created
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
# The overridden save of Ohm::Model. It checks if the model
|
146
|
+
# is valid, and executes all before :save callbacks.
|
147
|
+
#
|
148
|
+
# If the save also succeeds, all after :save callbacks are
|
149
|
+
# executed.
|
150
|
+
def save
|
151
|
+
execute_callback(:before, :save) if valid?
|
152
|
+
|
153
|
+
super.tap do |is_saved|
|
154
|
+
execute_callback(:after, :save) if is_saved
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
private
|
159
|
+
def execute_callback(position, method)
|
160
|
+
self.class.callbacks[position][method].each do |callback|
|
161
|
+
__send__(callback)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
data/lib/ohm/contrib.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Ohm
|
2
2
|
module Contrib
|
3
|
-
VERSION = '0.0.
|
3
|
+
VERSION = '0.0.14'
|
4
4
|
end
|
5
5
|
|
6
6
|
autoload :Boundaries, "ohm/contrib/boundaries"
|
@@ -11,4 +11,5 @@ module Ohm
|
|
11
11
|
autoload :ExtraValidations, "ohm/contrib/extra_validations"
|
12
12
|
autoload :Typecast, "ohm/contrib/typecast"
|
13
13
|
autoload :Locking, "ohm/contrib/locking"
|
14
|
+
autoload :Callbacks, "ohm/contrib/callbacks"
|
14
15
|
end
|
data/ohm-contrib.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{ohm-contrib}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.14"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Cyril David"]
|
12
|
-
s.date = %q{2010-05-
|
12
|
+
s.date = %q{2010-05-23}
|
13
13
|
s.description = %q{Highly decoupled drop-in functionality for Ohm models}
|
14
14
|
s.email = %q{cyx.ucron@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
|
|
25
25
|
"VERSION",
|
26
26
|
"lib/ohm/contrib.rb",
|
27
27
|
"lib/ohm/contrib/boundaries.rb",
|
28
|
+
"lib/ohm/contrib/callbacks.rb",
|
28
29
|
"lib/ohm/contrib/extra_validations.rb",
|
29
30
|
"lib/ohm/contrib/locking.rb",
|
30
31
|
"lib/ohm/contrib/number_validations.rb",
|
@@ -36,6 +37,7 @@ Gem::Specification.new do |s|
|
|
36
37
|
"test/helper.rb",
|
37
38
|
"test/test_ohm_boundaries.rb",
|
38
39
|
"test/test_ohm_contrib.rb",
|
40
|
+
"test/test_ohm_contrib_callbacks.rb",
|
39
41
|
"test/test_ohm_extra_validations.rb",
|
40
42
|
"test/test_ohm_number_validations.rb",
|
41
43
|
"test/test_ohm_timestamping.rb",
|
@@ -52,6 +54,7 @@ Gem::Specification.new do |s|
|
|
52
54
|
"test/helper.rb",
|
53
55
|
"test/test_ohm_boundaries.rb",
|
54
56
|
"test/test_ohm_contrib.rb",
|
57
|
+
"test/test_ohm_contrib_callbacks.rb",
|
55
58
|
"test/test_ohm_extra_validations.rb",
|
56
59
|
"test/test_ohm_number_validations.rb",
|
57
60
|
"test/test_ohm_timestamping.rb",
|
@@ -0,0 +1,117 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
class OhmContribCallbacksTest < Test::Unit::TestCase
|
4
|
+
class Post < Ohm::Model
|
5
|
+
include Ohm::Callbacks
|
6
|
+
|
7
|
+
attribute :body
|
8
|
+
|
9
|
+
before :validate, :do_before_validate
|
10
|
+
after :validate, :do_after_validate
|
11
|
+
|
12
|
+
before :create, :do_before_create
|
13
|
+
after :create, :do_after_create
|
14
|
+
|
15
|
+
before :save, :do_before_save
|
16
|
+
after :save, :do_after_save
|
17
|
+
|
18
|
+
def validate
|
19
|
+
super
|
20
|
+
|
21
|
+
assert_present :body
|
22
|
+
end
|
23
|
+
|
24
|
+
def did?(action)
|
25
|
+
instance_variable_get("@#{ action }")
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
def do_before_validate() @do_before_validate = true end
|
30
|
+
def do_after_validate() @do_after_validate = true end
|
31
|
+
def do_before_create() @do_before_create = true end
|
32
|
+
def do_after_create() @do_after_create = true end
|
33
|
+
def do_before_save() @do_before_save = true end
|
34
|
+
def do_after_save() @do_after_save = true end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "on save when invalid state" do
|
38
|
+
setup do
|
39
|
+
@post = Post.new
|
40
|
+
@post.save
|
41
|
+
end
|
42
|
+
|
43
|
+
should "still call before / after validate" do
|
44
|
+
assert @post.did?(:do_before_validate)
|
45
|
+
assert @post.did?(:do_after_validate)
|
46
|
+
end
|
47
|
+
|
48
|
+
should "not call all other callbacks" do
|
49
|
+
assert ! @post.did?(:do_before_create)
|
50
|
+
assert ! @post.did?(:do_after_create)
|
51
|
+
assert ! @post.did?(:do_before_save)
|
52
|
+
assert ! @post.did?(:do_after_save)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context "on save when valid state" do
|
57
|
+
setup do
|
58
|
+
@post = Post.new(:body => "The Body")
|
59
|
+
@post.save
|
60
|
+
end
|
61
|
+
|
62
|
+
should "call all callbacks" do
|
63
|
+
assert @post.did?(:do_before_validate)
|
64
|
+
assert @post.did?(:do_after_validate)
|
65
|
+
assert @post.did?(:do_before_create)
|
66
|
+
assert @post.did?(:do_after_create)
|
67
|
+
assert @post.did?(:do_before_save)
|
68
|
+
assert @post.did?(:do_after_save)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context "on save of an existing object" do
|
73
|
+
setup do
|
74
|
+
@post = Post.create(:body => "The Body")
|
75
|
+
@post = Post[@post.id]
|
76
|
+
|
77
|
+
@post.save
|
78
|
+
end
|
79
|
+
|
80
|
+
should "not call create related callbacks" do
|
81
|
+
assert ! @post.did?(:do_before_create)
|
82
|
+
assert ! @post.did?(:do_after_create)
|
83
|
+
end
|
84
|
+
|
85
|
+
should "call the rest of the callbacks" do
|
86
|
+
assert @post.did?(:do_before_validate)
|
87
|
+
assert @post.did?(:do_after_validate)
|
88
|
+
assert @post.did?(:do_before_save)
|
89
|
+
assert @post.did?(:do_after_save)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context "on save of an existing invalid object" do
|
94
|
+
setup do
|
95
|
+
@post = Post.create(:body => "The Body")
|
96
|
+
@post = Post[@post.id]
|
97
|
+
|
98
|
+
@post.body = nil
|
99
|
+
@post.save
|
100
|
+
end
|
101
|
+
|
102
|
+
should "call validation related callbacks" do
|
103
|
+
assert @post.did?(:do_before_validate)
|
104
|
+
assert @post.did?(:do_after_validate)
|
105
|
+
end
|
106
|
+
|
107
|
+
should "not call create related callbacks" do
|
108
|
+
assert ! @post.did?(:do_before_create)
|
109
|
+
assert ! @post.did?(:do_after_create)
|
110
|
+
end
|
111
|
+
|
112
|
+
should "also not call save related callbacks" do
|
113
|
+
assert ! @post.did?(:do_before_save)
|
114
|
+
assert ! @post.did?(:do_after_save)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 14
|
9
|
+
version: 0.0.14
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Cyril David
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-05-
|
17
|
+
date: 2010-05-23 00:00:00 +08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -95,6 +95,7 @@ files:
|
|
95
95
|
- VERSION
|
96
96
|
- lib/ohm/contrib.rb
|
97
97
|
- lib/ohm/contrib/boundaries.rb
|
98
|
+
- lib/ohm/contrib/callbacks.rb
|
98
99
|
- lib/ohm/contrib/extra_validations.rb
|
99
100
|
- lib/ohm/contrib/locking.rb
|
100
101
|
- lib/ohm/contrib/number_validations.rb
|
@@ -106,6 +107,7 @@ files:
|
|
106
107
|
- test/helper.rb
|
107
108
|
- test/test_ohm_boundaries.rb
|
108
109
|
- test/test_ohm_contrib.rb
|
110
|
+
- test/test_ohm_contrib_callbacks.rb
|
109
111
|
- test/test_ohm_extra_validations.rb
|
110
112
|
- test/test_ohm_number_validations.rb
|
111
113
|
- test/test_ohm_timestamping.rb
|
@@ -146,6 +148,7 @@ test_files:
|
|
146
148
|
- test/helper.rb
|
147
149
|
- test/test_ohm_boundaries.rb
|
148
150
|
- test/test_ohm_contrib.rb
|
151
|
+
- test/test_ohm_contrib_callbacks.rb
|
149
152
|
- test/test_ohm_extra_validations.rb
|
150
153
|
- test/test_ohm_number_validations.rb
|
151
154
|
- test/test_ohm_timestamping.rb
|