ohm-contrib 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +2 -1
- data/VERSION +1 -1
- data/lib/ohm/contrib.rb +1 -1
- data/lib/ohm/contrib/typecast.rb +45 -117
- data/ohm-contrib.gemspec +4 -1
- data/test/helper.rb +2 -1
- data/test/test_ohm_typecast.rb +144 -289
- metadata +14 -2
data/Rakefile
CHANGED
@@ -14,6 +14,7 @@ begin
|
|
14
14
|
gem.add_development_dependency "redis", ">= 0"
|
15
15
|
gem.add_development_dependency "ohm", ">= 0"
|
16
16
|
gem.add_development_dependency "timecop", ">= 0"
|
17
|
+
gem.add_development_dependency "mocha", ">= 0"
|
17
18
|
end
|
18
19
|
Jeweler::GemcutterTasks.new
|
19
20
|
rescue LoadError
|
@@ -52,4 +53,4 @@ Rake::RDocTask.new do |rdoc|
|
|
52
53
|
rdoc.title = "ohm-contrib #{version}"
|
53
54
|
rdoc.rdoc_files.include('README*')
|
54
55
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
55
|
-
end
|
56
|
+
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.9
|
data/lib/ohm/contrib.rb
CHANGED
data/lib/ohm/contrib/typecast.rb
CHANGED
@@ -1,163 +1,91 @@
|
|
1
1
|
require 'bigdecimal'
|
2
2
|
require 'time'
|
3
3
|
require 'date'
|
4
|
+
require 'forwardable'
|
4
5
|
|
5
6
|
module Ohm
|
6
7
|
module Types
|
7
|
-
class
|
8
|
-
def
|
9
|
-
|
8
|
+
class Primitive < BasicObject
|
9
|
+
def initialize(value)
|
10
|
+
@raw = value
|
10
11
|
end
|
11
|
-
end
|
12
|
-
|
13
|
-
class Time < ::Time
|
14
|
-
ASSERTION = :assert_type_time
|
15
|
-
|
16
|
-
def self.[](value)
|
17
|
-
return value if value.to_s.empty?
|
18
12
|
|
19
|
-
|
20
|
-
|
13
|
+
def to_s
|
14
|
+
@raw
|
21
15
|
end
|
22
|
-
end
|
23
|
-
|
24
|
-
class Date < ::Date
|
25
|
-
ASSERTION = :assert_type_date
|
26
|
-
|
27
|
-
def self.[](value)
|
28
|
-
return value if value.to_s.empty?
|
29
16
|
|
30
|
-
|
31
|
-
|
32
|
-
value
|
17
|
+
def inspect
|
18
|
+
"#<Primitive raw=#{@raw}>"
|
33
19
|
end
|
34
|
-
end
|
35
|
-
|
36
|
-
class Decimal < BigDecimal
|
37
|
-
ASSERTION = :assert_type_decimal
|
38
|
-
CANONICAL = /^(\d+)?(\.\d+)?(E[+\-]\d+)?$/
|
39
|
-
|
40
|
-
def self.[](value)
|
41
|
-
return value if value.to_s.empty?
|
42
20
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
value
|
47
|
-
end
|
21
|
+
protected
|
22
|
+
def object
|
23
|
+
@raw
|
48
24
|
end
|
49
|
-
end
|
50
25
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
def self.[](value)
|
55
|
-
return value if value.to_s.empty?
|
56
|
-
|
57
|
-
Float(value)
|
58
|
-
rescue ArgumentError
|
59
|
-
value
|
26
|
+
def method_missing(meth, *args, &blk)
|
27
|
+
object.send(meth, *args, &blk)
|
60
28
|
end
|
61
29
|
end
|
62
30
|
|
63
|
-
class
|
64
|
-
ASSERTION = :assert_type_integer
|
65
|
-
|
66
|
-
def self.[](value)
|
67
|
-
return value if value.to_s.empty?
|
68
|
-
|
69
|
-
Integer(value)
|
70
|
-
rescue ArgumentError
|
71
|
-
value
|
72
|
-
end
|
31
|
+
class String < Primitive
|
73
32
|
end
|
74
|
-
end
|
75
|
-
|
76
|
-
module TypeAssertions
|
77
33
|
|
78
|
-
|
79
|
-
|
80
|
-
|
34
|
+
class Decimal < Primitive
|
35
|
+
protected
|
36
|
+
def object
|
37
|
+
::Kernel::BigDecimal(@raw)
|
38
|
+
end
|
81
39
|
end
|
82
40
|
|
83
|
-
|
84
|
-
|
41
|
+
class Integer < Primitive
|
42
|
+
protected
|
43
|
+
def object
|
44
|
+
::Kernel::Integer(@raw)
|
45
|
+
end
|
85
46
|
end
|
86
47
|
|
87
|
-
|
88
|
-
|
48
|
+
class Float < Primitive
|
49
|
+
protected
|
50
|
+
def object
|
51
|
+
::Kernel::Float(@raw)
|
52
|
+
end
|
89
53
|
end
|
90
54
|
|
91
|
-
|
92
|
-
|
55
|
+
class Time < Primitive
|
56
|
+
protected
|
57
|
+
def object
|
58
|
+
::Time.parse(@raw)
|
59
|
+
end
|
93
60
|
end
|
94
61
|
|
95
|
-
|
96
|
-
|
62
|
+
class Date < Primitive
|
63
|
+
protected
|
64
|
+
def object
|
65
|
+
::Date.parse(@raw)
|
66
|
+
end
|
97
67
|
end
|
98
68
|
end
|
99
|
-
|
69
|
+
|
100
70
|
module Typecast
|
101
|
-
class MissingValidation < StandardError
|
102
|
-
MESSAGE = "%s :%s is required in your #validate method"
|
103
|
-
|
104
|
-
attr :field
|
105
|
-
attr :assertion
|
106
|
-
|
107
|
-
def initialize(field, assertion)
|
108
|
-
@field, @assertion = field, assertion
|
109
|
-
end
|
110
|
-
|
111
|
-
def message
|
112
|
-
MESSAGE % [assertion, field]
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
71
|
include Types
|
117
|
-
|
118
|
-
|
72
|
+
|
119
73
|
def self.included(base)
|
120
74
|
base.extend ClassMethods
|
121
75
|
end
|
122
76
|
|
123
77
|
module ClassMethods
|
124
|
-
def attribute(name, type = Ohm::Types::String
|
78
|
+
def attribute(name, type = Ohm::Types::String)
|
125
79
|
define_method(name) do
|
126
|
-
|
80
|
+
value = read_local(name)
|
81
|
+
value && type.new(value)
|
127
82
|
end
|
128
83
|
|
129
84
|
define_method(:"#{name}=") do |value|
|
130
|
-
|
131
|
-
write_local(name, type.dump(value))
|
132
|
-
else
|
133
|
-
write_local(name, value && value.to_s)
|
134
|
-
end
|
85
|
+
write_local(name, value && value.to_s)
|
135
86
|
end
|
136
87
|
|
137
88
|
attributes << name unless attributes.include?(name)
|
138
|
-
types[name] = [type, assertion] unless types.has_key?(name)
|
139
|
-
end
|
140
|
-
|
141
|
-
def types
|
142
|
-
@types ||= {}
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
def valid?
|
147
|
-
return unless super
|
148
|
-
|
149
|
-
self.class.types.each do |field, (type, assertion)|
|
150
|
-
value = send(field)
|
151
|
-
|
152
|
-
unless value.kind_of?(type)
|
153
|
-
raise MissingValidation.new(field, assertion)
|
154
|
-
end
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
def validate
|
159
|
-
self.class.types.each do |field, (type, assertion)|
|
160
|
-
send assertion, field if assertion
|
161
89
|
end
|
162
90
|
end
|
163
91
|
end
|
data/ohm-contrib.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
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.9"
|
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"]
|
@@ -65,17 +65,20 @@ Gem::Specification.new do |s|
|
|
65
65
|
s.add_development_dependency(%q<redis>, [">= 0"])
|
66
66
|
s.add_development_dependency(%q<ohm>, [">= 0"])
|
67
67
|
s.add_development_dependency(%q<timecop>, [">= 0"])
|
68
|
+
s.add_development_dependency(%q<mocha>, [">= 0"])
|
68
69
|
else
|
69
70
|
s.add_dependency(%q<contest>, [">= 0"])
|
70
71
|
s.add_dependency(%q<redis>, [">= 0"])
|
71
72
|
s.add_dependency(%q<ohm>, [">= 0"])
|
72
73
|
s.add_dependency(%q<timecop>, [">= 0"])
|
74
|
+
s.add_dependency(%q<mocha>, [">= 0"])
|
73
75
|
end
|
74
76
|
else
|
75
77
|
s.add_dependency(%q<contest>, [">= 0"])
|
76
78
|
s.add_dependency(%q<redis>, [">= 0"])
|
77
79
|
s.add_dependency(%q<ohm>, [">= 0"])
|
78
80
|
s.add_dependency(%q<timecop>, [">= 0"])
|
81
|
+
s.add_dependency(%q<mocha>, [">= 0"])
|
79
82
|
end
|
80
83
|
end
|
81
84
|
|
data/test/helper.rb
CHANGED
@@ -4,6 +4,7 @@ require 'contest'
|
|
4
4
|
require 'redis'
|
5
5
|
require 'ohm'
|
6
6
|
require 'timecop'
|
7
|
+
require 'mocha'
|
7
8
|
|
8
9
|
Ohm.connect :host => "localhost", :port => "6380"
|
9
10
|
|
@@ -12,4 +13,4 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
|
|
12
13
|
require 'ohm/contrib'
|
13
14
|
|
14
15
|
class Test::Unit::TestCase
|
15
|
-
end
|
16
|
+
end
|
data/test/test_ohm_typecast.rb
CHANGED
@@ -1,383 +1,238 @@
|
|
1
1
|
require "helper"
|
2
2
|
|
3
3
|
class TestOhmTypecast < Test::Unit::TestCase
|
4
|
-
context "
|
5
|
-
class
|
4
|
+
context "the default case of just an attribute" do
|
5
|
+
class Post < Ohm::Model
|
6
6
|
include Ohm::Typecast
|
7
|
-
|
8
|
-
attribute :
|
9
|
-
|
10
|
-
# override validate and purposely forget super
|
11
|
-
def validate
|
12
|
-
end
|
7
|
+
|
8
|
+
attribute :content
|
13
9
|
end
|
14
10
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
sum = 0
|
19
|
-
1_000.times { sum += product.price }
|
11
|
+
test "handles nil case correctly" do
|
12
|
+
post = Post.create(:content => nil)
|
13
|
+
post = Post[post.id]
|
20
14
|
|
21
|
-
|
15
|
+
assert_nil post.content
|
22
16
|
end
|
23
17
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
assert_equal "FooBar", product.price
|
28
|
-
end
|
29
|
-
|
30
|
-
should "raise a MissingValidation error during validation" do
|
31
|
-
product = Product.new(:price => "FooBar")
|
18
|
+
test "still responds to string methods properly" do
|
19
|
+
post = Post.create(:content => "FooBar")
|
20
|
+
post = Post[post.id]
|
32
21
|
|
33
|
-
|
34
|
-
product.valid?
|
35
|
-
rescue Ohm::Typecast::MissingValidation => ex
|
36
|
-
assert_match(/assert_type_decimal :price is required in your #validate method/, ex.message)
|
37
|
-
else
|
38
|
-
flunk "Should have raised MissingValidation"
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
context "given a non-decimal value FooBar but with assert_type_decimal" do
|
44
|
-
class Post < Ohm::Model
|
45
|
-
include Ohm::Typecast
|
46
|
-
|
47
|
-
attribute :price, Decimal
|
48
|
-
end
|
49
|
-
|
50
|
-
should "be invalid and have an error on the field" do
|
51
|
-
post = Post.new(:price => "FooBar")
|
52
|
-
|
53
|
-
assert ! post.valid?
|
54
|
-
assert_equal [[:price, :not_decimal]], post.errors
|
55
|
-
end
|
22
|
+
assert_equal "foobar", post.content.downcase
|
56
23
|
end
|
57
24
|
end
|
58
25
|
|
59
|
-
context "
|
60
|
-
class
|
26
|
+
context "when using a decimal" do
|
27
|
+
class Post < Ohm::Model
|
61
28
|
include Ohm::Typecast
|
62
|
-
|
63
|
-
attribute :
|
64
|
-
|
65
|
-
# we override validate and purposely forget to call super
|
66
|
-
def validate
|
67
|
-
end
|
29
|
+
|
30
|
+
attribute :price, Decimal
|
68
31
|
end
|
69
32
|
|
70
|
-
test "
|
71
|
-
|
72
|
-
|
73
|
-
product = Product[product.id]
|
33
|
+
test "handles nil case correctly" do
|
34
|
+
post = Post.create(:price => nil)
|
35
|
+
post = Post[post.id]
|
74
36
|
|
75
|
-
|
37
|
+
assert_nil post.price
|
76
38
|
end
|
77
39
|
|
78
|
-
test "
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
assert_kind_of Time, product.start_of_sale
|
40
|
+
test "handles empty string case correctly" do
|
41
|
+
post = Post.create(:price => "")
|
42
|
+
post = Post[post.id]
|
43
|
+
|
44
|
+
assert_equal "", post.price.to_s
|
84
45
|
end
|
85
46
|
|
86
|
-
test "
|
87
|
-
|
88
|
-
|
89
|
-
rescue Ohm::Typecast::MissingValidation => ex
|
90
|
-
assert_match(/assert_type_time :start_of_sale is required in your #validate method/, ex.message)
|
91
|
-
else
|
92
|
-
flunk "Should have raised MissingValidation"
|
93
|
-
end
|
94
|
-
end
|
47
|
+
test "allows for real arithmetic" do
|
48
|
+
post = Post.create(:price => "0.01")
|
49
|
+
post = Post[post.id]
|
95
50
|
|
96
|
-
|
97
|
-
|
98
|
-
|
51
|
+
assert_equal 0.02, post.price + post.price
|
52
|
+
assert_equal 0.0, post.price - post.price
|
53
|
+
assert_equal 0.0001, post.price * post.price
|
54
|
+
assert_equal 1.0, post.price / post.price
|
99
55
|
end
|
100
56
|
|
101
|
-
test "
|
102
|
-
|
103
|
-
|
57
|
+
test "is accurate accdg to the decimal spec" do
|
58
|
+
post = Post.create(:price => "0.0001")
|
59
|
+
post = Post[post.id]
|
60
|
+
|
61
|
+
sum = 0
|
62
|
+
1_000.times { sum += post.price }
|
63
|
+
assert_equal 0.1, sum
|
104
64
|
end
|
105
65
|
end
|
106
66
|
|
107
|
-
context "
|
67
|
+
context "when using an integer" do
|
108
68
|
class Post < Ohm::Model
|
109
69
|
include Ohm::Typecast
|
110
|
-
|
111
|
-
attribute :
|
112
|
-
end
|
113
|
-
|
114
|
-
should "be valid given invalid time value" do
|
115
|
-
post = Post.new(:start_of_sale => "FooBar")
|
116
|
-
assert ! post.valid?
|
117
|
-
assert_equal [[:start_of_sale, :not_time]], post.errors
|
70
|
+
|
71
|
+
attribute :price, Integer
|
118
72
|
end
|
119
|
-
end
|
120
73
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
attribute :date_bought, Date
|
74
|
+
test "handles nil case correctly" do
|
75
|
+
post = Post.create(:price => nil)
|
76
|
+
post = Post[post.id]
|
126
77
|
|
127
|
-
|
128
|
-
def validate
|
129
|
-
end
|
78
|
+
assert_nil post.price
|
130
79
|
end
|
131
80
|
|
132
|
-
test "
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
assert_equal @date, product.date_bought
|
81
|
+
test "handles empty string case correctly" do
|
82
|
+
post = Post.create(:price => "")
|
83
|
+
post = Post[post.id]
|
84
|
+
|
85
|
+
assert_equal "", post.price.to_s
|
138
86
|
end
|
139
87
|
|
140
|
-
test "
|
141
|
-
|
142
|
-
|
143
|
-
product = Product[product.id]
|
88
|
+
test "allows for real arithmetic" do
|
89
|
+
post = Post.create(:price => "3")
|
90
|
+
post = Post[post.id]
|
144
91
|
|
145
|
-
|
92
|
+
assert_equal 6, post.price + post.price
|
93
|
+
assert_equal 0, post.price - post.price
|
94
|
+
assert_equal 9, post.price * post.price
|
95
|
+
assert_equal 1, post.price / post.price
|
146
96
|
end
|
147
97
|
|
148
|
-
test "
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
flunk "Should have raised MissingValidation"
|
98
|
+
test "raises when trying to do arithmetic ops on a non-int" do
|
99
|
+
post = Post.create(:price => "FooBar")
|
100
|
+
post = Post[post.id]
|
101
|
+
|
102
|
+
assert_raise ArgumentError do
|
103
|
+
post.price * post.price
|
155
104
|
end
|
156
105
|
end
|
157
|
-
|
158
|
-
test "when nil" do
|
159
|
-
assert_nil Product.new.date_bought
|
160
|
-
end
|
161
|
-
|
162
|
-
test "empty string" do
|
163
|
-
assert_equal "", Product.new(:date_bought => "").date_bought
|
164
|
-
end
|
165
106
|
end
|
166
107
|
|
167
|
-
context "
|
108
|
+
context "when using an float" do
|
168
109
|
class Post < Ohm::Model
|
169
110
|
include Ohm::Typecast
|
170
|
-
|
171
|
-
attribute :
|
172
|
-
|
173
|
-
def validate
|
174
|
-
assert_type_date :created
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
should "have an invalid created field on save" do
|
179
|
-
post = Post.new(:created => "FooBar")
|
180
|
-
assert ! post.valid?
|
181
|
-
assert_equal [[:created, :not_date]], post.errors
|
182
|
-
assert_equal "FooBar", post.created
|
183
|
-
end
|
184
|
-
end
|
185
|
-
|
186
|
-
context "integer typecasting" do
|
187
|
-
class Product < Ohm::Model
|
188
|
-
include Ohm::Typecast
|
189
|
-
|
190
|
-
attribute :stock_count, Integer
|
111
|
+
|
112
|
+
attribute :price, Float
|
191
113
|
end
|
192
114
|
|
193
|
-
test "
|
194
|
-
|
195
|
-
|
115
|
+
test "handles nil case correctly" do
|
116
|
+
post = Post.create(:price => nil)
|
117
|
+
post = Post[post.id]
|
196
118
|
|
197
|
-
|
198
|
-
assert_equal "", Product.new(:stock_count => "").stock_count
|
119
|
+
assert_nil post.price
|
199
120
|
end
|
200
121
|
|
201
|
-
test "
|
202
|
-
|
122
|
+
test "handles empty string case correctly" do
|
123
|
+
post = Post.create(:price => "")
|
124
|
+
post = Post[post.id]
|
125
|
+
|
126
|
+
assert_equal "", post.price.to_s
|
203
127
|
end
|
204
128
|
|
205
|
-
test "
|
206
|
-
|
207
|
-
|
129
|
+
test "allows for real arithmetic" do
|
130
|
+
post = Post.create(:price => "3")
|
131
|
+
post = Post[post.id]
|
208
132
|
|
209
|
-
|
210
|
-
assert_equal
|
133
|
+
assert_equal 6.0, post.price + post.price
|
134
|
+
assert_equal 0.0, post.price - post.price
|
135
|
+
assert_equal 9.0, post.price * post.price
|
136
|
+
assert_equal 1.0, post.price / post.price
|
211
137
|
end
|
212
|
-
end
|
213
|
-
|
214
|
-
context "integer typecasting with assert_type_integer validation" do
|
215
|
-
class Post < Ohm::Model
|
216
|
-
include Ohm::Typecast
|
217
138
|
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
139
|
+
test "raises when trying to do arithmetic ops on a non-float" do
|
140
|
+
post = Post.create(:price => "FooBar")
|
141
|
+
post = Post[post.id]
|
142
|
+
|
143
|
+
assert_raise ArgumentError do
|
144
|
+
post.price * post.price
|
222
145
|
end
|
223
146
|
end
|
224
|
-
|
225
|
-
should "have an error on counter given a non-integer value" do
|
226
|
-
post = Post.new(:counter => "FooBar")
|
227
|
-
assert ! post.valid?
|
228
|
-
assert_equal [[:counter, :not_integer]], post.errors
|
229
|
-
assert_equal "FooBar", post.counter
|
230
|
-
end
|
231
147
|
end
|
232
148
|
|
233
|
-
context "
|
234
|
-
class
|
149
|
+
context "when using a time" do
|
150
|
+
class Post < Ohm::Model
|
235
151
|
include Ohm::Typecast
|
236
|
-
|
237
|
-
attribute :
|
238
|
-
end
|
239
|
-
|
240
|
-
test "when nil" do
|
241
|
-
assert_nil Product.new(:vat => nil).vat
|
152
|
+
|
153
|
+
attribute :created_at, Time
|
242
154
|
end
|
243
155
|
|
244
|
-
test "
|
245
|
-
|
246
|
-
|
156
|
+
test "handles nil case correctly" do
|
157
|
+
post = Post.create(:created_at => nil)
|
158
|
+
post = Post[post.id]
|
247
159
|
|
248
|
-
|
249
|
-
assert_equal "FooBar", Product.new(:vat => "FooBar").vat
|
160
|
+
assert_nil post.created_at
|
250
161
|
end
|
251
162
|
|
252
|
-
test "
|
253
|
-
|
163
|
+
test "handles empty string case correctly" do
|
164
|
+
post = Post.create(:created_at => "")
|
165
|
+
post = Post[post.id]
|
166
|
+
|
167
|
+
assert_equal "", post.created_at.to_s
|
254
168
|
end
|
255
169
|
|
256
|
-
test "
|
257
|
-
|
170
|
+
test "allows for real time operations" do
|
171
|
+
post = Post.create(:created_at => "2010-05-10")
|
172
|
+
post = Post[post.id]
|
173
|
+
|
174
|
+
assert_respond_to post.created_at, :strftime
|
175
|
+
assert_equal "2010-05-10", post.created_at.strftime('%Y-%m-%d')
|
258
176
|
end
|
259
177
|
|
260
|
-
test "when
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
context "float typecasting with assert_type_float validation" do
|
266
|
-
class Post < Ohm::Model
|
267
|
-
include Ohm::Typecast
|
268
|
-
|
269
|
-
attribute :quotient, Float
|
178
|
+
test "raises when trying to do non-time operations" do
|
179
|
+
post = Post.create(:created_at => "FooBar")
|
180
|
+
post = Post[post.id]
|
181
|
+
|
182
|
+
assert ! post.created_at.respond_to?(:slice)
|
270
183
|
|
271
|
-
|
272
|
-
|
184
|
+
assert_raise NoMethodError do
|
185
|
+
post.created_at.slice
|
273
186
|
end
|
274
187
|
end
|
275
|
-
|
276
|
-
should "have an error on quotient given a non-float value" do
|
277
|
-
post = Post.new(:quotient => "FooBar")
|
278
|
-
assert ! post.valid?
|
279
|
-
assert_equal [[:quotient, :not_float]], post.errors
|
280
|
-
assert_equal "FooBar", post.quotient
|
281
|
-
end
|
282
188
|
end
|
283
189
|
|
284
|
-
|
285
|
-
|
286
|
-
class Item < Ohm::Model
|
190
|
+
context "when using a date" do
|
191
|
+
class Post < Ohm::Model
|
287
192
|
include Ohm::Typecast
|
193
|
+
|
194
|
+
attribute :created_on, Date
|
288
195
|
|
289
|
-
|
290
|
-
|
291
|
-
def validate
|
292
|
-
assert_numeric :price
|
293
|
-
end
|
294
|
-
end
|
295
|
-
|
296
|
-
setup do
|
297
|
-
@item = Item.new(:price => "FooBar")
|
298
|
-
end
|
299
|
-
|
300
|
-
test "invalidation of price" do
|
301
|
-
assert ! @item.valid?
|
302
|
-
assert_equal [[:price, :not_numeric]], @item.errors
|
303
|
-
end
|
304
|
-
end
|
305
|
-
|
306
|
-
context "specifying a custom DataType without a #dump method" do
|
307
|
-
class ::FooBar < Struct.new(:value)
|
308
|
-
def self.[](str)
|
309
|
-
new(str)
|
196
|
+
def today
|
197
|
+
::Date.today
|
310
198
|
end
|
311
199
|
end
|
312
|
-
|
313
|
-
class Item < Ohm::Model
|
314
|
-
include Ohm::Typecast
|
315
200
|
|
316
|
-
|
317
|
-
|
201
|
+
test "handles nil case correctly" do
|
202
|
+
post = Post.create(:created_on => nil)
|
203
|
+
post = Post[post.id]
|
318
204
|
|
319
|
-
|
320
|
-
item = Item.new(:foo => "bar")
|
321
|
-
assert_equal FooBar.new("bar"), item.foo
|
322
|
-
end
|
323
|
-
end
|
324
|
-
|
325
|
-
context "specifying a customer DataType but with a #dump method" do
|
326
|
-
class ::CSV < Array
|
327
|
-
def self.dump(arr)
|
328
|
-
arr.join(',')
|
329
|
-
end
|
330
|
-
|
331
|
-
def self.[](str)
|
332
|
-
new(str.split(','))
|
333
|
-
end
|
334
|
-
|
335
|
-
def to_s
|
336
|
-
join(',')
|
337
|
-
end
|
205
|
+
assert_nil post.created_on
|
338
206
|
end
|
339
207
|
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
208
|
+
test "handles empty string case correctly" do
|
209
|
+
post = Post.create(:created_on => "")
|
210
|
+
post = Post[post.id]
|
211
|
+
|
212
|
+
assert_equal "", post.created_on.to_s
|
344
213
|
end
|
345
214
|
|
346
|
-
|
347
|
-
|
348
|
-
|
215
|
+
test "allows for real time operations" do
|
216
|
+
post = Post.create(:created_on => "2010-05-10")
|
217
|
+
post = Post[post.id]
|
349
218
|
|
350
|
-
|
219
|
+
assert_respond_to post.created_on, :strftime
|
220
|
+
assert_equal "2010-05-10", post.created_on.strftime('%Y-%m-%d')
|
351
221
|
end
|
352
222
|
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
223
|
+
test "raises when trying to do date operations on a non-date" do
|
224
|
+
post = Post.create(:created_on => "FooBar")
|
225
|
+
post = Post[post.id]
|
226
|
+
|
227
|
+
assert_raise ArgumentError do
|
228
|
+
post.created_on.strftime("%Y")
|
229
|
+
end
|
358
230
|
end
|
359
231
|
|
360
|
-
|
361
|
-
|
362
|
-
item = Item[item.id]
|
363
|
-
|
364
|
-
assert_equal ['first', 'second', 'third'], item.csv
|
232
|
+
test "still able to access Date" do
|
233
|
+
assert_equal Date.today, Post.new.today
|
365
234
|
end
|
366
235
|
end
|
367
236
|
|
368
|
-
context "defaut Strings" do
|
369
|
-
class Post < Ohm::Model
|
370
|
-
include Ohm::Typecast
|
371
|
-
|
372
|
-
attribute :field1
|
373
|
-
attribute :field2
|
374
|
-
attribute :field3
|
375
|
-
end
|
376
237
|
|
377
|
-
should "not throw a validation missing exception during save" do
|
378
|
-
assert_nothing_raised Ohm::Typecast::MissingValidation do
|
379
|
-
Post.create :field1 => "field1", :field2 => "field2", :field3 => "fld3"
|
380
|
-
end
|
381
|
-
end
|
382
|
-
end
|
383
238
|
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
|
+
- 9
|
9
|
+
version: 0.0.9
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Cyril David
|
@@ -65,6 +65,18 @@ dependencies:
|
|
65
65
|
version: "0"
|
66
66
|
type: :development
|
67
67
|
version_requirements: *id004
|
68
|
+
- !ruby/object:Gem::Dependency
|
69
|
+
name: mocha
|
70
|
+
prerelease: false
|
71
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
type: :development
|
79
|
+
version_requirements: *id005
|
68
80
|
description: Highly decoupled drop-in functionality for Ohm models
|
69
81
|
email: cyx.ucron@gmail.com
|
70
82
|
executables: []
|