multiparameter_date_time 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,6 +1,7 @@
1
1
  *.gem
2
2
  *.rbc
3
3
  .bundle
4
+ .vimlog
4
5
  .config
5
6
  .rvmrc
6
7
  .yardoc
@@ -5,15 +5,7 @@ class IsValidMultiparameterDateTimeValidator < ActiveModel::EachValidator
5
5
 
6
6
  return if date_value.blank? && time_value.blank?
7
7
 
8
- if date_value.present?
9
- date_invalid = date_value !~ MultiparameterDateTime::VALID_DATE_FORMAT
10
- end
11
-
12
- if time_value.present?
13
- time_invalid = time_value !~ MultiparameterDateTime::VALID_TIME_FORMAT
14
- end
15
-
16
- if date_invalid || time_invalid
8
+ if date_invalid?(date_value) || time_invalid?(time_value)
17
9
  record.errors.add(attribute, self.class.invalid_format_error_message)
18
10
  elsif date_value.blank?
19
11
  record.errors.add(attribute, "Please enter a date.")
@@ -37,4 +29,18 @@ class IsValidMultiparameterDateTimeValidator < ActiveModel::EachValidator
37
29
 
38
30
  "Please enter a valid date and time using the following formats: #{date_string}, #{time_string}"
39
31
  end
32
+
33
+ def time_invalid?(time_value)
34
+ if time_value.present?
35
+ time_invalid_standard = time_value !~ MultiparameterDateTime::VALID_STANDARD_TIME_FORMAT
36
+ time_invalid_military = time_value !~ MultiparameterDateTime::VALID_MILITARY_TIME_FORMAT
37
+ time_invalid_standard && time_invalid_military
38
+ end
39
+ end
40
+
41
+ def date_invalid?(date_value)
42
+ if date_value.present?
43
+ date_invalid = date_value !~ MultiparameterDateTime::VALID_DATE_FORMAT
44
+ end
45
+ end
40
46
  end
@@ -1,3 +1,3 @@
1
1
  module MultiparameterDateTime
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -5,8 +5,9 @@ require 'is_valid_multiparameter_date_time_validator'
5
5
  module MultiparameterDateTime
6
6
  extend ActiveSupport::Concern
7
7
 
8
- VALID_TIME_FORMAT = /\A\d?\d:\d{2}(:\d{2})?\s*([ap]m)?\s*([A-Z]{3,5})?\Z/
9
8
  VALID_DATE_FORMAT = /\A((\d{1,2}[\/\-.]\d{1,2}[\/\-.]\d{2,4})|(\d{4}-\d{1,2}-\d{1,2}))/
9
+ VALID_STANDARD_TIME_FORMAT = /\A[0]*([1-9]|1[0-2]):\d{2}(:\d{2})?\s*([apAP][mM])?\s*([A-Z]{3,5})?\Z/
10
+ VALID_MILITARY_TIME_FORMAT = /\A[0]*([0-9]|1[0-9]|2[0-3]):\d{2}(:\d{2})?\s*([A-Z]{3,5})?\Z/
10
11
 
11
12
  DEFAULT_DATE_FORMAT = "%-m/%-d/%Y"
12
13
  DEFAULT_TIME_FORMAT = "%-I:%M %P"
@@ -93,7 +94,7 @@ module MultiparameterDateTime
93
94
  private
94
95
 
95
96
  def set_combined_datetime(name, date_string, time_string)
96
- if date_string =~ MultiparameterDateTime::VALID_DATE_FORMAT && time_string =~ MultiparameterDateTime::VALID_TIME_FORMAT
97
+ if date_string =~ MultiparameterDateTime::VALID_DATE_FORMAT && (time_string =~ MultiparameterDateTime::VALID_STANDARD_TIME_FORMAT || time_string =~ VALID_MILITARY_TIME_FORMAT)
97
98
  begin
