ohm-contrib 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +1 -1
- data/README.markdown +10 -10
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/ohm/contrib.rb +1 -1
- data/lib/ohm/contrib/boundaries.rb +2 -2
- data/lib/ohm/contrib/number_validations.rb +1 -1
- data/lib/ohm/contrib/timestamping.rb +2 -2
- data/lib/ohm/contrib/to_hash.rb +1 -1
- data/lib/ohm/contrib/typecast.rb +10 -4
- data/lib/ohm/contrib/web_validations.rb +1 -1
- data/ohm-contrib.gemspec +1 -1
- data/test/helper.rb +1 -1
- data/test/test_ohm_boundaries.rb +8 -8
- data/test/test_ohm_contrib.rb +2 -2
- data/test/test_ohm_number_validations.rb +3 -3
- data/test/test_ohm_timestamping.rb +7 -7
- data/test/test_ohm_to_hash.rb +4 -4
- data/test/test_ohm_typecast.rb +27 -6
- data/test/test_ohm_web_validations.rb +4 -4
- metadata +2 -2
data/LICENSE
CHANGED
@@ -17,4 +17,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
17
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
18
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
19
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
CHANGED
@@ -14,14 +14,14 @@ Example usage
|
|
14
14
|
|
15
15
|
require 'ohm'
|
16
16
|
require 'ohm/contrib'
|
17
|
-
|
17
|
+
|
18
18
|
class Post < Ohm::Model
|
19
19
|
include Ohm::Timestamping
|
20
20
|
include Ohm::ToHash
|
21
21
|
include Ohm::Boundaries
|
22
22
|
include Ohm::WebValidations
|
23
23
|
include Ohm::NumberValidations
|
24
|
-
|
24
|
+
|
25
25
|
attribute :amount
|
26
26
|
attribute :url
|
27
27
|
attribute :poster_email
|
@@ -51,12 +51,12 @@ Example usage
|
|
51
51
|
# Casting example
|
52
52
|
class Product
|
53
53
|
include Ohm::Typecast
|
54
|
-
|
55
|
-
attribute :price,
|
56
|
-
attribute :start_of_sale,
|
57
|
-
attribute :end_of_sale,
|
58
|
-
attribute :priority,
|
59
|
-
attribute :rating,
|
54
|
+
|
55
|
+
attribute :price, Decimal
|
56
|
+
attribute :start_of_sale, Time
|
57
|
+
attribute :end_of_sale, Time
|
58
|
+
attribute :priority, Integer
|
59
|
+
attribute :rating, Float
|
60
60
|
end
|
61
61
|
|
62
62
|
Credits
|
@@ -70,10 +70,10 @@ Note on Patches/Pull Requests
|
|
70
70
|
* Add tests for it. This is important so I don't break it in a
|
71
71
|
future version unintentionally.
|
72
72
|
* Commit, do not mess with rakefile, version, or history.
|
73
|
-
(if you want to have your own version, that is fine but bump version in a
|
73
|
+
(if you want to have your own version, that is fine but bump version in a
|
74
74
|
commit by itself I can ignore when I pull)
|
75
75
|
* Send me a pull request. Bonus points for topic branches.
|
76
76
|
|
77
77
|
Copyright
|
78
78
|
---------
|
79
|
-
Copyright (c) 2010 Cyril David. See LICENSE for details.
|
79
|
+
Copyright (c) 2010 Cyril David. See LICENSE for details.
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.5
|
data/lib/ohm/contrib.rb
CHANGED
data/lib/ohm/contrib/to_hash.rb
CHANGED
data/lib/ohm/contrib/typecast.rb
CHANGED
@@ -22,7 +22,7 @@ module Ohm
|
|
22
22
|
class Date < ::Date
|
23
23
|
def self.[](value)
|
24
24
|
return value if value.to_s.empty?
|
25
|
-
|
25
|
+
|
26
26
|
parse(value)
|
27
27
|
rescue ArgumentError
|
28
28
|
value
|
@@ -30,10 +30,16 @@ module Ohm
|
|
30
30
|
end
|
31
31
|
|
32
32
|
class Decimal
|
33
|
+
CANONICAL = /^(\d+)?(\.\d+)?(E[+\-]\d+)?$/
|
34
|
+
|
33
35
|
def self.[](value)
|
34
36
|
return value if value.to_s.empty?
|
35
37
|
|
36
|
-
|
38
|
+
if value.to_s =~ CANONICAL
|
39
|
+
BigDecimal(value)
|
40
|
+
else
|
41
|
+
value
|
42
|
+
end
|
37
43
|
end
|
38
44
|
end
|
39
45
|
|
@@ -64,7 +70,7 @@ module Ohm
|
|
64
70
|
def self.included(base)
|
65
71
|
base.extend Macros
|
66
72
|
end
|
67
|
-
|
73
|
+
|
68
74
|
module Macros
|
69
75
|
def attribute(name, type = String)
|
70
76
|
define_method(name) do
|
@@ -79,4 +85,4 @@ module Ohm
|
|
79
85
|
end
|
80
86
|
end
|
81
87
|
end
|
82
|
-
end
|
88
|
+
end
|
data/ohm-contrib.gemspec
CHANGED
data/test/helper.rb
CHANGED
data/test/test_ohm_boundaries.rb
CHANGED
@@ -4,20 +4,20 @@ class TestOhmBoundaries < Test::Unit::TestCase
|
|
4
4
|
setup do
|
5
5
|
Ohm.flush
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
8
|
class Person < Ohm::Model
|
9
9
|
include Ohm::Boundaries
|
10
10
|
|
11
11
|
attribute :name
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
context "when there are no People" do
|
15
15
|
should "have a nil Person.first" do
|
16
16
|
assert_nil Person.first
|
17
17
|
end
|
18
18
|
|
19
19
|
should "have a nil Person.last" do
|
20
|
-
assert_nil Person.last
|
20
|
+
assert_nil Person.last
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -31,7 +31,7 @@ class TestOhmBoundaries < Test::Unit::TestCase
|
|
31
31
|
end
|
32
32
|
|
33
33
|
should "have matz as the last person" do
|
34
|
-
assert_equal @matz, Person.last
|
34
|
+
assert_equal @matz, Person.last
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
@@ -40,13 +40,13 @@ class TestOhmBoundaries < Test::Unit::TestCase
|
|
40
40
|
@matz = Person.create(:name => "matz")
|
41
41
|
@linus = Person.create(:name => "linus")
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
44
|
should "have matz as the first Person" do
|
45
45
|
assert_equal @matz, Person.first
|
46
|
-
end
|
46
|
+
end
|
47
47
|
|
48
48
|
should "have linus as the last Person" do
|
49
49
|
assert_equal @linus, Person.last
|
50
|
-
end
|
50
|
+
end
|
51
51
|
end
|
52
|
-
end
|
52
|
+
end
|
data/test/test_ohm_contrib.rb
CHANGED
@@ -12,7 +12,7 @@ class TestOhmNumberValidations < Test::Unit::TestCase
|
|
12
12
|
assert_decimal :optional_price unless optional_price.to_s.empty?
|
13
13
|
end
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
context "given no price" do
|
17
17
|
should "still validate as :not_decimal" do
|
18
18
|
product = Product.new(:price => nil)
|
@@ -29,7 +29,7 @@ class TestOhmNumberValidations < Test::Unit::TestCase
|
|
29
29
|
assert product.valid?
|
30
30
|
end
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
context "given 1 as a value" do
|
34
34
|
should "validate as a decimal" do
|
35
35
|
product = Product.new(:price => 1)
|
@@ -56,4 +56,4 @@ class TestOhmNumberValidations < Test::Unit::TestCase
|
|
56
56
|
assert product.valid?
|
57
57
|
end
|
58
58
|
end
|
59
|
-
end
|
59
|
+
end
|
@@ -11,11 +11,11 @@ class TestOhmTimestamping < Test::Unit::TestCase
|
|
11
11
|
|
12
12
|
context "a new? record" do
|
13
13
|
should "have no created_at" do
|
14
|
-
assert_nil Person.new.created_at
|
14
|
+
assert_nil Person.new.created_at
|
15
15
|
end
|
16
16
|
|
17
17
|
should "have no updated_at" do
|
18
|
-
assert_nil Person.new.updated_at
|
18
|
+
assert_nil Person.new.updated_at
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
@@ -26,8 +26,8 @@ class TestOhmTimestamping < Test::Unit::TestCase
|
|
26
26
|
@person = Person.create
|
27
27
|
@person = Person[@person.id]
|
28
28
|
end
|
29
|
-
|
30
|
-
|
29
|
+
|
30
|
+
|
31
31
|
should "set the created_at equal to the current time" do
|
32
32
|
assert_equal @now.to_s, @person.created_at
|
33
33
|
end
|
@@ -48,13 +48,13 @@ class TestOhmTimestamping < Test::Unit::TestCase
|
|
48
48
|
@person.save
|
49
49
|
@person = Person[@person.id]
|
50
50
|
end
|
51
|
-
|
51
|
+
|
52
52
|
should "leave created_at unchanged" do
|
53
53
|
assert_equal @old_created_at, @person.created_at
|
54
54
|
end
|
55
|
-
|
55
|
+
|
56
56
|
should "set updated_at to the current Time" do
|
57
57
|
assert_equal @now.to_s, @person.updated_at
|
58
58
|
end
|
59
59
|
end
|
60
|
-
end
|
60
|
+
end
|
data/test/test_ohm_to_hash.rb
CHANGED
@@ -18,7 +18,7 @@ class TestOhmToHash < Test::Unit::TestCase
|
|
18
18
|
@person = Person.create(:name => 'matz', :skills => 10)
|
19
19
|
@person = Person[@person.id]
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
should "have a to_hash of { id: 1, name: 'matz', age: nil, skills: 10 }" do
|
23
23
|
assert_equal(
|
24
24
|
{ :id => '1', :name => "matz", :age => nil, :skills => '10' },
|
@@ -26,7 +26,7 @@ class TestOhmToHash < Test::Unit::TestCase
|
|
26
26
|
)
|
27
27
|
end
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
context "when a Post has a votes counter" do
|
31
31
|
class Post < Ohm::Model
|
32
32
|
include Ohm::ToHash
|
@@ -48,7 +48,7 @@ class TestOhmToHash < Test::Unit::TestCase
|
|
48
48
|
|
49
49
|
context "when a comment has a reference to a person" do
|
50
50
|
Person = Class.new(Ohm::Model)
|
51
|
-
|
51
|
+
|
52
52
|
class Comment < Ohm::Model
|
53
53
|
include Ohm::ToHash
|
54
54
|
|
@@ -64,4 +64,4 @@ class TestOhmToHash < Test::Unit::TestCase
|
|
64
64
|
assert_equal({ :id => '1', :person_id => '1' }, @comment.to_hash)
|
65
65
|
end
|
66
66
|
end
|
67
|
-
end
|
67
|
+
end
|
data/test/test_ohm_typecast.rb
CHANGED
@@ -29,7 +29,7 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
29
29
|
@time = Time.now.utc
|
30
30
|
product = Product.create(:start_of_sale => @time)
|
31
31
|
product = Product[product.id]
|
32
|
-
|
32
|
+
|
33
33
|
assert_equal @time, product.start_of_sale
|
34
34
|
end
|
35
35
|
|
@@ -37,7 +37,7 @@ 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
|
-
|
40
|
+
|
41
41
|
assert_kind_of Time, product.start_of_sale
|
42
42
|
end
|
43
43
|
|
@@ -78,16 +78,16 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
78
78
|
@date = Date.today
|
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
|
86
86
|
product = Product.create(:date_bought => "Bla Bla")
|
87
|
-
|
87
|
+
|
88
88
|
assert_equal "Bla Bla", product.date_bought
|
89
89
|
end
|
90
|
-
|
90
|
+
|
91
91
|
test "when nil" do
|
92
92
|
assert_nil Product.new.date_bought
|
93
93
|
end
|
@@ -156,4 +156,25 @@ class TestOhmTypecast < Test::Unit::TestCase
|
|
156
156
|
assert_equal 0.12, Product.new(:vat => ".12").vat
|
157
157
|
end
|
158
158
|
end
|
159
|
-
|
159
|
+
|
160
|
+
context "trying to validate numericality of a decimal" do
|
161
|
+
class Item < Ohm::Model
|
162
|
+
include Ohm::Typecast
|
163
|
+
|
164
|
+
attribute :price, Decimal
|
165
|
+
|
166
|
+
def validate
|
167
|
+
assert_numeric :price
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
setup do
|
172
|
+
@item = Item.new(:price => "FooBar")
|
173
|
+
end
|
174
|
+
|
175
|
+
test "invalidation of price" do
|
176
|
+
assert ! @item.valid?
|
177
|
+
assert_equal [[:price, :not_numeric]], @item.errors
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
@@ -26,7 +26,7 @@ class TestOhmWebValidations < Test::Unit::TestCase
|
|
26
26
|
assert_email :email
|
27
27
|
end
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
context "The slug should be valid" do
|
31
31
|
def setup
|
32
32
|
@blog_post_01 = BlogPost.new
|
@@ -36,7 +36,7 @@ class TestOhmWebValidations < Test::Unit::TestCase
|
|
36
36
|
should "fail if the slug is not valid" do
|
37
37
|
@blog_post_01.slug = "This is a title, not a SLUG"
|
38
38
|
@blog_post_01.create
|
39
|
-
|
39
|
+
|
40
40
|
assert @blog_post_01.new?
|
41
41
|
assert_equal [[:slug, :not_slug]], @blog_post_01.errors
|
42
42
|
end
|
@@ -108,6 +108,6 @@ class TestOhmWebValidations < Test::Unit::TestCase
|
|
108
108
|
assert @comment.new?
|
109
109
|
assert @comment.errors.include? [:homepage, :not_url]
|
110
110
|
end
|
111
|
-
|
111
|
+
|
112
112
|
end
|
113
|
-
end
|
113
|
+
end
|