multiparameter_date_time 0.0.1 → 0.0.2

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/README.md CHANGED
@@ -52,6 +52,21 @@ record.publish_at_date_part #=> nil
52
52
  record.publish_at_time_part #=> "09:30 am"
53
53
  ````
54
54
 
55
+ ### Configuring the date and time formats
56
+ In config/initializers/multiparameter\_date\_time.rb:
57
+
58
+ ````ruby
59
+ MultiparameterDateTime.date_format = "%-m/%-d/%Y"
60
+ MultiparameterDateTime.time_format = "%-I:%M %P"
61
+ ````
62
+
63
+ ### Validating the multipart date time data
64
+
65
+ ````ruby
66
+ validates :published_at, presence: true, is_valid_multiparameter_date_time: true
67
+ ````
68
+
69
+
55
70
  ## Contributing
56
71
 
57
72
  1. Fork it
@@ -0,0 +1,42 @@
1
+ class IsValidMultiparameterDateTimeValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ date_value = record.public_send(:"#{attribute}_date_part")
4
+ time_value = record.public_send(:"#{attribute}_time_part")
5
+
6
+ return if date_value.blank? && time_value.blank?
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
17
+ record.errors.add(attribute, invalid_format_error_message)
18
+ elsif date_value.blank?
19
+ record.errors.add(attribute, "Please enter a date.")
20
+ elsif time_value.blank?
21
+ record.errors.add(attribute, "Please enter a time.")
22
+ else
23
+ attribute_value = record.public_send(:"#{attribute}_time_part")
24
+ begin
25
+ Time.zone.parse("#{date_value} #{time_value}")
26
+ Time.zone.parse(attribute_value)
27
+ rescue ArgumentError
28
+ record.errors.add(attribute, invalid_format_error_message)
29
+ end
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def invalid_format_error_message
36
+ date_time = Time.zone.parse("1/29/2000 5:15pm")
37
+ date_string = date_time.strftime(MultiparameterDateTime.date_format)
38
+ time_string = date_time.strftime(MultiparameterDateTime.time_format)
39
+
40
+ "Please enter a valid date and time using the following formats: #{date_string}, #{time_string}"
41
+ end
42
+ end
@@ -1,5 +1,6 @@
1
1
  require "active_support/concern"
2
2
  require "american_date"
3
+ require 'is_valid_multiparameter_date_time_validator'
3
4
 
4
5
  module MultiparameterDateTime
5
6
  extend ActiveSupport::Concern
@@ -7,6 +8,19 @@ module MultiparameterDateTime
7
8
  VALID_TIME_FORMAT = /\A\d?\d:\d{2}(:\d{2})?\s*([ap]m)?\s*([A-Z]{3,5})?\Z/
8
9
  VALID_DATE_FORMAT = /\A\d?\d\/\d?\d\/\d{4}|(\d{4}-\d{2}-\d{2})\Z/
9
10
 
11
+ DEFAULT_DATE_FORMAT = "%-m/%-d/%Y"
12
+ DEFAULT_TIME_FORMAT = "%-I:%M %P"
13
+
14
+ mattr_writer :date_format, :time_format
15
+
16
+ def self.date_format
17
+ @@date_format ||= DEFAULT_DATE_FORMAT
18
+ end
19
+
20
+ def self.time_format
21
+ @@time_format ||= DEFAULT_TIME_FORMAT
22
+ end
23
+
10
24
  module ClassMethods
11
25
  def multiparameter_date_time(attribute_name)
12
26
  date_attribute = :"#{attribute_name}_date_part"
@@ -60,7 +74,7 @@ module MultiparameterDateTime
60
74
  time = public_send(attribute_name)
61
75
  return nil if time.nil? || time == :incomplete
62
76
 
63
- time.strftime("%-I:%M %P")
77
+ time.strftime(MultiparameterDateTime.time_format)
64
78
  end
65
79
  end
66
80
 
@@ -70,7 +84,7 @@ module MultiparameterDateTime
70
84
  else
71
85
  date = public_send(attribute_name)
72
86
  return nil if date.nil? || date == :incomplete
73
- date.strftime("%-m/%-d/%Y")
87
+ date.strftime(MultiparameterDateTime.date_format)
74
88
  end
75
89
  end
76
90
  end
