ohm-contrib 0.0.10 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +27 -1
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/ohm/contrib/typecast.rb +12 -4
- data/lib/ohm/contrib.rb +2 -2
- data/ohm-contrib.gemspec +1 -1
- data/test/helper.rb +1 -1
- data/test/test_ohm_typecast.rb +68 -21
- metadata +2 -2
data/README.markdown
CHANGED
@@ -8,6 +8,9 @@ List of modules
|
|
8
8
|
* `Ohm::Boundaries`
|
9
9
|
* `Ohm::Timestamping`
|
10
10
|
* `Ohm::ToHash`
|
11
|
+
* `Ohm::WebValidations`
|
12
|
+
* `Ohm::NumberValidations`
|
13
|
+
* `Ohm::Typecast`
|
11
14
|
|
12
15
|
Example usage
|
13
16
|
-------------
|
@@ -51,7 +54,7 @@ Example usage
|
|
51
54
|
# Casting example
|
52
55
|
class Product
|
53
56
|
include Ohm::Typecast
|
54
|
-
|
57
|
+
|
55
58
|
attribute :price, Decimal
|
56
59
|
attribute :start_of_sale, Time
|
57
60
|
attribute :end_of_sale, Time
|
@@ -59,6 +62,29 @@ Example usage
|
|
59
62
|
attribute :rating, Float
|
60
63
|
end
|
61
64
|
|
65
|
+
Typecasting explained
|
66
|
+
---------------------
|
67
|
+
require 'ohm'
|
68
|
+
require 'ohm/contrib'
|
69
|
+
|
70
|
+
class Post < Ohm::Model
|
71
|
+
include Ohm::Typecast
|
72
|
+
|
73
|
+
attribute :price, Decimal
|
74
|
+
attribute :available_at, Time
|
75
|
+
attribute :stock, Integer
|
76
|
+
end
|
77
|
+
|
78
|
+
post = Post.create(:price => "10.20", :stock => "100")
|
79
|
+
post.price.to_s == "10.20"
|
80
|
+
# => true
|
81
|
+
|
82
|
+
post.price * 2 == 20.40
|
83
|
+
# => true
|
84
|
+
|
85
|
+
post.stock / 10 == 10
|
86
|
+
# => true
|
87
|
+
|
62
88
|
Credits
|
63
89
|
-------
|
64
90
|
Thanks to github user gnrfan for the web validations.
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.11
|
data/lib/ohm/contrib/typecast.rb
CHANGED
@@ -10,11 +10,15 @@ module Ohm
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def to_s
|
13
|
-
@raw
|
13
|
+
@raw.to_s
|
14
14
|
end
|
15
15
|
|
16
16
|
def inspect
|
17
|
-
|
17
|
+
object
|
18
|
+
end
|
19
|
+
|
20
|
+
def ==(other)
|
21
|
+
to_s == other.to_s
|
18
22
|
end
|
19
23
|
|
20
24
|
protected
|
@@ -31,6 +35,10 @@ module Ohm
|
|
31
35
|
end
|
32
36
|
|
33
37
|
class Decimal < Primitive
|
38
|
+
def inspect
|
39
|
+
object.to_s('F')
|
40
|
+
end
|
41
|
+
|
34
42
|
protected
|
35
43
|
def object
|
36
44
|
::Kernel::BigDecimal(@raw)
|
@@ -65,7 +73,7 @@ module Ohm
|
|
65
73
|
end
|
66
74
|
end
|
67
75
|
end
|
68
|
-
|
76
|
+
|
69
77
|
module Typecast
|
70
78
|
include Types
|
71
79
|
|
@@ -88,4 +96,4 @@ module Ohm
|
|
88
96
|
end
|
89
97
|
end
|
90
98
|
end
|
91
|
-
end
|
99
|
+
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.11'
|
4
4
|
end
|
5
5
|
|
6
6
|
autoload :Boundaries, "ohm/contrib/boundaries"
|
@@ -9,4 +9,4 @@ module Ohm
|
|
9
9
|
autoload :WebValidations, "ohm/contrib/web_validations"
|
10
10
|
autoload :NumberValidations, "ohm/contrib/number_validations"
|
11
11
|
autoload :Typecast, "ohm/contrib/typecast"
|
12
|
-
end
|
12
|
+
end
|
data/ohm-contrib.gemspec
CHANGED
data/test/helper.rb
CHANGED
data/test/test_ohm_typecast.rb
CHANGED
@@ -4,7 +4,7 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
4
4
|
context "the default case of just an attribute" do
|
5
5
|
class Post < Ohm::Model
|
6
6
|
include Ohm::Typecast
|
7
|
-
|
7
|
+
|
8
8
|
attribute :content
|
9
9
|
end
|
10
10
|
|
@@ -21,12 +21,26 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
21
21
|
|
22
22
|
assert_equal "foobar", post.content.downcase
|
23
23
|
end
|
24
|
+
|
25
|
+
test "mutating methods like upcase!" do
|
26
|
+
post = Post.create(:content => "FooBar")
|
27
|
+
post = Post[post.id]
|
28
|
+
|
29
|
+
post.content.upcase!
|
30
|
+
|
31
|
+
assert_equal "FOOBAR", post.content.to_s
|
32
|
+
end
|
33
|
+
|
34
|
+
test "inspecting" do
|
35
|
+
post = Post.new(:content => "FooBar")
|
36
|
+
assert_equal 'FooBar', post.content
|
37
|
+
end
|
24
38
|
end
|
25
39
|
|
26
40
|
context "when using a decimal" do
|
27
41
|
class Post < Ohm::Model
|
28
42
|
include Ohm::Typecast
|
29
|
-
|
43
|
+
|
30
44
|
attribute :price, Decimal
|
31
45
|
end
|
32
46
|
|
@@ -40,7 +54,7 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
40
54
|
test "handles empty string case correctly" do
|
41
55
|
post = Post.create(:price => "")
|
42
56
|
post = Post[post.id]
|
43
|
-
|
57
|
+
|
44
58
|
assert_equal "", post.price.to_s
|
45
59
|
end
|
46
60
|
|
@@ -57,17 +71,42 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
57
71
|
test "is accurate accdg to the decimal spec" do
|
58
72
|
post = Post.create(:price => "0.0001")
|
59
73
|
post = Post[post.id]
|
60
|
-
|
74
|
+
|
61
75
|
sum = 0
|
62
76
|
1_000.times { sum += post.price }
|
63
77
|
assert_equal 0.1, sum
|
64
78
|
end
|
79
|
+
|
80
|
+
test "using += with price" do
|
81
|
+
post = Post.create(:price => "0.0001")
|
82
|
+
post = Post[post.id]
|
83
|
+
|
84
|
+
post.price += 1
|
85
|
+
assert_equal 1.0001, post.price.to_f
|
86
|
+
end
|
87
|
+
|
88
|
+
test "assigning a raw BigDecimal" do
|
89
|
+
post = Post.create(:price => BigDecimal("399.50"))
|
90
|
+
post = Post[post.id]
|
91
|
+
|
92
|
+
assert_kind_of String, post.price.to_s
|
93
|
+
end
|
94
|
+
|
95
|
+
test "equality matching" do
|
96
|
+
post = Post.create(:price => "399.50")
|
97
|
+
assert (post.price == "399.50")
|
98
|
+
end
|
99
|
+
|
100
|
+
test "inspecting a Decimal" do
|
101
|
+
post = Post.new(:price => 399.50)
|
102
|
+
assert_equal '399.5', post.price.inspect
|
103
|
+
end
|
65
104
|
end
|
66
105
|
|
67
106
|
context "when using an integer" do
|
68
107
|
class Post < Ohm::Model
|
69
108
|
include Ohm::Typecast
|
70
|
-
|
109
|
+
|
71
110
|
attribute :price, Integer
|
72
111
|
end
|
73
112
|
|
@@ -81,7 +120,7 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
81
120
|
test "handles empty string case correctly" do
|
82
121
|
post = Post.create(:price => "")
|
83
122
|
post = Post[post.id]
|
84
|
-
|
123
|
+
|
85
124
|
assert_equal "", post.price.to_s
|
86
125
|
end
|
87
126
|
|
@@ -98,17 +137,22 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
98
137
|
test "raises when trying to do arithmetic ops on a non-int" do
|
99
138
|
post = Post.create(:price => "FooBar")
|
100
139
|
post = Post[post.id]
|
101
|
-
|
140
|
+
|
102
141
|
assert_raise ArgumentError do
|
103
142
|
post.price * post.price
|
104
143
|
end
|
105
144
|
end
|
145
|
+
|
146
|
+
test "inspecting" do
|
147
|
+
post = Post.new(:price => "50000")
|
148
|
+
assert_equal 50000, post.price.inspect
|
149
|
+
end
|
106
150
|
end
|
107
151
|
|
108
152
|
context "when using an float" do
|
109
153
|
class Post < Ohm::Model
|
110
154
|
include Ohm::Typecast
|
111
|
-
|
155
|
+
|
112
156
|
attribute :price, Float
|
113
157
|
end
|
114
158
|
|
@@ -122,7 +166,7 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
122
166
|
test "handles empty string case correctly" do
|
123
167
|
post = Post.create(:price => "")
|
124
168
|
post = Post[post.id]
|
125
|
-
|
169
|
+
|
126
170
|
assert_equal "", post.price.to_s
|
127
171
|
end
|
128
172
|
|
@@ -139,17 +183,22 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
139
183
|
test "raises when trying to do arithmetic ops on a non-float" do
|
140
184
|
post = Post.create(:price => "FooBar")
|
141
185
|
post = Post[post.id]
|
142
|
-
|
186
|
+
|
143
187
|
assert_raise ArgumentError do
|
144
188
|
post.price * post.price
|
145
189
|
end
|
146
190
|
end
|
191
|
+
|
192
|
+
test "inspecting" do
|
193
|
+
post = Post.new(:price => "12345.67890")
|
194
|
+
assert_equal 12345.67890, post.price.inspect
|
195
|
+
end
|
147
196
|
end
|
148
197
|
|
149
198
|
context "when using a time" do
|
150
199
|
class Post < Ohm::Model
|
151
200
|
include Ohm::Typecast
|
152
|
-
|
201
|
+
|
153
202
|
attribute :created_at, Time
|
154
203
|
end
|
155
204
|
|
@@ -163,14 +212,14 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
163
212
|
test "handles empty string case correctly" do
|
164
213
|
post = Post.create(:created_at => "")
|
165
214
|
post = Post[post.id]
|
166
|
-
|
215
|
+
|
167
216
|
assert_equal "", post.created_at.to_s
|
168
217
|
end
|
169
218
|
|
170
219
|
test "allows for real time operations" do
|
171
220
|
post = Post.create(:created_at => "2010-05-10")
|
172
221
|
post = Post[post.id]
|
173
|
-
|
222
|
+
|
174
223
|
assert_respond_to post.created_at, :strftime
|
175
224
|
assert_equal "2010-05-10", post.created_at.strftime('%Y-%m-%d')
|
176
225
|
end
|
@@ -178,7 +227,7 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
178
227
|
test "raises when trying to do non-time operations" do
|
179
228
|
post = Post.create(:created_at => "FooBar")
|
180
229
|
post = Post[post.id]
|
181
|
-
|
230
|
+
|
182
231
|
assert ! post.created_at.respond_to?(:slice)
|
183
232
|
|
184
233
|
assert_raise NoMethodError do
|
@@ -190,7 +239,7 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
190
239
|
context "when using a date" do
|
191
240
|
class Post < Ohm::Model
|
192
241
|
include Ohm::Typecast
|
193
|
-
|
242
|
+
|
194
243
|
attribute :created_on, Date
|
195
244
|
|
196
245
|
def today
|
@@ -208,14 +257,14 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
208
257
|
test "handles empty string case correctly" do
|
209
258
|
post = Post.create(:created_on => "")
|
210
259
|
post = Post[post.id]
|
211
|
-
|
260
|
+
|
212
261
|
assert_equal "", post.created_on.to_s
|
213
262
|
end
|
214
263
|
|
215
264
|
test "allows for real time operations" do
|
216
265
|
post = Post.create(:created_on => "2010-05-10")
|
217
266
|
post = Post[post.id]
|
218
|
-
|
267
|
+
|
219
268
|
assert_respond_to post.created_on, :strftime
|
220
269
|
assert_equal "2010-05-10", post.created_on.strftime('%Y-%m-%d')
|
221
270
|
end
|
@@ -223,7 +272,7 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
223
272
|
test "raises when trying to do date operations on a non-date" do
|
224
273
|
post = Post.create(:created_on => "FooBar")
|
225
274
|
post = Post[post.id]
|
226
|
-
|
275
|
+
|
227
276
|
assert_raise ArgumentError do
|
228
277
|
post.created_on.strftime("%Y")
|
229
278
|
end
|
@@ -233,6 +282,4 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
233
282
|
assert_equal Date.today, Post.new.today
|
234
283
|
end
|
235
284
|
end
|
236
|
-
|
237
|
-
|
238
|
-
end
|
285
|
+
end
|