datetime_helper 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OTU5ZmUzODhlYmM0ZThmNjI0NGUxMTQ2YmIzZmZkZDY1NWQyZWViOA==
4
+ MDFmZGQ5MjZiYmFiNWYzYWMxMjM1YTBjYmM1Y2VhMTJmZDY4MGExYg==
5
5
  data.tar.gz: !binary |-
6
- YTM0ZWE3MmEyZjljOTg2YzY4NTE0ODY1NWI3MTE5MWM5OWQyYWExNQ==
6
+ MGM5MTFhOWMzYjdhYTVjMGVkZjcxNDdiN2E0ODY4MjA2MTc2MDg4Zg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YjQ0NWIzNGE3ODk5MzA5ZTQ5NmE1M2I0NTJmNWE2YTM3OGVlOWMxNzU1ZDM1
10
- MDc2YzQzZmI2YTdiNGQ2NzNjNWJiOGVjMTk4Mzg2MGMxNzM2YWJkZjFhZmY0
11
- ZGMzMDk5NGViNmUyMmQzYzI5OWRlNTY0MmQ3Y2E0MzEyYWRjZjM=
9
+ NjczNzkyNDhmMTUwYzAyYTY4ZDdjNTg4MTkxZGRiZWZjOWM3NTIxYWMyM2Q0
10
+ Y2NiZjMzYzEyODVkYmRjNzYzOWExZDM3ZjNlMGVkMTdkOTQ3ZTkxMzM0YjZl
11
+ ZmQxY2QzYjgzMTQ0ZjNjODM4OTgwODJhZTg2NGM2NWFmZmU0ZTA=
12
12
  data.tar.gz: !binary |-
13
- ZjFhOTZjMzU5ZTA0N2Q2MGYzZmY3NTRmZmIxOTA1NTU0YjAwOTI2NzYxMjA4
14
- ZGZkZmE1ZGMxYjllNTFlNzNhOWY3MzAwYzZiMDM4YWY1NzU2ODRhZGRiMWQ3
15
- OWZkN2JjNTFmYWEwNmQzMjJiYmIwNGU2MzgxOGM5ODczZGY3ODE=
13
+ YWMwMzNhY2M1OTdmOTI1OTU3MjcwODA1MmFiYjkxY2RkZGEyYzJlMmVjYzNl
14
+ OTFkNTAwYTdmNTAxOTc0MmI2Mzk4ZmZjNmMwM2RjZGY0MjI0ODUzYWZmNThl
15
+ MDFkZmMwZmQyZTEyMDM2MmRlNWQ4ZmM2N2QxYjJlMzRjZTI0OGM=
data/README.md CHANGED
@@ -5,9 +5,9 @@ A collection of useful utilities for projects that have to deal with dates, time
5
5
  ## Features
6
6
 
7
7
  1. A base method called `is_zulu_time?`,
8
- 2. An `rspec` matcher called `be_zulu_time`,
8
+ 2. `rspec` matchers `be_zulu_time`, `be_an_iso_formatted_date`, and `be_an_iso_formatted_time`
9
9
  3. An `ActiveModel` validator called `zulu_time`, and
10
- 4. An `ActiveModel::Serializer` helper method called `in_zulu_time`.
10
+ 4. An `ActiveModel::Serializer` helper method called `enforce_zulu_time`.
11
11
 
12
12
  Each feature can be required individually so you can use the `rspec` matcher, `ActiveModel` validator, or `ActiveModel::Serializer` helper in isolation.
13
13
 
@@ -18,7 +18,7 @@ Each feature can be required individually so you can use the `rspec` matcher, `A
18
18
  1. `Ruby`, `Bundler`, etc. The usual suspects. (Tested against Ruby 2.0.0 and up)
19
19
  2. `rspec` if you `require 'datetime_helper/rspec'`
20
20
  3. `active_model` if you `require 'datetime_helper/active_model'`
21
- 4. `active_model_serializers` if you `require 'datetime_helper/active_model_serialiser'`
21
+ 4. `active_model_serializer` if you `require 'datetime_helper/active_model_serialiser'`
22
22
 
23
23
  ## TL;DR
24
24
 
@@ -36,10 +36,13 @@ Put this in your `Gemfile`
36
36
  gem 'datetime_helper'
37
37
  ```
38
38
 
