ohm-contrib 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/ohm/contrib.rb +1 -1
- data/lib/ohm/contrib/typecast.rb +61 -27
- data/ohm-contrib.gemspec +1 -1
- data/test/test_ohm_typecast.rb +8 -8
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
data/lib/ohm/contrib.rb
CHANGED
data/lib/ohm/contrib/typecast.rb
CHANGED
@@ -3,15 +3,72 @@ require 'time'
|
|
3
3
|
require 'date'
|
4
4
|
|
5
5
|
module Ohm
|
6
|
+
module Types
|
7
|
+
class String < ::String
|
8
|
+
def self.[](value)
|
9
|
+
value
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Time < ::Time
|
14
|
+
def self.[](value)
|
15
|
+
return value if value.to_s.empty?
|
16
|
+
|
17
|
+
ret = parse(value)
|
18
|
+
ret.to_s == value ? ret : value
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Date < ::Date
|
23
|
+
def self.[](value)
|
24
|
+
return value if value.to_s.empty?
|
25
|
+
|
26
|
+
parse(value)
|
27
|
+
rescue ArgumentError
|
28
|
+
value
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class Decimal
|
33
|
+
def self.[](value)
|
34
|
+
return value if value.to_s.empty?
|
35
|
+
|
36
|
+
BigDecimal(value)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class Float < ::Float
|
41
|
+
def self.[](value)
|
42
|
+
return value if value.to_s.empty?
|
43
|
+
|
44
|
+
Float(value)
|
45
|
+
rescue ArgumentError
|
46
|
+
value
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class Integer < ::Integer
|
51
|
+
def self.[](value)
|
52
|
+
return value if value.to_s.empty?
|
53
|
+
|
54
|
+
Integer(value)
|
55
|
+
rescue ArgumentError
|
56
|
+
value
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
6
61
|
module Typecast
|
62
|
+
include Types
|
63
|
+
|
7
64
|
def self.included(base)
|
8
|
-
base.extend Macros
|
65
|
+
base.extend Macros
|
9
66
|
end
|
10
|
-
|
67
|
+
|
11
68
|
module Macros
|
12
|
-
def attribute(name, type =
|
69
|
+
def attribute(name, type = String)
|
13
70
|
define_method(name) do
|
14
|
-
|
71
|
+
type[read_local(name)]
|
15
72
|
end
|
16
73
|
|
17
74
|
define_method(:"#{name}=") do |value|
|
@@ -21,28 +78,5 @@ module Ohm
|
|
21
78
|
attributes << name unless attributes.include?(name)
|
22
79
|
end
|
23
80
|
end
|
24
|
-
|
25
|
-
protected
|
26
|
-
def typecast(val, type)
|
27
|
-
return val if val.to_s.empty?
|
28
|
-
|
29
|
-
case type
|
30
|
-
when :integer then Integer(val)
|
31
|
-
when :float then Float(val)
|
32
|
-
when :decimal then BigDecimal(val)
|
33
|
-
when :time
|
34
|
-
ret = Time.parse(val)
|
35
|
-
ret.to_s == val ? ret : val
|
36
|
-
|
37
|
-
when :date then Date.parse(val)
|
38
|
-
when :string then val
|
39
|
-
end
|
40
|
-
|
41
|
-
# All of the casting methods used above raises an ArgumentError
|
42
|
-
# if it fails to parse the value properly. If this happens,
|
43
|
-
# the least surprising behavior is to return the original value
|
44
|
-
rescue ArgumentError
|
45
|
-
val
|
46
|
-
end
|
47
81
|
end
|
48
82
|
end
|
data/ohm-contrib.gemspec
CHANGED
data/test/test_ohm_typecast.rb
CHANGED
@@ -5,7 +5,7 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
5
5
|
class Product < Ohm::Model
|
6
6
|
include Ohm::Typecast
|
7
7
|
|
8
|
-
attribute :price,
|
8
|
+
attribute :price, Decimal
|
9
9
|
end
|
10
10
|
|
11
11
|
should "properly preserve the right precision" do
|
@@ -22,7 +22,7 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
22
22
|
class Product < Ohm::Model
|
23
23
|
include Ohm::Typecast
|
24
24
|
|
25
|
-
attribute :start_of_sale,
|
25
|
+
attribute :start_of_sale, Time
|
26
26
|
end
|
27
27
|
|
28
28
|
test "read / write" do
|
@@ -37,8 +37,8 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
37
37
|
@time = Time.now.utc
|
38
38
|
product = Product.create(:start_of_sale => @time)
|
39
39
|
product = Product[product.id]
|
40
|
-
|
41
|
-
|
40
|
+
|
41
|
+
assert_kind_of Time, product.start_of_sale
|
42
42
|
end
|
43
43
|
|
44
44
|
test "an invalid string" do
|
@@ -63,7 +63,7 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
63
63
|
class Product < Ohm::Model
|
64
64
|
include Ohm::Typecast
|
65
65
|
|
66
|
-
attribute :date_bought,
|
66
|
+
attribute :date_bought, Date
|
67
67
|
end
|
68
68
|
|
69
69
|
test "read / write" do
|
@@ -79,7 +79,7 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
79
79
|
product = Product.create(:date_bought => @date)
|
80
80
|
product = Product[product.id]
|
81
81
|
|
82
|
-
|
82
|
+
assert_kind_of Date, product.date_bought
|
83
83
|
end
|
84
84
|
|
85
85
|
test "assigning a string which is not a valid date before persisting" do
|
@@ -101,7 +101,7 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
101
101
|
class Product < Ohm::Model
|
102
102
|
include Ohm::Typecast
|
103
103
|
|
104
|
-
attribute :stock_count,
|
104
|
+
attribute :stock_count, Integer
|
105
105
|
end
|
106
106
|
|
107
107
|
test "when nil" do
|
@@ -129,7 +129,7 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
129
129
|
class Product < Ohm::Model
|
130
130
|
include Ohm::Typecast
|
131
131
|
|
132
|
-
attribute :vat,
|
132
|
+
attribute :vat, Float
|
133
133
|
end
|
134
134
|
|
135
135
|
test "when nil" do
|