datetime_helper 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +43 -0
- data/.travis.yml +19 -0
- data/Gemfile +4 -0
- data/LICENSE +13 -0
- data/README.md +130 -0
- data/Rakefile +12 -0
- data/contributing.md +15 -0
- data/datetime_helper.gemspec +29 -0
- data/lib/datetime_helper/active_model.rb +2 -0
- data/lib/datetime_helper/active_model_serialiser.rb +3 -0
- data/lib/datetime_helper/matchers/zulu_time_rspec_matcher.rb +8 -0
- data/lib/datetime_helper/rspec.rb +2 -0
- data/lib/datetime_helper/serialisers/zulu_time_active_model_serialiser_helper.rb +13 -0
- data/lib/datetime_helper/validators/zulu_time_active_model_validator.rb +29 -0
- data/lib/datetime_helper/version.rb +3 -0
- data/lib/datetime_helper.rb +11 -0
- data/spec/datetime_helper_spec.rb +23 -0
- data/spec/matchers/zulu_time_rspec_matcher_spec.rb +13 -0
- data/spec/serialisers/zulu_time_active_model_serialiser_helper_spec.rb +44 -0
- data/spec/spec_helper.rb +26 -0
- data/spec/validators/zulu_time_active_model_validator_spec.rb +60 -0
- metadata +157 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
OTU5ZmUzODhlYmM0ZThmNjI0NGUxMTQ2YmIzZmZkZDY1NWQyZWViOA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
YTM0ZWE3MmEyZjljOTg2YzY4NTE0ODY1NWI3MTE5MWM5OWQyYWExNQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YjQ0NWIzNGE3ODk5MzA5ZTQ5NmE1M2I0NTJmNWE2YTM3OGVlOWMxNzU1ZDM1
|
10
|
+
MDc2YzQzZmI2YTdiNGQ2NzNjNWJiOGVjMTk4Mzg2MGMxNzM2YWJkZjFhZmY0
|
11
|
+
ZGMzMDk5NGViNmUyMmQzYzI5OWRlNTY0MmQ3Y2E0MzEyYWRjZjM=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZjFhOTZjMzU5ZTA0N2Q2MGYzZmY3NTRmZmIxOTA1NTU0YjAwOTI2NzYxMjA4
|
14
|
+
ZGZkZmE1ZGMxYjllNTFlNzNhOWY3MzAwYzZiMDM4YWY1NzU2ODRhZGRiMWQ3
|
15
|
+
OWZkN2JjNTFmYWEwNmQzMjJiYmIwNGU2MzgxOGM5ODczZGY3ODE=
|
data/.gitignore
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
*.bundle
|
4
|
+
*.so
|
5
|
+
*.o
|
6
|
+
*.a
|
7
|
+
mkmf.log
|
8
|
+
/.config
|
9
|
+
/coverage/
|
10
|
+
/InstalledFiles
|
11
|
+
/pkg/
|
12
|
+
/spec/reports/
|
13
|
+
/test/tmp/
|
14
|
+
/test/version_tmp/
|
15
|
+
/tmp/
|
16
|
+
|
17
|
+
## Specific to RubyMotion:
|
18
|
+
.dat*
|
19
|
+
.repl_history
|
20
|
+
build/
|
21
|
+
|
22
|
+
## Documentation cache and generated files:
|
23
|
+
/.yardoc/
|
24
|
+
/_yardoc/
|
25
|
+
/doc/
|
26
|
+
/rdoc/
|
27
|
+
|
28
|
+
## Environment normalisation:
|
29
|
+
/.bundle/
|
30
|
+
/vendor/bundle
|
31
|
+
/lib/bundler/man/
|
32
|
+
|
33
|
+
# for a library or gem, you might want to ignore these files since the code is
|
34
|
+
# intended to run in multiple environments; otherwise, check them in:
|
35
|
+
Gemfile.lock
|
36
|
+
.ruby-version
|
37
|
+
.ruby-gemset
|
38
|
+
|
39
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
40
|
+
.rvmrc
|
41
|
+
|
42
|
+
# We develop on Macs
|
43
|
+
.DS_Store
|
data/.travis.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 2.0.0-p598
|
4
|
+
- 2.1.0
|
5
|
+
- 2.1.1
|
6
|
+
- 2.1.2
|
7
|
+
- 2.1.4
|
8
|
+
- 2.2.0
|
9
|
+
- 2.2.1
|
10
|
+
- 2.2.2
|
11
|
+
before_install:
|
12
|
+
- gem install bundler
|
13
|
+
- export TZ=Australia/Canberra
|
14
|
+
deploy:
|
15
|
+
provider: rubygems
|
16
|
+
api_key:
|
17
|
+
secure: FZVmAZ6QMTUCZU4Iogf3J+ DgVyqZMi353GaG1wecIejOgsZd5mcZu7j0AS2OFcU9gvoq28DG3v0aM70KoFqGIgcxPoP2Y84B6USK3rpVxGxudcj6yXuVLqvLWv2l47oUTVU5Z71GL42m6UchYub23eVkZoyVTuzKUzxQ3P9nMNg=
|
18
|
+
on:
|
19
|
+
tags: true
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Copyright (c) 2015 Westfield Labs
|
2
|
+
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
you may not use this file except in compliance with the License.
|
5
|
+
You may obtain a copy of the License at
|
6
|
+
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
See the License for the specific language governing permissions and
|
13
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
# The Datetime Helper
|
2
|
+
|
3
|
+
A collection of useful utilities for projects that have to deal with dates, times, and time zones, with particular utility for Rails projects that enforce the use of Zulu Time.
|
4
|
+
|
5
|
+
## Features
|
6
|
+
|
7
|
+
1. A base method called `is_zulu_time?`,
|
8
|
+
2. An `rspec` matcher called `be_zulu_time`,
|
9
|
+
3. An `ActiveModel` validator called `zulu_time`, and
|
10
|
+
4. An `ActiveModel::Serializer` helper method called `in_zulu_time`.
|
11
|
+
|
12
|
+
Each feature can be required individually so you can use the `rspec` matcher, `ActiveModel` validator, or `ActiveModel::Serializer` helper in isolation.
|
13
|
+
|
14
|
+
[![Build Status](https://travis-ci.org/westfieldlabs/datetime_helper.svg?branch=master)](https://travis-ci.org/westfieldlabs/datetime_helper)
|
15
|
+
|
16
|
+
## Requirements
|
17
|
+
|
18
|
+
1. `Ruby`, `Bundler`, etc. The usual suspects. (Tested against Ruby 2.0.0 and up)
|
19
|
+
2. `rspec` if you `require 'datetime_helper/rspec'`
|
20
|
+
3. `active_model` if you `require 'datetime_helper/active_model'`
|
21
|
+
4. `active_model_serializers` if you `require 'datetime_helper/active_model_serialiser'`
|
22
|
+
|
23
|
+
## TL;DR
|
24
|
+
|
25
|
+
Zulu Time is an ISO 8601 formatted string representing a `datetime` but in the time zone UTC+0. This makes it trivial for client applications to display dates and times correctly in their local, or other nominated time zones.
|
26
|
+
|
27
|
+
Enforcing Zulu Time across a range of projects requires a common approach to validating incoming strings, representing the data internally, serialising the data back out into strings, and testing date and time fields.
|
28
|
+
|
29
|
+
The `Datetime Helper` was developed to provide that common approach, and it is available as an open source project because we believe it is generically useful.
|
30
|
+
|
31
|
+
## To use
|
32
|
+
|
33
|
+
Put this in your `Gemfile`
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
gem 'datetime_helper'
|
37
|
+
```
|
38
|
+
|
39
|
+
### Testing a string to see if it is a Zulu Time formatted string
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
DatetimeHelper.is_zulu_time? "some_string"
|
43
|
+
```
|
44
|
+
|
45
|
+
### Using the `be_zulu_time` matcher in your `RSpec` tests
|
46
|
+
|
47
|
+
Put this in your `spec_helper.rb` or equivalent
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
require 'datetime_helper/rspec'
|
51
|
+
|
52
|
+
RSpec.configure do |config|
|
53
|
+
config.include DatetimeHelper::Matchers
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
And put this in your `rspec` tests.
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
it {expect(subject[:deleted_at]).to be_zulu_time}
|
61
|
+
```
|
62
|
+
|
63
|
+
### Validating `ActiveModel` fields to ensure they hold UTC+0 `datetime` data
|
64
|
+
|
65
|
+
First be sure you `require 'datetime_helper/active_model'`
|
66
|
+
|
67
|
+
Then your model class can add:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
include DatetimeHelper::Validators
|
71
|
+
|
72
|
+
validates :updated_at, zulu_time: true
|
73
|
+
```
|
74
|
+
|
75
|
+
This will verify that a `Time` is supplied at `UTC+0`, or that a `DateTime` has `.zone == "+00:00"`.
|
76
|
+
|
77
|
+
### Enforcing `ActiveModel::Serializer` Zulu Time string formats
|
78
|
+
|
79
|
+
First be sure you `require 'datetime_helper/active_model_serialisers'`
|
80
|
+
|
81
|
+
Then you can put this in your serialisers:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
extend DatetimeHelper::Serialisers
|
85
|
+
|
86
|
+
in_zulu_time :updated_at
|
87
|
+
```
|
88
|
+
|
89
|
+
or if you have a bunch of 'em
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
extend DatetimeHelper::Serialisers
|
93
|
+
|
94
|
+
%w(updated_at deleted_at).each { |attribute| in_zulu_time attribute }
|
95
|
+
```
|
96
|
+
|
97
|
+
This will ensure that the serialised output is a proper Zulu Time formatted string.
|
98
|
+
|
99
|
+
## To build
|
100
|
+
|
101
|
+
```sh
|
102
|
+
bundle install
|
103
|
+
gem build datetime_helper.gemspec
|
104
|
+
```
|
105
|
+
|
106
|
+
## To test
|
107
|
+
|
108
|
+
```sh
|
109
|
+
bundle install
|
110
|
+
rake
|
111
|
+
```
|
112
|
+
|
113
|
+
The tests offer insight into how to use these utilities.
|
114
|
+
|
115
|
+
## To contribute
|
116
|
+
|
117
|
+
Contributions are encouraged. See the [contribution instructions](contributing.md) for the preferred contribution process.
|
118
|
+
|
119
|
+
## License
|
120
|
+
|
121
|
+
The `Datetime Helper` is © 2015 Westfield Labs and is available for use under the [Apache 2.0](LICENSE) license.
|
122
|
+
|
123
|
+
## Version history
|
124
|
+
|
125
|
+
|Version| Comments |
|
126
|
+
|:------|:-------------------------------------------|
|
127
|
+
|`0.0.1`| First draft — only the rspec matcher |
|
128
|
+
|`0.0.2`| Added the `ActiveModel` validator |
|
129
|
+
|`0.0.3`| Added the `ActiveModel::Serializer` helper |
|
130
|
+
|`1.0.0`| Cleaned up for first official release |
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
|
4
|
+
task :default => [:spec]
|
5
|
+
|
6
|
+
desc "Validate the gemspec"
|
7
|
+
task :gemspec do
|
8
|
+
gemspec = eval(File.read(Dir["*.gemspec"].first))
|
9
|
+
gemspec.validate && puts('gemspec valid')
|
10
|
+
end
|
11
|
+
|
12
|
+
RSpec::Core::RakeTask.new(:spec)
|
data/contributing.md
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
## Contributing
|
2
|
+
|
3
|
+
"Brother I got a 12 step plan for you…" - The Alabama Three
|
4
|
+
|
5
|
+
0. Raise an issue in this project outling the change you'd like to make. You'll need the `issue_number`
|
6
|
+
1. Fork this project into your own Github account
|
7
|
+
2. Create your feature branch (`git checkout -b {issue_number}_my-new-feature`)
|
8
|
+
4. Push to the branch (`git push -u origin {issue_number}_my-new-feature`)
|
9
|
+
5. Write rspec tests to formalise the outcomes of your change
|
10
|
+
6. write the code to implement the changes, ensuring that the tests eventually pass
|
11
|
+
7. Commit your changes (`git commit -am '{issue_number} Added some feature'`)
|
12
|
+
8. Push to the branch (`git push -u origin my-new-feature`)
|
13
|
+
9. Create a new Pull Request using Github's built-in mechanisms.
|
14
|
+
10. Wait for a code review to come back, or an approval.
|
15
|
+
11. Either go back and fix whatever the reviewers recommend, or watch and smile as your PR gets merged in and you become part of sotware development history.
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'datetime_helper/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "datetime_helper"
|
8
|
+
spec.version = DatetimeHelper::VERSION
|
9
|
+
spec.authors = ["davesag"]
|
10
|
+
spec.email = ["davesag@gmail.com"]
|
11
|
+
spec.summary = "A collection of useful helpers for projects which have to deal with dates, times, and time zones."
|
12
|
+
spec.description = "The `Datetime Helper` was developed to provide a common approach to validating incoming datetime data, representing that data internally, serialising it back out into strings, and testing of date and time fields."
|
13
|
+
spec.homepage = "https://github.com/westfieldlabs/datetime_helper"
|
14
|
+
spec.license = "Apache 2.0"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency 'rspec', '~> 3'
|
22
|
+
spec.add_runtime_dependency 'activemodel', '~> 4'
|
23
|
+
spec.add_runtime_dependency 'active_model_serializers'
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.4"
|
27
|
+
spec.add_development_dependency "pry"
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# Enforces that a DateTime is serialised as zulu time
|
2
|
+
|
3
|
+
module DatetimeHelper
|
4
|
+
module Serialisers
|
5
|
+
|
6
|
+
def enforce_zulu_time(attribute)
|
7
|
+
define_method(attribute) do
|
8
|
+
object.send(attribute).utc.iso8601 unless object.send(attribute).nil?
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# Validates that a supplied value is in fact
|
2
|
+
# a DateTime, and is in timezone UTC+0
|
3
|
+
require 'time'
|
4
|
+
require 'active_model'
|
5
|
+
|
6
|
+
module DatetimeHelper
|
7
|
+
module Validators
|
8
|
+
class ZuluTimeValidator < ActiveModel::EachValidator
|
9
|
+
|
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
|
+
|
14
|
+
record.errors[attribute] << (options[:message] || 'is not in UTC+0 (Zulu Time)')
|
15
|
+
end
|
16
|
+
|
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
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe DatetimeHelper do
|
4
|
+
describe '#is_zulu_time?' do
|
5
|
+
context "given a valid Zulu Time string" do
|
6
|
+
let(:valid_string) { Time.now.utc.iso8601 }
|
7
|
+
it { expect{ Time.parse(valid_string)}.to_not raise_error }
|
8
|
+
it { expect(DatetimeHelper.is_zulu_time?(valid_string)).to eq true }
|
9
|
+
end
|
10
|
+
|
11
|
+
context "given a valid ISO 8601 string that's not Zulu Time" do
|
12
|
+
let(:invalid_string) { Time.now.iso8601 }
|
13
|
+
it { expect{ Time.parse(invalid_string)}.to_not raise_error }
|
14
|
+
it { expect(DatetimeHelper.is_zulu_time?(invalid_string)).to eq false }
|
15
|
+
end
|
16
|
+
|
17
|
+
context "given nonsense" do
|
18
|
+
let(:nonsense) { "not really a zulu time string" }
|
19
|
+
it { expect{ Time.parse(nonsense)}.to raise_error }
|
20
|
+
it { expect(DatetimeHelper.is_zulu_time?(nonsense)).to eq false }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
describe 'be_zulu_time' do
|
4
|
+
context 'given a valid zulu time string' do
|
5
|
+
let(:valid_time_string) { Time.now.utc.iso8601 }
|
6
|
+
it { expect(valid_time_string).to be_zulu_time }
|
7
|
+
end
|
8
|
+
|
9
|
+
context 'given an invalid zulu time string' do
|
10
|
+
let(:invalid_time_string) { "This is not a zulu time string" }
|
11
|
+
it { expect(invalid_time_string).to_not be_zulu_time }
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require "datetime_helper/active_model_serialiser"
|
3
|
+
|
4
|
+
class TestSerialiser < ActiveModel::Serializer
|
5
|
+
extend DatetimeHelper::Serialisers
|
6
|
+
attributes :updated_at
|
7
|
+
|
8
|
+
enforce_zulu_time :updated_at
|
9
|
+
end
|
10
|
+
|
11
|
+
class TestItemNoTime
|
12
|
+
include ActiveModel::SerializerSupport
|
13
|
+
attr_reader :updated_at
|
14
|
+
def initialize(options = {})
|
15
|
+
@updated_at = nil
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class TestItem < TestItemNoTime
|
20
|
+
def initialize(options = {})
|
21
|
+
@updated_at = options[:updated_at] || Time.now.utc
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe DatetimeHelper::Serialisers do
|
26
|
+
let(:serialiser) { TestSerialiser.new(item) }
|
27
|
+
subject { serialiser.attributes }
|
28
|
+
|
29
|
+
context 'with a UTC time' do
|
30
|
+
let(:item) { TestItem.new }
|
31
|
+
it { expect(subject[:updated_at]).to be_zulu_time}
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'with a local time' do
|
35
|
+
let(:item) { TestItem.new updated_at: Time.now.getlocal }
|
36
|
+
it { expect(subject[:updated_at]).to be_zulu_time}
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'with a nil time' do
|
40
|
+
let(:item) { TestItemNoTime.new }
|
41
|
+
it { expect(subject[:updated_at]).to be_nil}
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'datetime_helper/rspec'
|
2
|
+
require 'pry' # for debugging
|
3
|
+
require 'time' # used all over the place
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
|
7
|
+
# If no example or groups contains the filter symbol below, then run all
|
8
|
+
config.run_all_when_everything_filtered = true
|
9
|
+
# If supplied, run only those examples and groups with the symbol below
|
10
|
+
# i.e. it "some text", :focus do
|
11
|
+
config.filter_run_including :focus
|
12
|
+
|
13
|
+
# Run specs in random order to surface order dependencies. If you find an
|
14
|
+
# order dependency and want to debug it, you can fix the order by providing
|
15
|
+
# the seed, which is printed after each run.
|
16
|
+
# --seed 1234
|
17
|
+
config.order = 'random'
|
18
|
+
|
19
|
+
# Hi I'm Dave I like pretty colours.
|
20
|
+
config.color = true
|
21
|
+
config.tty = true
|
22
|
+
config.formatter = :documentation
|
23
|
+
|
24
|
+
config.include DatetimeHelper::Matchers
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require "datetime_helper/active_model"
|
3
|
+
|
4
|
+
describe DatetimeHelper::Validators::ZuluTimeValidator do
|
5
|
+
let(:validator) { described_class.new({attributes: [:updated_at]})}
|
6
|
+
let(:model) { double('model') }
|
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
|
+
before :each do
|
14
|
+
allow(model).to receive_message_chain('errors').and_return([])
|
15
|
+
allow(model.errors).to receive_message_chain('[]').and_return({})
|
16
|
+
allow(model.errors[]).to receive_message_chain('<<')
|
17
|
+
end
|
18
|
+
|
19
|
+
context "given a UTC+0 Time" do
|
20
|
+
let(:valid_time) { Time.now.utc }
|
21
|
+
it "is accepted as valid" do
|
22
|
+
expect(model.errors[]).to_not receive('<<')
|
23
|
+
validator.validate_each(model, 'updated_at', valid_time)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "given a local Time" do
|
28
|
+
let(:invalid_time) { Time.now.getlocal }
|
29
|
+
it "is not accepted as valid" do
|
30
|
+
pending "Local Time is UTC so skipping test" if already_in_utc
|
31
|
+
expect(model.errors[]).to receive('<<')
|
32
|
+
validator.validate_each(model, 'updated_at', invalid_time)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "given a UTC+0 DateTime" do
|
37
|
+
let(:valid_time) { DateTime.now.new_offset(0) }
|
38
|
+
it "is accepted as valid" do
|
39
|
+
expect(model.errors[]).to_not receive('<<')
|
40
|
+
validator.validate_each(model, 'updated_at', valid_time)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "given a local DateTime" do
|
45
|
+
let(:invalid_time) { DateTime.now }
|
46
|
+
it "is not accepted as valid" do
|
47
|
+
pending "Local Time is UTC so skipping test" if already_in_utc
|
48
|
+
expect(model.errors[]).to receive('<<')
|
49
|
+
validator.validate_each(model, 'updated_at', invalid_time)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context "given a String instead of a Time or DateTime" do
|
54
|
+
let(:nonsense) { "this makes no sense" }
|
55
|
+
it "is not accepted as valid" do
|
56
|
+
expect(model.errors[]).to receive('<<')
|
57
|
+
validator.validate_each(model, 'updated_at', nonsense)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
metadata
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: datetime_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- davesag
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activemodel
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: active_model_serializers
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.9'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.9'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.4'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.4'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ! '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: The `Datetime Helper` was developed to provide a common approach to validating
|
98
|
+
incoming datetime data, representing that data internally, serialising it back out
|
99
|
+
into strings, and testing of date and time fields.
|
100
|
+
email:
|
101
|
+
- davesag@gmail.com
|
102
|
+
executables: []
|
103
|
+
extensions: []
|
104
|
+
extra_rdoc_files: []
|
105
|
+
files:
|
106
|
+
- .gitignore
|
107
|
+
- .travis.yml
|
108
|
+
- Gemfile
|
109
|
+
- LICENSE
|
110
|
+
- README.md
|
111
|
+
- Rakefile
|
112
|
+
- contributing.md
|
113
|
+
- datetime_helper.gemspec
|
114
|
+
- lib/datetime_helper.rb
|
115
|
+
- lib/datetime_helper/active_model.rb
|
116
|
+
- lib/datetime_helper/active_model_serialiser.rb
|
117
|
+
- lib/datetime_helper/matchers/zulu_time_rspec_matcher.rb
|
118
|
+
- lib/datetime_helper/rspec.rb
|
119
|
+
- lib/datetime_helper/serialisers/zulu_time_active_model_serialiser_helper.rb
|
120
|
+
- lib/datetime_helper/validators/zulu_time_active_model_validator.rb
|
121
|
+
- lib/datetime_helper/version.rb
|
122
|
+
- spec/datetime_helper_spec.rb
|
123
|
+
- spec/matchers/zulu_time_rspec_matcher_spec.rb
|
124
|
+
- spec/serialisers/zulu_time_active_model_serialiser_helper_spec.rb
|
125
|
+
- spec/spec_helper.rb
|
126
|
+
- spec/validators/zulu_time_active_model_validator_spec.rb
|
127
|
+
homepage: https://github.com/westfieldlabs/datetime_helper
|
128
|
+
licenses:
|
129
|
+
- Apache 2.0
|
130
|
+
metadata: {}
|
131
|
+
post_install_message:
|
132
|
+
rdoc_options: []
|
133
|
+
require_paths:
|
134
|
+
- lib
|
135
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ! '>='
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ! '>='
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
requirements: []
|
146
|
+
rubyforge_project:
|
147
|
+
rubygems_version: 2.4.5
|
148
|
+
signing_key:
|
149
|
+
specification_version: 4
|
150
|
+
summary: A collection of useful helpers for projects which have to deal with dates,
|
151
|
+
times, and time zones.
|
152
|
+
test_files:
|
153
|
+
- spec/datetime_helper_spec.rb
|
154
|
+
- spec/matchers/zulu_time_rspec_matcher_spec.rb
|
155
|
+
- spec/serialisers/zulu_time_active_model_serialiser_helper_spec.rb
|
156
|
+
- spec/spec_helper.rb
|
157
|
+
- spec/validators/zulu_time_active_model_validator_spec.rb
|