@@ -1,3 +1,3 @@
1
1
  module MultiparameterDateTime
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,237 @@
1
+ require 'spec_helper'
2
+
3
+ require 'multiparameter_date_time'
4
+ require 'informal'
5
+ require 'active_support/core_ext/time/zones'
6
+ require 'is_valid_multiparameter_date_time_validator'
7
+
8
+ describe IsValidMultiparameterDateTimeValidator do
9
+ before do
10
+ Time.zone = "US/Eastern"
11
+ end
12
+
13
+ with_model :ModelWithDatetime do
14
+ table do |t|
15
+ t.datetime :foo
16
+ end
17
+
18
+ model do
19
+ include MultiparameterDateTime
20
+ multiparameter_date_time :foo
21
+ validates :foo, is_valid_multiparameter_date_time: true, allow_blank: true
22
+ end
23
+ end
24
+
25
+ describe "#validate_each" do
26
+ subject { record }
27
+ let(:record) do
28
+ ModelWithDatetime.new(
29
+ foo_date_part: date_string,
30
+ foo_time_part: time_string
31
+ )
32
+ end
33
+
34
+ let(:bad_format_error) do
35
+ "Please enter a valid date and time using the following formats: 1/29/2000, 5:15 pm"
36
+ end
37
+ let(:missing_time_error) { "Please enter a time." }
38
+ let(:missing_date_error) { "Please enter a date." }
39
+
40
+ before { record.valid? }
41
+
42
+ context "with valid date" do
43
+ let(:date_string) { "01/01/2001" }
44
+
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
49
+ end
50
+ end
51
+
52
+ context "with invalid time" do
53
+ let(:time_string) { "asdf" }
54
+
55
+ it "should show the bad format error" do
56
+ record.errors[:foo].should == [bad_format_error]
57
+ end
58
+ end
59
+
60
+ [" ", nil].each do |time_value|
61
+ context "with time = #{time_value.inspect}" do
62
+ let(:time_string) { time_value }
63
+ it "should show the missing time error" do
64
+ record.errors[:foo].should == [missing_time_error]
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ context "with invalid date" do
71
+ let(:date_string) { "asdf" }
72
+
73
+ context "with valid time" do
74
+ let(:time_string) { "12:31pm" }
75
+
76
+ it "should show the bad format error" do
77
+ record.errors[:foo].should == [bad_format_error]
78
+ end
79
+ end
80
+
81
+ context "with invalid time" do
82
+ let(:time_string) { "asdf" }
83
+
84
+ it "should show the bad format error" do
85
+ record.errors[:foo].should == [bad_format_error]
86
+ end
87
+ end
88
+
89
+ [" ", nil].each do |time_value|
90
+ context "with time = #{time_value.inspect}" do
91
+ let(:time_string) { time_value }
92
+ it "should show the bad format error" do
93
+ record.errors[:foo].should == [bad_format_error]
94
+ end
95
+ end
96
+ end
97
+ end
98
+
99
+ [" ", nil].each do |date_value|
100
+ context "with date = #{date_value.inspect}" do
101
+ let(:date_string) { date_value }
102
+
103
+ context "with valid time" do
104
+ let(:time_string) { "12:31pm" }
105
+
106
+ it "should show the missing date error" do
107
+ record.errors[:foo].should == [missing_date_error]
108
+ end
109
+ end
110
+
111
+ context "with invalid time" do
112
+ let(:time_string) { "asdf" }
113
+
114
+ it "should show the bad format error" do
115
+ record.errors[:foo].should == [bad_format_error]
116
+ end
117
+ end
118
+
119
+ [" ", nil].each do |time_value|
120
+ context "with time = #{time_value.inspect}" do
121
+ let(:time_string) { time_value }
122
+ it "should not have an error" do
123
+ record.errors[:foo].should be_empty
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
129
+
130
+ context "when the datetime is set directly" do
131
+ let(:record) { ModelWithDatetime.new(foo: Time.current) }
132
+ it "should not have an error" do
133
+ record.errors[:foo].should be_empty
134
+ end
135
+ end
136
+
137
+ context "when the datetime is set directly to nil" do
138
+ let(:record) { ModelWithDatetime.new(foo: nil) }
139
+ it "should not have an error" do
140
+ record.errors[:foo].should be_empty
141
+ end
142
+ end
143
+
144
+ context "when nothing is set at all" do
145
+ let(:record) { ModelWithDatetime.new }
146
+ it "should not have an error" do
147
+ record.errors[:foo].should be_empty
148
+ end
149
+ end
150
+
151
+ context "with an impossible date" do
152
+ context "set in parts" do
153
+ let(:record) do
154
+ ModelWithDatetime.new(foo_date_part: "19/19/1919", foo_time_part: "04:50pm")
155
+ end
156
+
157
+ it "should show the bad format error" do
158
+ record.errors[:foo].should == [bad_format_error]
159
+ end
160
+ end
161
+
162
+ context "set directly" do
163
+ let(:record) do
164
+ ModelWithDatetime.new(foo: "19/19/1919 04:50pm")
165
+ end
166
+
167
+ it "should show the bad format error" do
168
+ record.errors[:foo].should == [bad_format_error]
169
+ end
170
+ end
171
+ end
172
+
173
+ context "with an impossible time" do
174
+ context "set in parts" do
175
+ let(:record) do
176
+ ModelWithDatetime.new(foo_date_part: "01/01/2001", foo_time_part: "09:99pm")
177
+ end
178
+
179
+ it "should show the bad format error" do
180
+ record.errors[:foo].should == [bad_format_error]
181
+ end
182
+ end
183
+
184
+ context "set directly" do
185
+ let(:record) do
186
+ ModelWithDatetime.new(foo: "01/01/2001 09:99pm")
187
+ end
188
+
189
+ it "should show the bad format error" do
190
+ record.errors[:foo].should == [bad_format_error]
191
+ end
192
+ end
193
+ end
194
+
195
+ context "when the display format has been configured" do
196
+ let(:date_string) { "asdf" }
197
+ let(:time_string) { "foo" }
198
+
199
+ context "when the date format is set" do
200
+ before do
201
+ MultiparameterDateTime.date_format = "%-m-%-e-%y"
202
+ record.valid?
203
+ end
204
+
205
+ it "should show the bad format error" do
206
+ record.errors[:foo].should == [
207
+ "Please enter a valid date and time using the following formats: 1-29-00, 5:15 pm"
208
+ ]
209
+ end
210
+
211
+ after do
212
+ MultiparameterDateTime.date_format = MultiparameterDateTime::DEFAULT_DATE_FORMAT
213
+ end
214
+ end
215
+
216
+ context "when the time format is set" do
217
+ let(:time_string) { "asdf" }
218
+
219
+ before do
220
+ MultiparameterDateTime.time_format = "%H%M hours"
221
+ record.valid?
222
+ end
223
+
224
+ it "should show the bad format error" do
225
+ record.errors[:foo].should == [
226
+ "Please enter a valid date and time using the following formats: 1/29/2000, 1715 hours"
227
+ ]
228
+ end
229
+
230
+ after do
231
+ MultiparameterDateTime.time_format = MultiparameterDateTime::DEFAULT_TIME_FORMAT
232
+ end
233
+ end
234
+ end
235
+ end
236
+
237
+ end
@@ -344,6 +344,47 @@ describe MultiparameterDateTime do
344
344
  subject.foo_time_part.should == "12:00 am"
