ohm-contrib 0.1.2 → 1.0.rc0

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.
Files changed (52) hide show
  1. data/lib/ohm/{contrib/callbacks.rb → callbacks.rb} +20 -65
  2. data/lib/ohm/contrib.rb +9 -17
  3. data/lib/ohm/datatypes.rb +38 -0
  4. data/lib/ohm/{contrib/locking.rb → locking.rb} +1 -2
  5. data/lib/ohm/plugin.rb +54 -0
  6. data/lib/ohm/{contrib/scope.rb → scope.rb} +7 -6
  7. data/lib/ohm/{contrib/slug.rb → slug.rb} +11 -6
  8. data/lib/ohm/{contrib/soft_delete.rb → softdelete.rb} +23 -19
  9. data/lib/ohm/timestamping.rb +41 -0
  10. data/ohm-contrib.gemspec +30 -0
  11. data/rakefile +6 -0
  12. data/test/{instance_callbacks_test.rb → instance_callbacks.rb} +2 -6
  13. data/test/{macro_callbacks_test.rb → macro_callbacks.rb} +1 -9
  14. data/test/plugin.rb +212 -0
  15. data/test/{scope_test.rb → scope.rb} +2 -3
  16. data/test/slug.rb +24 -0
  17. data/test/{soft_delete_test.rb → soft_delete.rb} +13 -3
  18. data/test/{timestamping_test.rb → timestamping.rb} +5 -6
  19. metadata +33 -70
  20. data/Rakefile +0 -8
  21. data/lib/ohm/contrib/active_model_extension.rb +0 -87
  22. data/lib/ohm/contrib/boundaries.rb +0 -41
  23. data/lib/ohm/contrib/date_validations.rb +0 -23
  24. data/lib/ohm/contrib/extra_validations.rb +0 -48
  25. data/lib/ohm/contrib/fulltext_searching.rb +0 -80
  26. data/lib/ohm/contrib/length_validations.rb +0 -32
  27. data/lib/ohm/contrib/number_validations.rb +0 -14
  28. data/lib/ohm/contrib/timestamping.rb +0 -38
  29. data/lib/ohm/contrib/typecast.rb +0 -350
  30. data/lib/ohm/contrib/web_validations.rb +0 -52
  31. data/test/activemodel_test.rb +0 -27
  32. data/test/boundaries_test.rb +0 -45
  33. data/test/callbacks_lint.rb +0 -141
  34. data/test/date_validations_test.rb +0 -29
  35. data/test/fixtures/ascii8bit.txt +0 -1
  36. data/test/fulltext_searching_test.rb +0 -63
  37. data/test/length_validations_test.rb +0 -31
  38. data/test/membership_validation_test.rb +0 -31
  39. data/test/number_validations_test.rb +0 -37
  40. data/test/slug_test.rb +0 -30
  41. data/test/typecast_array_test.rb +0 -146
  42. data/test/typecast_boolean_test.rb +0 -43
  43. data/test/typecast_date_test.rb +0 -82
  44. data/test/typecast_decimal_test.rb +0 -82
  45. data/test/typecast_existing_attribute_test.rb +0 -22
  46. data/test/typecast_float_test.rb +0 -57
  47. data/test/typecast_hash_test.rb +0 -120
  48. data/test/typecast_integer_test.rb +0 -71
  49. data/test/typecast_string_test.rb +0 -43
  50. data/test/typecast_time_test.rb +0 -72
  51. data/test/typecast_timezone_test.rb +0 -37
  52. data/test/web_validations_test.rb +0 -79