39
- ### Testing a string to see if it is a Zulu Time formatted string
39
+ ### Basic Zulu Time checking
40
+
41
+ You can also use this to test that a `DateTime`, or `Time`, are at `UTC+0`, or
42
+ that a `String` is formatted in correct Zulu Time format.
40
43
 
41
44
  ```ruby
42
- DatetimeHelper.is_zulu_time? "some_string"
45
+ DatetimeHelper.is_zulu_time? something
43
46
  ```
44
47
 
45
48
  ### Using the `be_zulu_time` matcher in your `RSpec` tests
@@ -60,6 +63,15 @@ And put this in your `rspec` tests.
60
63
  it {expect(subject[:deleted_at]).to be_zulu_time}
61
64
  ```
62
65
 
66
+ This can be used to expect that a `DateTime`, or `Time`, are at `UTC+0`, or that a `String` is formatted in Zulu Time.
67
+
68
+ #### ISO Times and Dates
69
+
70
+ Similarly to the above you can also test Time and Date strings with
71
+
72
+ * `be_an_iso_formatted_time`, and
73
+ * `be_an_iso_formatted_date`
74
+
63
75
  ### Validating `ActiveModel` fields to ensure they hold UTC+0 `datetime` data
64
76
 
65
77
  First be sure you `require 'datetime_helper/active_model'`
@@ -72,18 +84,20 @@ include DatetimeHelper::Validators
72
84
  validates :updated_at, zulu_time: true
73
85
  ```
74
86
 
75
- This will verify that a `Time` is supplied at `UTC+0`, or that a `DateTime` has `.zone == "+00:00"`.
87
+ This will verify that a `Time` is supplied at `UTC+0`,
88
+ or that a `DateTime` has `.zone == "+00:00"`,
89
+ or that a `String` is in Zulu Time format.
76
90
 
77
91
  ### Enforcing `ActiveModel::Serializer` Zulu Time string formats
78
92
 
79
- First be sure you `require 'datetime_helper/active_model_serialisers'`
93
+ First be sure you `require 'datetime_helper/active_model_serialiser'`
80
94
 
81
95
  Then you can put this in your serialisers:
82
96
 
83
97
  ```ruby
84
98
  extend DatetimeHelper::Serialisers
85
99
 
86
- in_zulu_time :updated_at
100
+ enforce_zulu_time :updated_at
87
101
  ```
88
102
 
89
103
  or if you have a bunch of 'em
@@ -91,7 +105,7 @@ or if you have a bunch of 'em
91
105
  ```ruby
92
106
  extend DatetimeHelper::Serialisers
93
107
 
94
- %w(updated_at deleted_at).each { |attribute| in_zulu_time attribute }
108
+ %w(updated_at deleted_at).each { |attribute| enforce_zulu_time attribute }
95
109
  ```
96
110
 
97
111
  This will ensure that the serialised output is a proper Zulu Time formatted string.
@@ -128,3 +142,4 @@ The `Datetime Helper` is © 2015 Westfield Labs and is available for use under t
128
142
  |`0.0.2`| Added the `ActiveModel` validator |
129
143
  |`0.0.3`| Added the `ActiveModel::Serializer` helper |
130
144
  |`1.0.0`| Cleaned up for first official release |
145
+ |`1.0.1`| Enhanced matchers, and validator |
@@ -2,10 +2,27 @@ require "datetime_helper/version"
2
2
 
3
3
  module DatetimeHelper
4
4
 
5
- ZULU_TIME_PATTERN = /^\d{4}-[0-1][0-9]-[0-3]\d{1}T[0-2]\d{1}:[0-5]\d{1}:[0-5]\d{1}Z$/
5
+ ZULU_TIME_PATTERN = /^(\d{4})-([0-1][0-9])-([0-3]\d{1})T([0-2]\d{1}):([0-5]\d{1}):([0-5]\d{1})(\.[0-9]{1,3})?Z$/
6
6
 
7
- def self.is_zulu_time?(time_string)
8
- !(time_string =~ ZULU_TIME_PATTERN).nil?
9
- end
7
+ class << self
8
+ def is_zulu_time?(something)
9
+ return is_zulu_time_string?(something) if something.is_a? String
10
+ return is_zulu_time_datetime?(something) if something.is_a? DateTime
11
+ return is_zulu_time_time?(something) if something.is_a? Time
12
+ false
13
+ end
14
+
15
+ def is_zulu_time_string?(time_string)
16
+ !(time_string =~ ZULU_TIME_PATTERN).nil?
17
+ end
18
+
19
+ def is_zulu_time_datetime?(datetime)
20
+ datetime.zone == "+00:00"
21
+ end
10
22
 
