ohm-contrib 0.0.7 → 0.0.8

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.7
1
+ 0.0.8
@@ -1,6 +1,6 @@
1
1
  module Ohm
2
2
  module Contrib
3
- VERSION = '0.0.7'
3
+ VERSION = '0.0.8'
4
4
  end
5
5
 
6
6
  autoload :Boundaries, "ohm/contrib/boundaries"
@@ -11,6 +11,8 @@ module Ohm
11
11
  end
12
12
 
13
13
  class Time < ::Time
14
+ ASSERTION = :assert_type_time
15
+
14
16
  def self.[](value)
15
17
  return value if value.to_s.empty?
16
18
 
@@ -20,6 +22,8 @@ module Ohm
20
22
  end
21
23
 
22
24
  class Date < ::Date
25
+ ASSERTION = :assert_type_date
26
+
23
27
  def self.[](value)
24
28
  return value if value.to_s.empty?
25
29
 
@@ -30,6 +34,7 @@ module Ohm
30
34
  end
31
35
 
32
36
  class Decimal < BigDecimal
37
+ ASSERTION = :assert_type_decimal
33
38
  CANONICAL = /^(\d+)?(\.\d+)?(E[+\-]\d+)?$/
34
39
 
35
40
  def self.[](value)
@@ -44,6 +49,8 @@ module Ohm
44
49
  end
45
50
 
46
51
  class Float < ::Float
52
+ ASSERTION = :assert_type_float
53
+
47
54
  def self.[](value)
48
55
  return value if value.to_s.empty?
49
56
 
@@ -54,6 +61,8 @@ module Ohm
54
61
  end
55
62
 
56
63
  class Integer < ::Integer
64
+ ASSERTION = :assert_type_integer
65
+
57
66
  def self.[](value)
58
67
  return value if value.to_s.empty?
59
68
 
@@ -92,21 +101,16 @@ module Ohm
92
101
  class MissingValidation < StandardError
93
102
  MESSAGE = "%s :%s is required in your #validate method"
94
103
 
95
- attr :type
96
104
  attr :field
105
+ attr :assertion
97
106
 
98
- def initialize(type, field)
99
- @type, @field = type, field
107
+ def initialize(field, assertion)
108
+ @field, @assertion = field, assertion
100
109
  end
101
110
 
102
111
  def message
103
112
  MESSAGE % [assertion, field]
104
113
  end
105
-
106
- protected
107
- def assertion
108
- 'assert_type_%s' % type.name.split('::').last.downcase
109
- end
110
114
  end
111
115
 
112
116
  include Types
@@ -117,7 +121,7 @@ module Ohm
117
121
  end
118
122
 
119
123
  module ClassMethods
120
- def attribute(name, type = Ohm::Types::String)
124
+ def attribute(name, type = Ohm::Types::String, assertion = defined?(type::ASSERTION) ? type::ASSERTION : nil)
121
125
  define_method(name) do
122
126
  type[read_local(name)]
123
127
  end
@@ -131,7 +135,7 @@ module Ohm
131
135
  end
132
136
 
133
137
  attributes << name unless attributes.include?(name)
134
- types[name] = type unless types.has_key?(name)
138
+ types[name] = [type, assertion] unless types.has_key?(name)
135
139
  end
136
140
 
137
141
  def types
@@ -142,13 +146,19 @@ module Ohm
142
146
  def valid?
143
147
  return unless super
144
148
 
145
- self.class.types.each do |field, type|
149
+ self.class.types.each do |field, (type, assertion)|
146
150
  value = send(field)
147
151
 
148
152
  unless value.kind_of?(type)
149
- raise MissingValidation.new(type, field)
153
+ raise MissingValidation.new(field, assertion)
150
154
  end
151
155
  end
152
156
  end
157
+
158
+ def validate
159
+ self.class.types.each do |field, (type, assertion)|
160
+ send assertion, field if assertion
161
+ end
162
+ end
153
163
  end
154
164
  end
@@ -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.7"
8
+ s.version = "0.0.8"
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"]
@@ -6,6 +6,10 @@ class TestOhmTypecast < Test::Unit::TestCase
6
6
  include Ohm::Typecast
7
7
 
8
8
  attribute :price, Decimal
9
+
10
+ # override validate and purposely forget super
11
+ def validate
12
+ end
9
13
  end
10
14
 
11
15
  should "properly preserve the right precision" do
@@ -41,12 +45,6 @@ class TestOhmTypecast < Test::Unit::TestCase
41
45
  include Ohm::Typecast
42
46
 
43
47
  attribute :price, Decimal
44
-
45
- def validate
46
- super
47
-
48
- assert_type_decimal :price
49
- end
50
48
  end
51
49
 
52
50
  should "be invalid and have an error on the field" do
@@ -63,6 +61,10 @@ class TestOhmTypecast < Test::Unit::TestCase
63
61
  include Ohm::Typecast
64
62
 
65
63
  attribute :start_of_sale, Time
64
+
65
+ # we override validate and purposely forget to call super
66
+ def validate
67
+ end
66
68
  end
67
69
 
68
70
  test "read / write" do
@@ -107,12 +109,6 @@ class TestOhmTypecast < Test::Unit::TestCase
107
109
  include Ohm::Typecast
108
110
 
109
111
  attribute :start_of_sale, Time
110
-
111
- def validate
112
- super
113
-
114
- assert_type_time :start_of_sale
115
- end
116
112
  end
117
113
 
118
114
  should "be valid given invalid time value" do
@@ -127,6 +123,10 @@ class TestOhmTypecast < Test::Unit::TestCase
127
123
  include Ohm::Typecast
128
124
 
129
125
  attribute :date_bought, Date
126
+
127
+ # override validate and purposely forget super
128
+ def validate
129
+ end
130
130
  end
131
131
 
132
132
  test "read / write" do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 7
9
- version: 0.0.7
8
+ - 8
9
+ version: 0.0.8
10
10
  platform: ruby
11
11
  authors:
12
12
  - Cyril David