345
345
  end
346
346
  end
347
+
348
+ describe "configuring the datetime format" do
349
+ let(:record) { model.new(foo: Time.zone.parse("01/09/2000 1:30 pm")) }
350
+
351
+ context "when the date format is set" do
352
+ before do
353
+ MultiparameterDateTime.date_format = "%-m-%-e-%y"
354
+ end
355
+
356
+ it "should format the date properly" do
357
+ subject.foo_date_part.should == "1-9-00"
358
+ end
359
+
360
+ it "should use the default format for the time" do
361
+ subject.foo_time_part.should == "1:30 pm"
362
+ end
363
+
364
+ after do
365
+ MultiparameterDateTime.date_format = MultiparameterDateTime::DEFAULT_DATE_FORMAT
366
+ end
367
+ end
368
+
369
+ context "when the time format is set" do
370
+ before do
371
+ MultiparameterDateTime.time_format = "%H%M hours"
372
+ end
373
+
374
+ it "should format the time properly" do
375
+ subject.foo_time_part.should == "1330 hours"
376
+ end
377
+
378
+ it "should use the default format for the date" do
379
+ subject.foo_date_part.should == "1/9/2000"
380
+ end
381
+
382
+ after do
383
+ MultiparameterDateTime.time_format = MultiparameterDateTime::DEFAULT_TIME_FORMAT
384
+ end
385
+ end
386
+
387
+ end
347
388
  end
348
389
  end
349
390
  end
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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-08-09 00:00:00.000000000 Z
14
+ date: 2012-08-10 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: american_date
@@ -123,9 +123,11 @@ files:
123
123
  - LICENSE
124
124
  - README.md
125
125
  - Rakefile
126
+ - lib/is_valid_multiparameter_date_time_validator.rb
126
127
  - lib/multiparameter_date_time.rb
127
128
  - lib/multiparameter_date_time/version.rb
128
129
  - multiparameter_date_time.gemspec
130
+ - spec/is_valid_multiparameter_date_time_validator_spec.rb
129
131
  - spec/multiparameter_date_time_spec.rb
130
132
  - spec/spec_helper.rb
131
133
  homepage: https://github.com/Casecommons/multiparameter_date_time
@@ -142,7 +144,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
142
144
  version: '0'
143
145
  segments:
144
146
  - 0
145
- hash: -1009483105681798666
147
+ hash: 340950943914695248
146
148
  required_rubygems_version: !ruby/object:Gem::Requirement
147
149
  none: false
148
150
  requirements:
@@ -151,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
153
  version: '0'
152
154
  segments:
153
155
  - 0
154
- hash: -1009483105681798666
156
+ hash: 340950943914695248
155
157
  requirements: []
156
158
  rubyforge_project:
157
159
  rubygems_version: 1.8.24
@@ -159,5 +161,6 @@ signing_key:
159
161
  specification_version: 3
160
162
  summary: Set a DateTime via two accessors, one for the date, one for the time
161
163
  test_files:
164
+ - spec/is_valid_multiparameter_date_time_validator_spec.rb
162
165
  - spec/multiparameter_date_time_spec.rb
163
166
  - spec/spec_helper.rb