ohm-contrib 0.0.23 → 0.0.24

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.23
1
+ 0.0.24
data/lib/ohm/contrib.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Ohm
2
2
  module Contrib
3
- VERSION = '0.0.23'
3
+ VERSION = '0.0.24'
4
4
  end
5
5
 
6
6
  autoload :Boundaries, "ohm/contrib/boundaries"
@@ -8,6 +8,7 @@ module Ohm
8
8
  autoload :ToHash, "ohm/contrib/to_hash"
9
9
  autoload :WebValidations, "ohm/contrib/web_validations"
10
10
  autoload :NumberValidations, "ohm/contrib/number_validations"
11
+ autoload :DateValidations, "ohm/contrib/date_validations"
11
12
  autoload :ExtraValidations, "ohm/contrib/extra_validations"
12
13
  autoload :Typecast, "ohm/contrib/typecast"
13
14
  autoload :Locking, "ohm/contrib/locking"
@@ -0,0 +1,23 @@
1
+ require 'date'
2
+
3
+ module Ohm
4
+ module DateValidations
5
+ DATE = /\A([0-9]{4})-([01]?[0-9])-([0123]?[0-9])\z/
6
+
7
+ def assert_date(att, error = [att, :not_date])
8
+ if assert_format att, DATE, error
9
+ m = send(att).to_s.match(DATE)
10
+ assert is_date_parseable?(m[1], m[2], m[3]), error
11
+ end
12
+ end
13
+
14
+ private
15
+ def is_date_parseable?(year, month, day)
16
+ Date.new(Integer(year), Integer(month), Integer(day))
17
+ rescue ArgumentError
18
+ return false
19
+ else
20
+ return true
21
+ end
22
+ end
23
+ end
@@ -12,6 +12,7 @@ module Ohm
12
12
  # attribute :author_email
13
13
  # attribute :url
14
14
  # attribute :ipaddr
15
+ # attribute :birthday
15
16
  #
16
17
  # def validate
17
18
  # super
@@ -21,6 +22,7 @@ module Ohm
21
22
  # assert_ipaddr :ipaddr
22
23
  # assert_url :url
23
24
  # assert_email :author_email
25
+ # assert_date :birthday
24
26
  # end
25
27
  # end
26
28
  #
@@ -32,10 +34,11 @@ module Ohm
32
34
  module ExtraValidations
33
35
  include NumberValidations
34
36
  include WebValidations
37
+ include DateValidations
35
38
 
36
39
  protected
37
40
  def assert_member(att, set, error = [att, :not_member])
38
41
  assert set.include?(send(att)), error
39
42
  end
40
43
  end
41
- end
44
+ end
@@ -15,6 +15,7 @@ module Ohm
15
15
  # * Time
16
16
  # * Hash
17
17
  # * Array
18
+ # * Boolean
18
19
  module Types
19
20
  def self.defined?(type)
20
21
  @constants ||= constants.map(&:to_s)
@@ -35,11 +36,15 @@ module Ohm
35
36
  ]
36
37
 
37
38
  def self.[](value)
38
- return (self::EMPTY.nil? ? nil : self::EMPTY.dup) if value.to_s.empty?
39
+ return empty if value.to_s.empty?
39
40
 
40
41
  new(value)
41
42
  end
42
43
 
44
+ def self.empty
45
+ defined?(self::RAW) ? self::RAW.new : nil
46
+ end
47
+
43
48
  def self.delegate_to(klass, except = @@delegation_blacklist)
44
49
  methods = klass.public_instance_methods.map(&:to_sym) - except
45
50
  def_delegators :object, *methods
@@ -51,8 +56,6 @@ module Ohm
51
56
  end
52
57
 
53
58
  class Primitive < Base
54
- EMPTY = nil
55
-
56
59
  def initialize(value)
57
60
  @raw = value
58
61
  end
@@ -120,6 +123,15 @@ module Ohm
120
123
  end
121
124
  end
122
125
 
126
+ class Boolean
127
+ def self.[](value)
128
+ case value
129
+ when 'false', false, '0', 0 then false
130
+ when 'true', true, '1', 1 then true
131
+ end
132
+ end
133
+ end
134
+
123
135
  class Serialized < Base
124
136
  attr :object
125
137
 
@@ -152,7 +164,6 @@ module Ohm
152
164
  end
153
165
 
154
166
  class Hash < Serialized
155
- EMPTY = {}
156
167
  RAW = ::Hash
157
168
 
158
169
  delegate_to ::Hash
@@ -165,7 +176,6 @@ module Ohm
165
176
  end
166
177
 
167
178
  class Array < Serialized
168
- EMPTY = []
169
179
  RAW = ::Array
170
180
 
171
181
  delegate_to ::Array
data/ohm-contrib.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ohm-contrib}
8
- s.version = "0.0.23"
8
+ s.version = "0.0.24"
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"]
12
- s.date = %q{2010-06-01}
12
+ s.date = %q{2010-06-11}
13
13
  s.description = %q{Highly decoupled drop-in functionality for Ohm models}