98
99
  write_attribute_for_multiparameter_date_time(
99
100
  name, Time.zone.parse("#{date_string} #{time_string}")
@@ -22,6 +22,18 @@ describe IsValidMultiparameterDateTimeValidator do
22
22
  end
23
23
  end
24
24
 
25
+ shared_examples_for "a valid time" do
26
+ it "should not have an error" do
27
+ record.errors[:foo].should be_empty
28
+ end
29
+ end
30
+
31
+ shared_examples_for "a badly formatted date or time" do
32
+ it "should show the bad format error" do
33
+ record.errors[:foo].should == [bad_format_error]
34
+ end
35
+ end
36
+
25
37
  describe "#validate_each" do
26
38
  subject { record }
27
39
  let(:record) do
@@ -42,18 +54,72 @@ describe IsValidMultiparameterDateTimeValidator do
42
54
  context "with valid date" do
43
55
  let(:date_string) { "01/01/2001" }
44
56
 
45
- context "with valid time" do
46
- let(:time_string) { "12:31pm" }
47
- it "should not have an error" do
48
- record.errors[:foo].should be_empty
57
+ context "with valid time in" do
58
+ context "military format" do
59
+ context "lots of zeros" do
60
+ let(:time_string) { "00:00" }
61
+ it_should_behave_like "a valid time"
62
+ end
63
+
64
+ context "last valid value" do
65
+ let(:time_string){ "23:59" }
66
+ it_should_behave_like "a valid time"
67
+ end
68
+
69
+ context "1 pm" do
70
+ let(:time_string){ "13:00" }
71
+ it_should_behave_like "a valid time"
72
+ end
73
+ end
74
+
75
+ context "standard format" do
76
+ let(:time_string) { "12:31pm" }
77
+ it_should_behave_like "a valid time"
78
+
79
+ context "with a capital AM or PM" do
80
+ let(:time_string) { "12:31 PM" }
81
+ it_should_behave_like "a valid time"
82
+ end
83
+
84
+ context "without a space between the time and AM or PM" do
85
+ let(:time_string) { "12:31AM" }
86
+ it_should_behave_like "a valid time"
87
+ end
88
+
89
+ context "with no space and a mixed case aM or pM" do
90
+ let(:time_string) { "12:31aM" }
91
+ it_should_behave_like "a valid time"
92
+ end
93
+
94
+ context "with a space and a mixed case aM or pM" do
95
+ let(:time_string) { "12:31 aM" }
96
+ it_should_behave_like "a valid time"
97
+ end
98
+
99
+ context "with a space and a lower case am or pm" do
100
+ let(:time_string) { "12:31 am" }
101
+ it_should_behave_like "a valid time"
102
+ end
49
103
  end
50
104
  end
51
105
 
52
- context "with invalid time" do
53
- let(:time_string) { "asdf" }
106
+ context "with invalid time in" do
107
+ context "military format" do
108
+ context "above 23:59" do
109
+ let(:time_string) { "25:00" }
110
+ it_should_behave_like "a badly formatted date or time"
111
+ end
54
112
 
55
- it "should show the bad format error" do
56
- record.errors[:foo].should == [bad_format_error]
113
+ context "with am or pm" do
114
+ let(:time_string) { "23:00 am" }
115
+ it_should_behave_like "a badly formatted date or time"
116
+ end
117
+ end
118
+
119
+ context "standard format" do
120
+ let(:time_string) { "90:00pm" }
121
+
122
+ it_should_behave_like "a badly formatted date or time"
57
123
  end
58
124
  end
59
125
 
@@ -73,25 +139,19 @@ describe IsValidMultiparameterDateTimeValidator do
73
139
  context "with valid time" do
74
140
  let(:time_string) { "12:31pm" }
75
141
 
76
- it "should show the bad format error" do
77
- record.errors[:foo].should == [bad_format_error]
78
- end
142
+ it_should_behave_like "a badly formatted date or time"
79
143
  end
80
144
 
81
145
  context "with invalid time" do
82
146
  let(:time_string) { "asdf" }
83
147
 
84
- it "should show the bad format error" do
85
- record.errors[:foo].should == [bad_format_error]
86
- end
148
+ it_should_behave_like "a badly formatted date or time"
87
149
  end
88
150
 
89
151
  [" ", nil].each do |time_value|
90
152
  context "with time = #{time_value.inspect}" do
91
153
  let(:time_string) { time_value }
92
- it "should show the bad format error" do
93
- record.errors[:foo].should == [bad_format_error]
94
- end
154
+ it_should_behave_like "a badly formatted date or time"
95
155
  end
96
156
  end
97
157
  end
@@ -111,9 +171,7 @@ describe IsValidMultiparameterDateTimeValidator do
111
171
  context "with invalid time" do
112
172
  let(:time_string) { "asdf" }
113
173
 
114
- it "should show the bad format error" do
115
- record.errors[:foo].should == [bad_format_error]
116
- end
174
+ it_should_behave_like "a badly formatted date or time"
117
175
  end
118
176
 
119
177
  [" ", nil].each do |time_value|
@@ -154,9 +212,7 @@ describe IsValidMultiparameterDateTimeValidator do
154
212
  ModelWithDatetime.new(foo_date_part: "19/19/1919", foo_time_part: "04:50pm")
155
213
  end
156
214
 
157
- it "should show the bad format error" do
158
- record.errors[:foo].should == [bad_format_error]
159
- end
215
+ it_should_behave_like "a badly formatted date or time"
160
216
  end
161
217
 
162
218
  context "set directly" do
@@ -164,9 +220,7 @@ describe IsValidMultiparameterDateTimeValidator do
164
220
  ModelWithDatetime.new(foo: "19/19/1919 04:50pm")
165
221
  end
166
222
 
167
- it "should show the bad format error" do
168
- record.errors[:foo].should == [bad_format_error]
169
- end
223
+ it_should_behave_like "a badly formatted date or time"
170
224
  end
171
225
  end
172
226
 
@@ -176,9 +230,7 @@ describe IsValidMultiparameterDateTimeValidator do
176
230
  ModelWithDatetime.new(foo_date_part: "01/01/2001", foo_time_part: "09:99pm")
177
231
  end
178
232
 
179
- it "should show the bad format error" do
180
- record.errors[:foo].should == [bad_format_error]
181
- end
233
+ it_should_behave_like "a badly formatted date or time"
182
234
  end
183
235
 
184
236
  context "set directly" do
@@ -186,9 +238,7 @@ describe IsValidMultiparameterDateTimeValidator do
186
238
  ModelWithDatetime.new(foo: "01/01/2001 09:99pm")
187
239
  end
188
240
 
189
- it "should show the bad format error" do
190
- record.errors[:foo].should == [bad_format_error]
191
- end
241
+ it_should_behave_like "a badly formatted date or time"
192
242
  end
193
243
  end
194
244
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multiparameter_date_time
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2012-08-22 00:00:00.000000000 Z
15
+ date: 2013-10-03 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: american_date
@@ -152,10 +152,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
152
  version: '0'
153
153
  segments:
154
154
  - 0
155
- hash: 4193815623193875602
155
+ hash: -4212657700101252110
156
156
  requirements: []
157
157
  rubyforge_project:
158
- rubygems_version: 1.8.24
158
+ rubygems_version: 1.8.25
159
159
  signing_key:
160
160
  specification_version: 3
161
161
  summary: Set a DateTime via two accessors, one for the date, one for the time