ohm-contrib 0.0.6 → 0.0.7
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/VERSION +1 -1
- data/lib/ohm/contrib.rb +1 -1
- data/lib/ohm/contrib/typecast.rb +20 -2
- data/ohm-contrib.gemspec +1 -1
- data/test/test_ohm_typecast.rb +17 -5
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.7
|
data/lib/ohm/contrib.rb
CHANGED
data/lib/ohm/contrib/typecast.rb
CHANGED
@@ -89,7 +89,25 @@ module Ohm
|
|
89
89
|
end
|
90
90
|
|
91
91
|
module Typecast
|
92
|
-
MissingValidation
|
92
|
+
class MissingValidation < StandardError
|
93
|
+
MESSAGE = "%s :%s is required in your #validate method"
|
94
|
+
|
95
|
+
attr :type
|
96
|
+
attr :field
|
97
|
+
|
98
|
+
def initialize(type, field)
|
99
|
+
@type, @field = type, field
|
100
|
+
end
|
101
|
+
|
102
|
+
def message
|
103
|
+
MESSAGE % [assertion, field]
|
104
|
+
end
|
105
|
+
|
106
|
+
protected
|
107
|
+
def assertion
|
108
|
+
'assert_type_%s' % type.name.split('::').last.downcase
|
109
|
+
end
|
110
|
+
end
|
93
111
|
|
94
112
|
include Types
|
95
113
|
include TypeAssertions
|
@@ -128,7 +146,7 @@ module Ohm
|
|
128
146
|
value = send(field)
|
129
147
|
|
130
148
|
unless value.kind_of?(type)
|
131
|
-
raise MissingValidation,
|
149
|
+
raise MissingValidation.new(type, field)
|
132
150
|
end
|
133
151
|
end
|
134
152
|
end
|
data/ohm-contrib.gemspec
CHANGED
data/test/test_ohm_typecast.rb
CHANGED
@@ -26,8 +26,12 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
26
26
|
should "raise a MissingValidation error during validation" do
|
27
27
|
product = Product.new(:price => "FooBar")
|
28
28
|
|
29
|
-
|
29
|
+
begin
|
30
30
|
product.valid?
|
31
|
+
rescue Ohm::Typecast::MissingValidation => ex
|
32
|
+
assert_match(/assert_type_decimal :price is required in your #validate method/, ex.message)
|
33
|
+
else
|
34
|
+
flunk "Should have raised MissingValidation"
|
31
35
|
end
|
32
36
|
end
|
33
37
|
end
|
@@ -78,8 +82,12 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
78
82
|
end
|
79
83
|
|
80
84
|
test "an invalid string" do
|
81
|
-
|
82
|
-
|
85
|
+
begin
|
86
|
+
Product.create(:start_of_sale => "invalid time")
|
87
|
+
rescue Ohm::Typecast::MissingValidation => ex
|
88
|
+
assert_match(/assert_type_time :start_of_sale is required in your #validate method/, ex.message)
|
89
|
+
else
|
90
|
+
flunk "Should have raised MissingValidation"
|
83
91
|
end
|
84
92
|
end
|
85
93
|
|
@@ -138,8 +146,12 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
138
146
|
end
|
139
147
|
|
140
148
|
test "assigning a string which is not a valid date" do
|
141
|
-
|
142
|
-
|
149
|
+
begin
|
150
|
+
Product.create(:date_bought => "Bla Bla")
|
151
|
+
rescue Ohm::Typecast::MissingValidation => ex
|
152
|
+
assert_match(/assert_type_date :date_bought is required in your #validate method/, ex.message)
|
153
|
+
else
|
154
|
+
flunk "Should have raised MissingValidation"
|
143
155
|
end
|
144
156
|
end
|
145
157
|
|