@@ -1,82 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require File.expand_path("./helper", File.dirname(__FILE__))
4
-
5
- class Post < Ohm::Model
6
- include Ohm::Typecast
7
-
8
- attribute :price, Decimal
9
- end
10
-
11
- test "handles nil case correctly" do
12
- post = Post.create(:price => nil)
13
- post = Post[post.id]
14
-
15
- assert nil == post.price
16
- end
17
-
18
- test "handles empty string case correctly" do
19
- post = Post.create(:price => "")
20
- post = Post[post.id]
21
-
22
- assert "" == post.price.to_s
23
- end
24
-
25
- test "allows for real arithmetic" do
26
- post = Post.create(:price => "0.01")
27
- post = Post[post.id]
28
-
29
- assert 0.02 == post.price + post.price
30
- assert 0.0 == post.price - post.price
31
- assert 0.0001 == post.price * post.price
32
- assert 1.0 == post.price / post.price
33
- end
34
-
35
- test "is accurate accdg to the decimal spec" do
36
- post = Post.create(:price => "0.0001")
37
- post = Post[post.id]
38
-
39
- sum = 0
40
- 1_000.times { sum += post.price }
41
- assert 0.1 == sum
42
- end
43
-
44
- test "using += with price" do
45
- post = Post.create(:price => "0.0001")
46
- post = Post[post.id]
47
-
48
- post.price += 1
49
- assert 1.0001 == post.price.to_f
50
- end
51
-
52
- test "assigning a raw BigDecimal" do
53
- post = Post.create(:price => BigDecimal("399.50"))
54
- post = Post[post.id]
55
-
56
- assert post.price.to_s.kind_of?(String)
57
- end
58
-
59
- test "equality and comparable matching" do
60
- post = Post.create(:price => "399.50")
61
- assert (post.price == "399.50")
62
- assert (post.price < 399.51)
63
- assert (post.price > 399.49)
64
- assert (post.price <= 399.50)
65
- assert (post.price <= 399.51)
66
- assert (post.price >= 399.50)
67
- assert (post.price >= 399.49)
68
- end
69
-
70
- test "inspecting a Decimal" do
71
- post = Post.new(:price => 399.50)
72
- assert '"399.5"' == post.price.inspect
73
-
74
- post.price = 'FooBar'
75
- assert '"FooBar"' == post.price.inspect
76
- end
77
-
78
- test "type is BigDecimal" do
79
- post = Post.new(:price => 399.50)
80
- assert BigDecimal == post.price.type
81
- end
82
-
@@ -1,22 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require File.expand_path("./helper", File.dirname(__FILE__))
4
-
5
- class Post < Ohm::Model
6
- include Ohm::Timestamping
7
- include Ohm::Typecast
8
-
9
- typecast :created_at, Time
10
- typecast :updated_at, Time
11
- end
12
-
13
- test "created_at and updated_at are typecasted" do
14
- post = Post.create
15
-
16
- assert post.created_at.respond_to?(:strftime)
17
- assert post.updated_at.respond_to?(:strftime)
18
-
19
- assert NOW.strftime("%Y-%m-%d") == post.created_at.strftime("%Y-%m-%d")
20
- assert NOW.strftime("%Y-%m-%d") == post.updated_at.strftime("%Y-%m-%d")
21
- end
22
-
@@ -1,57 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require File.expand_path("./helper", File.dirname(__FILE__))
4
-
5
- class Post < Ohm::Model
6
- include Ohm::Typecast
7
-
8
- attribute :price, Float
9
- end
10
-
11
- test "handles nil case correctly" do
12
- post = Post.create(:price => nil)
13
- post = Post[post.id]
14
-
15
- assert nil == post.price
16
- end
17
-
18
- test "handles empty string case correctly" do
19
- post = Post.create(:price => "")
20
- post = Post[post.id]
21
-
22
- assert "" == post.price.to_s
23
- end
24
-
25
- test "allows for real arithmetic" do
26
- post = Post.create(:price => "3")
27
- post = Post[post.id]
28
-
29
- assert 6.0 == post.price + post.price
30
- assert 0.0 == post.price - post.price
31
- assert 9.0 == post.price * post.price
32
- assert 1.0 == post.price / post.price
33
- end
34
-
35
- test "raises when trying to do arithmetic ops on a non-float" do
36
-
37
- post = Post.create(:price => "FooBar")
38
- post = Post[post.id]
39
-
40
- assert_raise ArgumentError do
41
- post.price * post.price
42
- end
43
- end
44
-
45
- test "inspecting" do
46
- post = Post.new(:price => "12345.67890")
47
- assert '"12345.67890"' == post.price.inspect
48
-
49
- post.price = 'FooBar'
50
- assert '"FooBar"' == post.price.inspect
51
- end
52
-
53
- test "type is Float" do
54
- post = Post.new(:price => 399.50)
55
- assert Float == post.price.type
56
- end
57
-
@@ -1,120 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require File.expand_path("./helper", File.dirname(__FILE__))
4
-
5
- class Post < Ohm::Model
6
- include Ohm::Typecast
7
-
8
- attribute :address, Hash
9
-
10
- def hash
11
- Hash.new
12
- end
13
-
14
- def top_level_hash
15
- Hash
16
- end
17
- end
18
-
19
- test "importing" do
20
- assert Hash.new == Ohm::Types::Hash[nil]
21
- assert Hash.new == Ohm::Types::Hash[""]
22
- assert Hash.new == Ohm::Types::Hash[{}]
23
-
24
- expected = Hash[:a => "b", :c => "d"]
25
- assert expected == Ohm::Types::Hash[{ :a => "b", :c => "d" }]
26
- end
27
-
28
- test "exporting / dumping" do
29
- assert "{}" == Ohm::Types::Hash[nil].to_s
30
- assert "{}" == Ohm::Types::Hash[""].to_s
31
- assert "{}" == Ohm::Types::Hash[{}].to_s
32
-
33
- expected = %q{{"a":"b","c":"d"}}
34
- assert expected == Ohm::Types::Hash[{ :a => "b", :c => "d" }].to_s
35
- end
36
-
37
- test "still able to get top level methods" do
38
- assert Post.new.hash == {}
39
- assert Hash == Post.new.top_level_hash
40
- end
41
-
42
- test "handles nil case correctly" do
43
- post = Post.create(:address => nil)
44
- assert post.address == {}
45
-
46
- post = Post[post.id]
47
- assert post.address == {}
48
- end
49
-
50
- test "handles empty string case correctly" do
51
- post = Post.create(:address => "")
52
- assert post.address == {}
53
-
54
- post = Post[post.id]
55
- assert post.address == {}
56
- end
57
-
58
- test "handles populated hashes" do
59
- address = { "address1" => "#123", "city" => "Singapore", "country" => "SG"}
60
- post = Post.create(:address => address)
61
- assert address == post.address
62
-
63
- post = Post[post.id]
64
- assert address == post.address
65
- end
66
-
67
- test "allows for hash operations" do
68
- address = { "address1" => "#123", "city" => "Singapore", "country" => "SG"}
69
- post = Post.create(:address => address)
70
-
71
- assert ["address1", "city", "country"] == post.address.keys
72
- assert ["#123", "Singapore", "SG"] == post.address.values
73
-
74
- post = Post[post.id]
75
- assert ["address1", "city", "country"] == post.address.keys
76
- assert ["#123", "Singapore", "SG"] == post.address.values
77
- end
78
-
79
- test "handles mutation" do
80
- address = { "address1" => "#123", "city" => "Singapore", "country" => "SG"}
81
- post = Post.create(:address => address)
82
-
83
- post.address["address1"] = "#456"
84
- post.save
85
-
86
- assert ["address1", "city", "country"] == post.address.keys
87
- assert ["#456", "Singapore", "SG"] == post.address.values
88
-
89
- post = Post[post.id]
90
- assert ["address1", "city", "country"] == post.address.keys
91
- assert ["#456", "Singapore", "SG"] == post.address.values
92
- end
93
-
94
- Address = Class.new(Struct.new(:city, :country))
95
-
96
- test "raises when trying to assign a non-hash" do
97
- assert_raise TypeError do
98
- Post.new(:address => [])
99
- end
100
-
101
- assert_raise TypeError do
102
- Post.new(:address => Address.new)
103
- end
104
- end
105
-
106
- test "type is Hash" do
107
- post = Post.new(:address => { "address1" => "#456" })
108
- assert Hash == post.address.type
109
- end
110
-
111
- test "ascii 8bit encoding" do
112
- txt = File.expand_path("fixtures/ascii8bit.txt", File.dirname(__FILE__))
113
-
114
- data = File.read(txt, :encoding => "ascii-8bit")
115
-
116
- post = Post.create(:address => { "address1" => data.force_encoding("UTF-8") })
117
- post = Post[post.id]
118
-
119
- assert_equal post.address["address1"], data.force_encoding("UTF-8")
120
- end if defined?(Encoding)
@@ -1,71 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require File.expand_path("./helper", File.dirname(__FILE__))
4
-
5
- class Post < Ohm::Model
6
- include Ohm::Typecast
7
-
8
- attribute :price, Integer
9
- end
10
-
11
- test "handles nil case correctly" do
12
- post = Post.create(:price => nil)
13
- post = Post[post.id]
14
-
15
- assert nil == post.price
16
- end
17
-
18
- test "handles empty string case correctly" do
19
- post = Post.create(:price => "")
20
- post = Post[post.id]
21
-
22
- assert "" == post.price.to_s
23
- end
24
-
25
- test "allows respond_to on an invalid integer" do
26
- post = Post.new(:price => "FooBar")
27
-
28
- assert_nothing_raised ArgumentError do
29
- post.price.respond_to?(:**)
30
- end
31
-
32
- assert ! post.price.respond_to?(:**)
33
- end
34
-
35
- test "falls back to String#respond_to? when invalid" do
36
- post = Post.new(:price => "FooBar")
37
- assert post.price.respond_to?(:capitalize)
38
- end
39
-
40
- test "allows for real arithmetic" do
41
- post = Post.create(:price => "3")
42
- post = Post[post.id]
43
-
44
- assert 6 == post.price + post.price
45
- assert 0 == post.price - post.price
46
- assert 9 == post.price * post.price
47
- assert 1 == post.price / post.price
48
- end
49
-
50
- test "raises when trying to do arithmetic ops on a non-int" do
51
- post = Post.create(:price => "FooBar")
52
- post = Post[post.id]
53
-
54
- assert_raise ArgumentError do
55
- post.price * post.price
56
- end
57
- end
58
-
59
- test "inspecting" do
60
- post = Post.new(:price => "50000")
61
- assert '"50000"' == post.price.inspect
62
-
63
- post.price = 'FooBar'
64
- assert '"FooBar"' == post.price.inspect
65
- end
66
-
67
- test "type is Fixnum" do
68
- post = Post.new(:price => 399)
69
- assert Fixnum == post.price.type
70
- end
71
-
@@ -1,43 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require File.expand_path("./helper", File.dirname(__FILE__))
4
-
5
- class Post < Ohm::Model
6
- include Ohm::Typecast
7
-
8
- attribute :content
9
- end
10
-
11
- test "handles nil case correctly" do
12
- post = Post.create(:content => nil)
13
- post = Post[post.id]
14
-
15
- assert nil == post.content
16
- end
17
-
18
- test "still responds to string methods properly" do
19
- post = Post.create(:content => "FooBar")
20
- post = Post[post.id]
21
-
22
- assert "foobar" == post.content.downcase
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 "FOOBAR" == post.content.to_s
32
- end
33
-
34
- test "inspecting" do
35
- post = Post.new(:content => "FooBar")
36
- assert 'FooBar' == post.content
37
- end
38
-
39
- test "type is String" do
40
- post = Post.new(:content => "FooBar")
41
- assert String == post.content.type
42
- end
43
-
@@ -1,72 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require File.expand_path("./helper", File.dirname(__FILE__))
4
-
5
- class Post < Ohm::Model
6
- include Ohm::Typecast
7
-
8
- attribute :created_at, Time
9
-
10
- def now
11
- Time.now
12
- end
13
-
14
- def new_time
15
- Time.new
16
- end
17
- end
18
-
19
- test "still able to access top level Time" do
20
- assert Post.new.now.to_s == Time.now.to_s
21
- end
22
-
23
- test "should be able to use Time.new inside the class" do
24
- assert Post.new.new_time.to_s == Time.new.to_s
25
- end
26
-
27
- test "handles nil case correctly" do
28
- post = Post.create(:created_at => nil)
29
- post = Post[post.id]
30
-
31
- assert nil == post.created_at
32
- end
33
-
34
- test "handles empty string case correctly" do
35
- post = Post.create(:created_at => "")
36
- post = Post[post.id]
37
-
38
- assert "" == post.created_at.to_s
39
- end
40
-
41
- test "allows for real time operations" do
42
- post = Post.create(:created_at => "2010-05-10T00:00Z")
43
- post = Post[post.id]
44
-
45
- assert post.created_at.respond_to?(:strftime)
46
- assert "2010-05-10" == post.created_at.strftime('%Y-%m-%d')
47
- end
48
-
49
- test "raises when trying to do non-time operations" do
50
- post = Post.create(:created_at => "FooBar")
51
- post = Post[post.id]
52
-
53
- assert ! post.created_at.respond_to?(:abs)
54
-
55
- assert_raise NoMethodError do
56
- post.created_at.abs
57
- end
58
- end
59
-
60
- test "inspecting" do
61
- post = Post.create(:created_at => Time.utc(2010, 05, 05))
62
- assert '"2010-05-05 00:00:00 UTC"' == post.created_at.inspect
63
-
64
- post.created_at = 'FooBar'
65
- assert '"FooBar"' == post.created_at.inspect
66
- end
67
-
68
- test "type is Time" do
69
- post = Post.create(:created_at => Time.utc(2010, 05, 05))
70
- assert Time == post.created_at.type
71
- end
72
-
@@ -1,37 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require File.expand_path("./helper", File.dirname(__FILE__))
4
-
5
- class Post < Ohm::Model
6
- include Ohm::Typecast
7
-
8
- attribute :printed_at, Time
9
- end
10
-
11
- test "2010-08-07T00:00Z is parsed as 2010-08-07 00:00:00 UTC" do
12
- post = Post.new(:printed_at => "2010-08-07T00:00Z")
13
- assert Time.utc(2010, 8, 7).to_s == post.printed_at.utc.to_s
14
-
15
- post.save
16
- post = Post[post.id]
17
- assert Time.utc(2010, 8, 7).to_s == post.printed_at.utc.to_s
18
- end
19
-
20
- test "2010-08-07 18:29Z is parsed as 2010-08-07 18:29:00 UTC" do
21
- post = Post.new(:printed_at => "2010-08-07 18:29Z")
22
- assert Time.utc(2010, 8, 7, 18, 29).to_s == post.printed_at.utc.to_s
23
-
24
- post.save
25
- post = Post[post.id]
26
- assert Time.utc(2010, 8, 7, 18, 29).to_s == post.printed_at.utc.to_s
27
- end
28
-
29
- test "2010-08-07T18:29:31Z is parsed as 2010-08-07 18:29:31 UTC" do
30
- post = Post.new(:printed_at => "2010-08-07T18:29:31Z")
31
- assert Time.utc(2010, 8, 7, 18, 29, 31).to_s == post.printed_at.utc.to_s
32
-
33
- post.save
34
- post = Post[post.id]
35
- assert Time.utc(2010, 8, 7, 18, 29, 31).to_s == post.printed_at.utc.to_s
36
- end
37
-
@@ -1,79 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require File.expand_path("./helper", File.dirname(__FILE__))
4
-
5
- class BlogPost < Ohm::Model
6
- include Ohm::WebValidations
7
-
8
- attribute :slug
9
-
10
- index :slug
11
-
12
- def validate
13
- assert_slug :slug
14
- end
15
- end
16
-
17
- class Comment < Ohm::Model
18
- include Ohm::WebValidations
19
-
20
- attribute :ip_address
21
- attribute :homepage
22
- attribute :email
23
-
24
- def validate
25
- assert_ipaddr :ip_address
26
- assert_url :homepage
27
- assert_email :email
28
- end
29
- end
30
-
31
- test "invalid slug scenario" do
32
- p = BlogPost.new(:slug => "This is a title, not a SLUG")
33
- assert ! p.valid?
34
- assert [[:slug, :not_slug]] == p.errors
35
- end
36
-
37
- test "valid slug scenario" do
38
- p = BlogPost.new(:slug => "this-is-a-valid-slug")
39
- assert p.valid?
40
- end
41
-
42
- test "slug uniqueness validation" do
43
- p1 = BlogPost.create(:slug => "this-is-a-valid-slug")
44
- p2 = BlogPost.create(:slug => "this-is-a-valid-slug")
45
-
46
- assert ! p2.valid?
47
- assert [[:slug, :not_unique]] == p2.errors
48
- end
49
-
50
- test "ip address validation" do
51
- c = Comment.new(:ip_address => "400.500.600.700")
52
- assert ! c.valid?
53
- assert c.errors.include?([:ip_address, :not_ipaddr])
54
- end
55
-
56
- test "email address validation" do
57
- c = Comment.new(:email => "something.com")
58
- assert ! c.valid?
59
- assert c.errors.include?([:email, :not_email])
60
-
61
- c = Comment.new(:email => "john@doe.com")
62
- c.valid?
63
- assert ! c.errors.include?([:email, :not_email])
64
- end
65
-
66
- test "url validaiton" do
67
- c = Comment.new(:homepage => "somehing")
68
- assert ! c.valid?
69
- assert c.errors.include?([:homepage, :not_url])
70
-
71
- c = Comment.new(:homepage => "irc://irc.freenode.net/something")
72
- assert ! c.valid?
73
- assert c.errors.include?([:homepage, :not_url])
74
-
75
- c = Comment.new(:homepage => "http://test.com")
76
- c.valid?
77
- assert ! c.errors.include?([:homepage, :not_url])
78
- end
79
-