ohm-contrib 0.0.16 → 0.0.17
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/README.markdown +1 -1
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/ohm/contrib/boundaries.rb +2 -2
- data/lib/ohm/contrib/callbacks.rb +10 -8
- data/lib/ohm/contrib/extra_validations.rb +3 -3
- data/lib/ohm/contrib/locking.rb +3 -3
- data/lib/ohm/contrib/number_validations.rb +1 -1
- data/lib/ohm/contrib/timestamping.rb +1 -1
- data/lib/ohm/contrib/to_hash.rb +2 -2
- data/lib/ohm/contrib/typecast.rb +15 -15
- data/lib/ohm/contrib/web_validations.rb +1 -1
- data/lib/ohm/contrib.rb +1 -1
- data/ohm-contrib.gemspec +1 -1
- data/test/test_ohm_contrib.rb +1 -1
- data/test/test_ohm_contrib_callbacks.rb +38 -14
- data/test/test_ohm_extra_validations.rb +1 -1
- data/test/test_ohm_typecast.rb +9 -9
- metadata +2 -2
data/README.markdown
CHANGED
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.17
|
@@ -36,7 +36,7 @@ module Ohm
|
|
36
36
|
# end
|
37
37
|
# end
|
38
38
|
#
|
39
|
-
|
39
|
+
|
40
40
|
module Callbacks
|
41
41
|
def self.included(base)
|
42
42
|
base.extend Macros
|
@@ -66,7 +66,7 @@ module Ohm
|
|
66
66
|
#
|
67
67
|
# @param [Symbol] method the method type, `:validate`, `:create`, or `:save`
|
68
68
|
# @param [Symbol] callback the name of the method to execute
|
69
|
-
# @return [Array<callback>, nil] the callback wrapped in an array or nil
|
69
|
+
# @return [Array<callback>, nil] the callback wrapped in an array or nil
|
70
70
|
# if the callback exists
|
71
71
|
def before(method, callback)
|
72
72
|
unless callbacks[:before][method].include? callback
|
@@ -97,18 +97,20 @@ module Ohm
|
|
97
97
|
#
|
98
98
|
# @param [Symbol] method the method type, `:validate`, `:create`, or `:save`
|
99
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
|
100
|
+
# @return [Array<callback>, nil] the callback in an array or nil if the
|
101
101
|
# callback exists
|
102
102
|
def after(method, callback)
|
103
|
-
callbacks[:after][method]
|
103
|
+
unless callbacks[:after][method].include? callback
|
104
|
+
callbacks[:after][method] << callback
|
105
|
+
end
|
104
106
|
end
|
105
|
-
|
107
|
+
|
106
108
|
# @private internally used to maintain the state of callbacks
|
107
109
|
def callbacks
|
108
110
|
@callbacks ||= Hash.new { |h, k| h[k] = Hash.new { |h, k| h[k] = [] }}
|
109
111
|
end
|
110
112
|
end
|
111
|
-
|
113
|
+
|
112
114
|
# Overrides the validate method of Ohm::Model. This is a bit tricky,
|
113
115
|
# since typically you override this. Make sure you do something like:
|
114
116
|
#
|
@@ -128,7 +130,7 @@ module Ohm
|
|
128
130
|
super
|
129
131
|
execute_callback(:after, :validate)
|
130
132
|
end
|
131
|
-
|
133
|
+
|
132
134
|
# The overriden create of Ohm::Model. It checks if the
|
133
135
|
# model is valid, and executes all before :create callbacks.
|
134
136
|
#
|
@@ -141,7 +143,7 @@ module Ohm
|
|
141
143
|
execute_callback(:after, :create) if is_created
|
142
144
|
end
|
143
145
|
end
|
144
|
-
|
146
|
+
|
145
147
|
# The overridden save of Ohm::Model. It checks if the model
|
146
148
|
# is valid, and executes all before :save callbacks.
|
147
149
|
#
|
@@ -5,7 +5,7 @@ module Ohm
|
|
5
5
|
#
|
6
6
|
# class Post < Ohm::Model
|
7
7
|
# include Ohm::ExtraValidations
|
8
|
-
#
|
8
|
+
#
|
9
9
|
# attribute :price
|
10
10
|
# attribute :state
|
11
11
|
# attribute :slug
|
@@ -36,6 +36,6 @@ module Ohm
|
|
36
36
|
protected
|
37
37
|
def assert_member(att, set, error = [att, :not_member])
|
38
38
|
assert set.include?(send(att)), error
|
39
|
-
end
|
39
|
+
end
|
40
40
|
end
|
41
|
-
end
|
41
|
+
end
|
data/lib/ohm/contrib/locking.rb
CHANGED
@@ -5,9 +5,9 @@ module Ohm
|
|
5
5
|
# In addition, since future ohm versions might drop mutexes, I thought it
|
6
6
|
# might be a good idea to preseve this feature as a drop-in module.
|
7
7
|
module Locking
|
8
|
-
# Lock the object before executing the block, and release it once the block
|
8
|
+
# Lock the object before executing the block, and release it once the block
|
9
9
|
# is done.
|
10
|
-
#
|
10
|
+
#
|
11
11
|
# @example
|
12
12
|
#
|
13
13
|
# post = Order.create(:customer => Customer.create)
|
@@ -52,4 +52,4 @@ module Ohm
|
|
52
52
|
lock.to_f < Time.now.to_f
|
53
53
|
end
|
54
54
|
end
|
55
|
-
end
|
55
|
+
end
|
data/lib/ohm/contrib/to_hash.rb
CHANGED
@@ -3,7 +3,7 @@ module Ohm
|
|
3
3
|
# is that it chose the albeit better whitelisted approach.
|
4
4
|
#
|
5
5
|
# @example
|
6
|
-
#
|
6
|
+
#
|
7
7
|
# # this is the core Ohm#to_hash implementation
|
8
8
|
# class Post < Ohm::Model
|
9
9
|
# attribute :body
|
@@ -34,4 +34,4 @@ module Ohm
|
|
34
34
|
end
|
35
35
|
alias :to_h :to_hash
|
36
36
|
end
|
37
|
-
end
|
37
|
+
end
|
data/lib/ohm/contrib/typecast.rb
CHANGED
@@ -71,10 +71,10 @@ module Ohm
|
|
71
71
|
class Time < Primitive
|
72
72
|
class << self
|
73
73
|
extend Forwardable
|
74
|
-
def_delegators :Time,
|
75
|
-
:_load, :apply_offset, :at, :gm, :httpdate, :json_create, :local,
|
76
|
-
:make_time, :mktime, :month_days, :now, :parse, :rfc2822,
|
77
|
-
:strptime, :utc, :w3cdtf, :xmlschema, :yaml_new, :zone_offset,
|
74
|
+
def_delegators :Time,
|
75
|
+
:_load, :apply_offset, :at, :gm, :httpdate, :json_create, :local,
|
76
|
+
:make_time, :mktime, :month_days, :now, :parse, :rfc2822,
|
77
|
+
:strptime, :utc, :w3cdtf, :xmlschema, :yaml_new, :zone_offset,
|
78
78
|
:zone_utc?
|
79
79
|
end
|
80
80
|
|
@@ -87,10 +87,10 @@ module Ohm
|
|
87
87
|
class Date < Primitive
|
88
88
|
class << self
|
89
89
|
extend Forwardable
|
90
|
-
def_delegators :Date,
|
91
|
-
:_parse, :_strptime, :civil, :commercial, :gregorian_leap?, :jd,
|
92
|
-
:json_create, :julian_leap?, :now, :nth_kday, :ordinal, :parse, :s3e,
|
93
|
-
:strptime, :today, :valid_civil?, :valid_commercial?, :valid_jd?,
|
90
|
+
def_delegators :Date,
|
91
|
+
:_parse, :_strptime, :civil, :commercial, :gregorian_leap?, :jd,
|
92
|
+
:json_create, :julian_leap?, :now, :nth_kday, :ordinal, :parse, :s3e,
|
93
|
+
:strptime, :today, :valid_civil?, :valid_commercial?, :valid_jd?,
|
94
94
|
:valid_ordinal?, :weeknum
|
95
95
|
end
|
96
96
|
|
@@ -100,14 +100,14 @@ module Ohm
|
|
100
100
|
end
|
101
101
|
end
|
102
102
|
end
|
103
|
-
|
104
|
-
# Provides unobtrusive, non-explosive typecasting.Instead of exploding on set
|
105
|
-
# of an invalid value, this module takes the approach of just taking in
|
106
|
-
# parameters and letting you do validation yourself. The only thing this
|
103
|
+
|
104
|
+
# Provides unobtrusive, non-explosive typecasting.Instead of exploding on set
|
105
|
+
# of an invalid value, this module takes the approach of just taking in
|
106
|
+
# parameters and letting you do validation yourself. The only thing this
|
107
107
|
# module does for you is the boilerplate casting you might need to do.
|
108
108
|
#
|
109
109
|
# @example
|
110
|
-
#
|
110
|
+
#
|
111
111
|
# # without typecasting
|
112
112
|
# class Item < Ohm::Model
|
113
113
|
# attribute :price
|
@@ -122,7 +122,7 @@ module Ohm
|
|
122
122
|
# # => true
|
123
123
|
#
|
124
124
|
# # you can opt to manually cast everytime, or do it in the model, i.e.
|
125
|
-
#
|
125
|
+
#
|
126
126
|
# class Item
|
127
127
|
# def price
|
128
128
|
# BigDecimal(read_local(:price))
|
@@ -174,4 +174,4 @@ module Ohm
|
|
174
174
|
end
|
175
175
|
end
|
176
176
|
end
|
177
|
-
end
|
177
|
+
end
|
data/lib/ohm/contrib.rb
CHANGED
data/ohm-contrib.gemspec
CHANGED
data/test/test_ohm_contrib.rb
CHANGED
@@ -5,7 +5,7 @@ class OhmContribCallbacksTest < Test::Unit::TestCase
|
|
5
5
|
include Ohm::Callbacks
|
6
6
|
|
7
7
|
attribute :body
|
8
|
-
|
8
|
+
|
9
9
|
before :validate, :do_before_validate
|
10
10
|
after :validate, :do_after_validate
|
11
11
|
|
@@ -25,13 +25,25 @@ class OhmContribCallbacksTest < Test::Unit::TestCase
|
|
25
25
|
instance_variable_get("@#{ action }")
|
26
26
|
end
|
27
27
|
|
28
|
+
def count(action)
|
29
|
+
instance_variable_get("@#{ action }")
|
30
|
+
end
|
31
|
+
|
28
32
|
protected
|
29
|
-
def do_before_validate()
|
30
|
-
def do_after_validate()
|
31
|
-
def do_before_create()
|
32
|
-
def do_after_create()
|
33
|
-
def do_before_save()
|
34
|
-
def do_after_save()
|
33
|
+
def do_before_validate() incr(:do_before_validate) end
|
34
|
+
def do_after_validate() incr(:do_after_validate) end
|
35
|
+
def do_before_create() incr(:do_before_create) end
|
36
|
+
def do_after_create() incr(:do_after_create) end
|
37
|
+
def do_before_save() incr(:do_before_save) end
|
38
|
+
def do_after_save() incr(:do_after_save) end
|
39
|
+
|
40
|
+
def incr(action)
|
41
|
+
val = instance_variable_get("@#{ action }")
|
42
|
+
val ||= 0
|
43
|
+
val += 1
|
44
|
+
|
45
|
+
instance_variable_set("@#{ action }", val)
|
46
|
+
end
|
35
47
|
end
|
36
48
|
|
37
49
|
context "on save when invalid state" do
|
@@ -39,9 +51,9 @@ class OhmContribCallbacksTest < Test::Unit::TestCase
|
|
39
51
|
@post = Post.new
|
40
52
|
@post.save
|
41
53
|
end
|
42
|
-
|
54
|
+
|
43
55
|
should "still call before / after validate" do
|
44
|
-
assert @post.did?(:do_before_validate)
|
56
|
+
assert @post.did?(:do_before_validate)
|
45
57
|
assert @post.did?(:do_after_validate)
|
46
58
|
end
|
47
59
|
|
@@ -58,7 +70,7 @@ class OhmContribCallbacksTest < Test::Unit::TestCase
|
|
58
70
|
@post = Post.new(:body => "The Body")
|
59
71
|
@post.save
|
60
72
|
end
|
61
|
-
|
73
|
+
|
62
74
|
should "call all callbacks" do
|
63
75
|
assert @post.did?(:do_before_validate)
|
64
76
|
assert @post.did?(:do_after_validate)
|
@@ -67,6 +79,13 @@ class OhmContribCallbacksTest < Test::Unit::TestCase
|
|
67
79
|
assert @post.did?(:do_before_save)
|
68
80
|
assert @post.did?(:do_after_save)
|
69
81
|
end
|
82
|
+
|
83
|
+
should "call create / save callbacks only once" do
|
84
|
+
assert_equal 1, @post.count(:do_before_create)
|
85
|
+
assert_equal 1, @post.count(:do_after_create)
|
86
|
+
assert_equal 1, @post.count(:do_before_save)
|
87
|
+
assert_equal 1, @post.count(:do_after_create)
|
88
|
+
end
|
70
89
|
end
|
71
90
|
|
72
91
|
context "on save of an existing object" do
|
@@ -76,7 +95,7 @@ class OhmContribCallbacksTest < Test::Unit::TestCase
|
|
76
95
|
|
77
96
|
@post.save
|
78
97
|
end
|
79
|
-
|
98
|
+
|
80
99
|
should "not call create related callbacks" do
|
81
100
|
assert ! @post.did?(:do_before_create)
|
82
101
|
assert ! @post.did?(:do_after_create)
|
@@ -87,7 +106,12 @@ class OhmContribCallbacksTest < Test::Unit::TestCase
|
|
87
106
|
assert @post.did?(:do_after_validate)
|
88
107
|
assert @post.did?(:do_before_save)
|
89
108
|
assert @post.did?(:do_after_save)
|
90
|
-
end
|
109
|
+
end
|
110
|
+
|
111
|
+
should "call save callbacks only once" do
|
112
|
+
assert_equal 1, @post.count(:do_before_save)
|
113
|
+
assert_equal 1, @post.count(:do_after_save)
|
114
|
+
end
|
91
115
|
end
|
92
116
|
|
93
117
|
context "on save of an existing invalid object" do
|
@@ -103,7 +127,7 @@ class OhmContribCallbacksTest < Test::Unit::TestCase
|
|
103
127
|
assert @post.did?(:do_before_validate)
|
104
128
|
assert @post.did?(:do_after_validate)
|
105
129
|
end
|
106
|
-
|
130
|
+
|
107
131
|
should "not call create related callbacks" do
|
108
132
|
assert ! @post.did?(:do_before_create)
|
109
133
|
assert ! @post.did?(:do_after_create)
|
@@ -114,4 +138,4 @@ class OhmContribCallbacksTest < Test::Unit::TestCase
|
|
114
138
|
assert ! @post.did?(:do_after_save)
|
115
139
|
end
|
116
140
|
end
|
117
|
-
end
|
141
|
+
end
|
data/test/test_ohm_typecast.rb
CHANGED
@@ -209,7 +209,7 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
209
209
|
Time
|
210
210
|
end
|
211
211
|
end
|
212
|
-
|
212
|
+
|
213
213
|
test "still able to access top level Time" do
|
214
214
|
post = Post.new
|
215
215
|
assert_equal post.now.to_s, Time.now.to_s
|
@@ -219,11 +219,11 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
219
219
|
post = Post.new
|
220
220
|
methods = [
|
221
221
|
:_load, :apply_offset, :at, :gm, :httpdate, :json_create, :local,
|
222
|
-
:make_time, :mktime, :month_days, :new, :now, :parse, :rfc2822,
|
223
|
-
:strptime, :utc, :w3cdtf, :xmlschema, :yaml_new, :zone_offset,
|
222
|
+
:make_time, :mktime, :month_days, :new, :now, :parse, :rfc2822,
|
223
|
+
:strptime, :utc, :w3cdtf, :xmlschema, :yaml_new, :zone_offset,
|
224
224
|
:zone_utc?
|
225
225
|
]
|
226
|
-
|
226
|
+
|
227
227
|
methods.each do |m|
|
228
228
|
assert_respond_to post.time, m
|
229
229
|
end
|
@@ -281,7 +281,7 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
281
281
|
Date.today
|
282
282
|
end
|
283
283
|
end
|
284
|
-
|
284
|
+
|
285
285
|
test "still able to get top level methods" do
|
286
286
|
assert_equal Date.today, Post.new.base_today
|
287
287
|
end
|
@@ -290,9 +290,9 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
290
290
|
post = Post.new
|
291
291
|
|
292
292
|
methods = [
|
293
|
-
:_parse, :_strptime, :civil, :commercial, :gregorian_leap?, :jd,
|
294
|
-
:json_create, :julian_leap?, :now, :nth_kday, :ordinal, :parse, :s3e,
|
295
|
-
:strptime, :today, :valid_civil?, :valid_commercial?, :valid_jd?,
|
293
|
+
:_parse, :_strptime, :civil, :commercial, :gregorian_leap?, :jd,
|
294
|
+
:json_create, :julian_leap?, :now, :nth_kday, :ordinal, :parse, :s3e,
|
295
|
+
:strptime, :today, :valid_civil?, :valid_commercial?, :valid_jd?,
|
296
296
|
:valid_ordinal?, :weeknum
|
297
297
|
]
|
298
298
|
methods.each do |m|
|
@@ -335,4 +335,4 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
335
335
|
assert_equal Date.today, Post.new.today
|
336
336
|
end
|
337
337
|
end
|
338
|
-
end
|
338
|
+
end
|