modelish 0.1.0 → 0.1.1
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/CHANGELOG.md +7 -0
- data/lib/modelish/property_types.rb +3 -2
- data/lib/modelish/validations.rb +3 -2
- data/lib/modelish/version.rb +1 -1
- data/spec/modelish/property_types_spec.rb +10 -0
- data/spec/modelish/validations_spec.rb +31 -0
- metadata +13 -4
data/CHANGELOG.md
ADDED
@@ -29,6 +29,7 @@ module Modelish
|
|
29
29
|
# * +Float+
|
30
30
|
# * +Array+
|
31
31
|
# * +Date+ -- converts using Date.parse on value.to_s
|
32
|
+
# * +DateTime+ -- converts using DateTime.parse on value.to_s
|
32
33
|
# * +Symbol+ -- also converts from camel case to snake case
|
33
34
|
# * +String+
|
34
35
|
# * any arbitrary +Class+ -- will attempt conversion by passing the raw
|
@@ -112,8 +113,8 @@ module Modelish
|
|
112
113
|
end
|
113
114
|
|
114
115
|
def value_converter(property_type)
|
115
|
-
if property_type
|
116
|
-
lambda { |val|
|
116
|
+
if [Date, DateTime].include?(property_type)
|
117
|
+
lambda { |val| property_type.parse(val.to_s) }
|
117
118
|
elsif property_type == Symbol
|
118
119
|
lambda { |val| val.to_s.strip.gsub(/([A-Z]+)([A-Z][a-z])/, '\\1_\\2').
|
119
120
|
gsub(/([a-z\d])([A-Z])/, '\\1_\\2').
|
data/lib/modelish/validations.rb
CHANGED
@@ -172,6 +172,7 @@ module Modelish
|
|
172
172
|
# * +Integer+
|
173
173
|
# * +Float+
|
174
174
|
# * +Date+
|
175
|
+
# * +DateTime+
|
175
176
|
# * +Symbol+
|
176
177
|
# * any arbitrary +Class+ -- validates based on the results of is_a?
|
177
178
|
# @return [ArgumentError] when validation fails
|
@@ -185,8 +186,8 @@ module Modelish
|
|
185
186
|
is_valid = (value.is_a?(Integer) || value.to_s =~ /^\-?\d+$/)
|
186
187
|
elsif type == Float
|
187
188
|
is_valid = (value.is_a?(Float) || value.to_s =~ /^\-?\d+\.?\d*$/)
|
188
|
-
elsif type
|
189
|
-
is_valid =
|
189
|
+
elsif [Date, DateTime].include?(type)
|
190
|
+
is_valid = value.is_a?(type) || (type.parse(value.to_s) rescue false)
|
190
191
|
elsif type == Symbol
|
191
192
|
is_valid = value.respond_to?(:to_sym)
|
192
193
|
else
|
data/lib/modelish/version.rb
CHANGED
@@ -55,6 +55,16 @@ describe Modelish::PropertyTypes do
|
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
|
+
context "when property_type is DateTime" do
|
59
|
+
let(:property_type) { DateTime }
|
60
|
+
|
61
|
+
it_should_behave_like 'a typed property', :my_property, DateTime do
|
62
|
+
let(:valid_string) { '2011-02-24T14:09:43-07:00' }
|
63
|
+
let(:valid_typed_value) { DateTime.civil(2011, 2, 24, 14, 9, 43, Rational(-7, 24)) }
|
64
|
+
let(:invalid_value) { 'foo' }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
58
68
|
context "when property_type is String" do
|
59
69
|
let(:property_type) { String }
|
60
70
|
|
@@ -389,6 +389,37 @@ describe Modelish::Validations do
|
|
389
389
|
end
|
390
390
|
end
|
391
391
|
|
392
|
+
context "for type DateTime" do
|
393
|
+
let(:property_type) { DateTime }
|
394
|
+
|
395
|
+
context "with nil value" do
|
396
|
+
let(:property_value) { nil }
|
397
|
+
|
398
|
+
it { should be_nil }
|
399
|
+
end
|
400
|
+
|
401
|
+
context "with valid string" do
|
402
|
+
let(:property_value) { '2011-03-10T03:15:23-05:00' }
|
403
|
+
|
404
|
+
it { should be_nil }
|
405
|
+
end
|
406
|
+
|
407
|
+
context "with valid DateTime" do
|
408
|
+
let(:property_value) { DateTime.civil(2011, 3, 10, 3, 15, 23, Rational(-5, 24)) }
|
409
|
+
|
410
|
+
it { should be_nil }
|
411
|
+
end
|
412
|
+
|
413
|
+
context "with invalid value" do
|
414
|
+
let(:property_value) { 'this is not a date time' }
|
415
|
+
|
416
|
+
it { should be_an ArgumentError }
|
417
|
+
its(:message) { should match(/#{property_name}/i) }
|
418
|
+
its(:message) { should match(/#{property_value.inspect}/i) }
|
419
|
+
its(:message) { should match(/#{property_type}/i) }
|
420
|
+
end
|
421
|
+
end
|
422
|
+
|
392
423
|
context "for a Symbol type" do
|
393
424
|
let(:property_type) { Symbol }
|
394
425
|
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: modelish
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 25
|
5
|
+
prerelease:
|
5
6
|
segments:
|
6
7
|
- 0
|
7
8
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Maeve Revels
|
@@ -25,6 +26,7 @@ dependencies:
|
|
25
26
|
requirements:
|
26
27
|
- - ~>
|
27
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 15
|
28
30
|
segments:
|
29
31
|
- 1
|
30
32
|
- 0
|
@@ -39,6 +41,7 @@ dependencies:
|
|
39
41
|
requirements:
|
40
42
|
- - ~>
|
41
43
|
- !ruby/object:Gem::Version
|
44
|
+
hash: 9
|
42
45
|
segments:
|
43
46
|
- 2
|
44
47
|
- 5
|
@@ -53,6 +56,7 @@ dependencies:
|
|
53
56
|
requirements:
|
54
57
|
- - ~>
|
55
58
|
- !ruby/object:Gem::Version
|
59
|
+
hash: 7
|
56
60
|
segments:
|
57
61
|
- 0
|
58
62
|
- 6
|
@@ -67,6 +71,7 @@ dependencies:
|
|
67
71
|
requirements:
|
68
72
|
- - ~>
|
69
73
|
- !ruby/object:Gem::Version
|
74
|
+
hash: 25
|
70
75
|
segments:
|
71
76
|
- 2
|
72
77
|
- 0
|
@@ -82,6 +87,7 @@ dependencies:
|
|
82
87
|
requirements:
|
83
88
|
- - ">="
|
84
89
|
- !ruby/object:Gem::Version
|
90
|
+
hash: 3
|
85
91
|
segments:
|
86
92
|
- 0
|
87
93
|
version: "0"
|
@@ -100,6 +106,7 @@ files:
|
|
100
106
|
- .gitignore
|
101
107
|
- .rspec
|
102
108
|
- .rvmrc
|
109
|
+
- CHANGELOG.md
|
103
110
|
- Gemfile
|
104
111
|
- LICENSE
|
105
112
|
- README.md
|
@@ -132,6 +139,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
132
139
|
requirements:
|
133
140
|
- - ">="
|
134
141
|
- !ruby/object:Gem::Version
|
142
|
+
hash: 3
|
135
143
|
segments:
|
136
144
|
- 0
|
137
145
|
version: "0"
|
@@ -140,13 +148,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
148
|
requirements:
|
141
149
|
- - ">="
|
142
150
|
- !ruby/object:Gem::Version
|
151
|
+
hash: 3
|
143
152
|
segments:
|
144
153
|
- 0
|
145
154
|
version: "0"
|
146
155
|
requirements: []
|
147
156
|
|
148
157
|
rubyforge_project: modelish
|
149
|
-
rubygems_version: 1.
|
158
|
+
rubygems_version: 1.6.2
|
150
159
|
signing_key:
|
151
160
|
specification_version: 3
|
152
161
|
summary: A lightweight pseudo-modeling not-quite-framework
|