23
+ def is_zulu_time_time?(time)
24
+ time.utc?
25
+ end
26
+
27
+ end
11
28
  end
@@ -0,0 +1,17 @@
1
+ module DatetimeHelper
2
+ module Matchers
3
+
4
+ RSpec::Matchers.define :be_an_iso_formatted_time do |expected|
5
+ match do |time|
6
+ !(time =~ /\d{2}:\d{2}/).nil?
7
+ end
8
+ end
9
+
10
+ RSpec::Matchers.define :be_an_iso_formatted_date do |expected|
11
+ match do |date|
12
+ !(date =~ /\d{4}\-\d{2}\-\d{2}/).nil?
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -1,2 +1,3 @@
1
1
  require_relative "../datetime_helper"
2
2
  require_relative "matchers/zulu_time_rspec_matcher"
3
+ require_relative "matchers/iso_date_and_time_rspec_matchers"
@@ -8,22 +8,10 @@ module DatetimeHelper
8
8
  class ZuluTimeValidator < ActiveModel::EachValidator
9
9
 
10
10
  def validate_each(record, attribute, value)
11
- return if value.is_a? DateTime and utc_datetime? value
12
- return if value.is_a? Time and utc_time? value
13
-
11
+ return if DatetimeHelper.is_zulu_time? value
14
12
  record.errors[attribute] << (options[:message] || 'is not in UTC+0 (Zulu Time)')
15
13
  end
16
14
 
17
- private
18
-
19
- def utc_datetime?(value)
20
- value.zone == "+00:00"
21
- end
22
-
23
- def utc_time?(value)
24
- value.utc?
25
- end
26
-
27
15
  end
28
16
  end
29
17
  end
@@ -1,3 +1,3 @@
1
1
  module DatetimeHelper
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -0,0 +1,27 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe 'iso_formatted' do
4
+ describe 'be_an_iso_formatted_time' do
5
+ context 'given a valid time string' do
6
+ let(:valid_time_string) { "11:00" }
7
+ it { expect(valid_time_string).to be_an_iso_formatted_time }
8
+ end
9
+
10
+ context 'given an invalid time string' do
11
+ let(:invalid_time_string) { "This is not a time string" }
12
+ it { expect(invalid_time_string).to_not be_an_iso_formatted_time }
13
+ end
14
+ end
15
+
16
+ describe 'be_an_iso_formatted_date' do
17
+ context 'given a valid date string' do
18
+ let(:valid_time_string) { "2015-11-30" }
19
+ it { expect(valid_time_string).to be_an_iso_formatted_date }
20
+ end
21
+
22
+ context 'given an invalid date string' do
23
+ let(:invalid_time_string) { "This is not a date string" }
24
+ it { expect(invalid_time_string).to_not be_an_iso_formatted_date }
25
+ end
26
+ end
27
+ end
@@ -10,4 +10,24 @@ describe 'be_zulu_time' do
10
10
  let(:invalid_time_string) { "This is not a zulu time string" }
11
11
  it { expect(invalid_time_string).to_not be_zulu_time }
12
12
  end
13
+
14
+ context 'given a datetime that is at UTC0' do
15
+ let(:valid_datetime) { DateTime.now.new_offset(0) }
16
+ it { expect(valid_datetime).to be_zulu_time }
17
+ end
18
+
19
+ context 'given a datetime that is not at UTC0' do
20
+ let(:invalid_datetime) { DateTime.now.new_offset("+05:30") }
21
+ it { expect(invalid_datetime).to_not be_zulu_time }
22
+ end
23
+
24
+ context 'given a time that is at UTC0' do
25
+ let(:valid_time) { Time.now.utc }
26
+ it { expect(valid_time).to be_zulu_time }
27
+ end
28
+
29
+ context 'given a time that is not at UTC0' do
30
+ let(:invalid_time) { Time.now.getlocal("+05:30") }
31
+ it { expect(invalid_time).to_not be_zulu_time }
32
+ end
13
33
  end
data/spec/spec_helper.rb CHANGED
@@ -16,11 +16,6 @@ RSpec.configure do |config|
16
16
  # --seed 1234
17
17
  config.order = 'random'