14
14
  s.email = %q{cyx.ucron@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
26
26
  "lib/ohm/contrib.rb",
27
27
  "lib/ohm/contrib/boundaries.rb",
28
28
  "lib/ohm/contrib/callbacks.rb",
29
+ "lib/ohm/contrib/date_validations.rb",
29
30
  "lib/ohm/contrib/extra_validations.rb",
30
31
  "lib/ohm/contrib/locking.rb",
31
32
  "lib/ohm/contrib/number_validations.rb",
@@ -38,6 +39,7 @@ Gem::Specification.new do |s|
38
39
  "test/test_ohm_boundaries.rb",
39
40
  "test/test_ohm_contrib.rb",
40
41
  "test/test_ohm_contrib_callbacks.rb",
42
+ "test/test_ohm_date_validations.rb",
41
43
  "test/test_ohm_extra_validations.rb",
42
44
  "test/test_ohm_number_validations.rb",
43
45
  "test/test_ohm_timestamping.rb",
@@ -55,6 +57,7 @@ Gem::Specification.new do |s|
55
57
  "test/test_ohm_boundaries.rb",
56
58
  "test/test_ohm_contrib.rb",
57
59
  "test/test_ohm_contrib_callbacks.rb",
60
+ "test/test_ohm_date_validations.rb",
58
61
  "test/test_ohm_extra_validations.rb",
59
62
  "test/test_ohm_number_validations.rb",
60
63
  "test/test_ohm_timestamping.rb",
@@ -48,4 +48,10 @@ class TestOhmContrib < Test::Unit::TestCase
48
48
  Ohm::ExtraValidations
49
49
  end
50
50
  end
51
- end
51
+
52
+ test "autoloading of DateValidations" do
53
+ assert_nothing_raised NameError, LoadError do
54
+ Ohm::DateValidations
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,30 @@
1
+ require "helper"
2
+
3
+ class DateValidationsTest < Test::Unit::TestCase
4
+ describe "date validation with a normal string column" do
5
+ class Person < Ohm::Model
6
+ include Ohm::DateValidations
7
+
8
+ attribute :birthday
9
+
10
+ def validate
11
+ assert_date :birthday
12
+ end
13
+ end
14
+
15
+ test "accepts all canonical dates" do
16
+ assert Person.new(:birthday => "2010-05-05").valid?
17
+ assert Person.new(:birthday => "2010-5-5").valid?
18
+ assert Person.new(:birthday => "2010-05-5").valid?
19
+ assert Person.new(:birthday => "2010-5-05").valid?
20
+ end
21
+
22
+ test "also catches invalid dates" do
23
+ assert ! Person.new(:birthday => "2010-02-29").valid?
24
+ end
25
+
26
+ test "invalid when empty" do
27
+ assert ! Person.new(:birthday => "").valid?
28
+ end
29
+ end
30
+ end
@@ -29,4 +29,4 @@ class OhmExtraValidationsTest < Test::Unit::TestCase
29
29
  assert_equal [[:state, :not_member]], order.errors
30
30
  end
31
31
  end
32
- end
32
+ end
@@ -614,4 +614,45 @@ class TestOhmTypecast < Test::Unit::TestCase
614
614
  assert_equal %{"\\\"FooBar\\\""}, post.addresses.inspect
615
615
  end
616
616
  end
617
- end
617
+
618
+ context "when using Boolean" do
619
+ class User < Ohm::Model
620
+ include Ohm::Typecast
621
+
622
+ attribute :is_admin, Boolean
623
+ end
624
+
625
+ test "empty is nil" do
626
+ assert_equal nil, User.new.is_admin
627
+
628
+ u = User.create
629
+ u = User[u.id]
630
+
631
+ assert_equal nil, User.new.is_admin
632
+ end
633
+
634
+ test "false, 0, '0' is false" do
635
+ [false, 0, '0'].each do |val|
636
+ assert_equal false, User.new(:is_admin => val).is_admin
637
+ end
638
+
639
+ [false, 0, '0'].each do |val|
640
+ u = User.create(:is_admin => val)
641
+ u = User[u.id]
642
+ assert_equal false, u.is_admin
643
+ end
644
+ end
645
+
646
+ test "true, 1, '1' is true" do
647
+ [true, 1, '1'].each do |val|
648
+ assert_equal true, User.new(:is_admin => val).is_admin
649
+ end
650
+
651
+ [true, 1, '1'].each do |val|
652
+ u = User.create(:is_admin => val)
653
+ u = User[u.id]
654
+ assert_equal true, u.is_admin
655
+ end
656
+ end
657
+ end
658
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 23
9
- version: 0.0.23
8
+ - 24
9
+ version: 0.0.24
10
10
  platform: ruby
11
11
  authors:
12
12
  - Cyril David
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-01 00:00:00 +08:00
17
+ date: 2010-06-11 00:00:00 +08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -96,6 +96,7 @@ files:
96
96
  - lib/ohm/contrib.rb
97
97
  - lib/ohm/contrib/boundaries.rb
98
98
  - lib/ohm/contrib/callbacks.rb
99
+ - lib/ohm/contrib/date_validations.rb
99
100
  - lib/ohm/contrib/extra_validations.rb
100
101
  - lib/ohm/contrib/locking.rb
101
102
  - lib/ohm/contrib/number_validations.rb
@@ -108,6 +109,7 @@ files:
108
109
  - test/test_ohm_boundaries.rb
109
110
  - test/test_ohm_contrib.rb
110
111
  - test/test_ohm_contrib_callbacks.rb
112
+ - test/test_ohm_date_validations.rb
111
113
  - test/test_ohm_extra_validations.rb
112
114
  - test/test_ohm_number_validations.rb
113
115
  - test/test_ohm_timestamping.rb
@@ -149,6 +151,7 @@ test_files:
149
151
  - test/test_ohm_boundaries.rb
150
152
  - test/test_ohm_contrib.rb
151
153
  - test/test_ohm_contrib_callbacks.rb
154
+ - test/test_ohm_date_validations.rb
152
155
  - test/test_ohm_extra_validations.rb
153
156
  - test/test_ohm_number_validations.rb
154
157
  - test/test_ohm_timestamping.rb