18
18
 
19
- # Hi I'm Dave I like pretty colours.
20
- config.color = true
21
- config.tty = true
22
- config.formatter = :documentation
23
-
24
19
  config.include DatetimeHelper::Matchers
25
20
 
26
21
  end
@@ -5,11 +5,6 @@ describe DatetimeHelper::Validators::ZuluTimeValidator do
5
5
  let(:validator) { described_class.new({attributes: [:updated_at]})}
6
6
  let(:model) { double('model') }
7
7
 
8
- # if, by chance, you happen to be running these tests
9
- # on a machine set to UTC+0 then the local time tests
10
- # won't really be a valid test.
11
- let(:already_in_utc) { Time.now.utc == Time.now.getlocal }
12
-
13
8
  before :each do
14
9
  allow(model).to receive_message_chain('errors').and_return([])
15
10
  allow(model.errors).to receive_message_chain('[]').and_return({})
@@ -24,10 +19,9 @@ describe DatetimeHelper::Validators::ZuluTimeValidator do
24
19
  end
25
20
  end
26
21
 
27
- context "given a local Time" do
28
- let(:invalid_time) { Time.now.getlocal }
22
+ context "given a non UTC+0 Time" do
23
+ let(:invalid_time) { Time.now.getlocal("+05:30") }
29
24
  it "is not accepted as valid" do
30
- pending "Local Time is UTC so skipping test" if already_in_utc
31
25
  expect(model.errors[]).to receive('<<')
32
26
  validator.validate_each(model, 'updated_at', invalid_time)
33
27
  end
@@ -42,14 +36,21 @@ describe DatetimeHelper::Validators::ZuluTimeValidator do
42
36
  end
43
37
 
44
38
  context "given a local DateTime" do
45
- let(:invalid_time) { DateTime.now }
39
+ let(:invalid_time) { DateTime.now.new_offset("+05:30") }
46
40
  it "is not accepted as valid" do
47
- pending "Local Time is UTC so skipping test" if already_in_utc
48
41
  expect(model.errors[]).to receive('<<')
49
42
  validator.validate_each(model, 'updated_at', invalid_time)
50
43
  end
51
44
  end
52
45
 
46
+ context "given a zulu time string" do
47
+ let(:valid_time) { Time.now.utc.iso8601 }
48
+ it "is accepted as valid" do
49
+ expect(model.errors[]).to_not receive('<<')
50
+ validator.validate_each(model, 'updated_at', valid_time)
51
+ end
52
+ end
53
+
53
54
  context "given a String instead of a Time or DateTime" do
54
55
  let(:nonsense) { "this makes no sense" }
55
56
  it "is not accepted as valid" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datetime_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - davesag
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-02 00:00:00.000000000 Z
11
+ date: 2015-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -114,12 +114,14 @@ files:
114
114
  - lib/datetime_helper.rb
115
115
  - lib/datetime_helper/active_model.rb
116
116
  - lib/datetime_helper/active_model_serialiser.rb
117
+ - lib/datetime_helper/matchers/iso_date_and_time_rspec_matchers.rb
117
118
  - lib/datetime_helper/matchers/zulu_time_rspec_matcher.rb
118
119
  - lib/datetime_helper/rspec.rb
119
120
  - lib/datetime_helper/serialisers/zulu_time_active_model_serialiser_helper.rb
120
121
  - lib/datetime_helper/validators/zulu_time_active_model_validator.rb
121
122
  - lib/datetime_helper/version.rb
122
123
  - spec/datetime_helper_spec.rb
124
+ - spec/matchers/iso_date_and_time_rspec_matcher_spec.rb
123
125
  - spec/matchers/zulu_time_rspec_matcher_spec.rb
124
126
  - spec/serialisers/zulu_time_active_model_serialiser_helper_spec.rb
125
127
  - spec/spec_helper.rb
@@ -151,6 +153,7 @@ summary: A collection of useful helpers for projects which have to deal with dat
151
153
  times, and time zones.
152
154
  test_files:
153
155
  - spec/datetime_helper_spec.rb
156
+ - spec/matchers/iso_date_and_time_rspec_matcher_spec.rb
154
157
  - spec/matchers/zulu_time_rspec_matcher_spec.rb
155
158
  - spec/serialisers/zulu_time_active_model_serialiser_helper_spec.rb
156
159
  - spec/spec_helper.rb