mongoid-time_with_named_zone 0.2.1 → 0.2.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.
- checksums.yaml +5 -5
- data/lib/mongoid/time_with_named_zone.rb +6 -15
- data/lib/mongoid/time_with_named_zone/version.rb +1 -1
- data/spec/time_with_zone_spec.rb +41 -22
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cc8b913538b6ba3be4b79eca00c8b6a572c2c4973208297d5d67b77bc5841791
|
4
|
+
data.tar.gz: 71d10a6d60b1fdd00bc315bb73add49eb1531a3e82fabafec656d0f8127eac84
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 78b7cbaec7e9fd6c7e0b5fd09c2305bc39e7600658f2bd2e6fb4237187d4727faf96e04d4f0a19cbd205115fdfc606e7468af25dbc17fb29dede0d30117d15fb
|
7
|
+
data.tar.gz: 7ed62848769c66d972fa2869dad19cfc5442f11b9450a6339d289041cb7ea2f25b247269e9c69b851d93ee2b232d28098eb1b7e5dd28e335e9d5ecf1ddb95a3f
|
@@ -10,7 +10,7 @@ module Mongoid
|
|
10
10
|
# Convert the object from its mongo friendly ruby type to this type
|
11
11
|
def demongoize(object)
|
12
12
|
return nil unless object.is_a? Hash
|
13
|
-
object.symbolize_keys!
|
13
|
+
object = object.to_h.symbolize_keys!
|
14
14
|
return nil unless object[:time] && object[:zone]
|
15
15
|
object[:time].in_time_zone(object[:zone])
|
16
16
|
end
|
@@ -19,22 +19,13 @@ module Mongoid
|
|
19
19
|
def mongoize(object)
|
20
20
|
case object
|
21
21
|
when ActiveSupport::TimeWithZone
|
22
|
-
{
|
23
|
-
time: object.utc.mongoize,
|
24
|
-
zone: object.time_zone.name.mongoize
|
25
|
-
}
|
22
|
+
{ time: object.utc.mongoize, zone: object.time_zone.name.mongoize }
|
26
23
|
when Time || DateTime
|
27
|
-
{
|
28
|
-
time: object.utc.mongoize,
|
29
|
-
zone: 'UTC'.mongoize
|
30
|
-
}
|
24
|
+
{ time: object.utc.mongoize, zone: 'UTC'.mongoize }
|
31
25
|
when Date
|
32
|
-
{
|
33
|
-
|
34
|
-
|
35
|
-
}
|
36
|
-
else
|
37
|
-
nil
|
26
|
+
{ time: object.mongoize, zone: 'UTC'.mongoize }
|
27
|
+
when Hash
|
28
|
+
{ time: object[:time], zone: object[:zone] }
|
38
29
|
end
|
39
30
|
end
|
40
31
|
|
data/spec/time_with_zone_spec.rb
CHANGED
@@ -2,52 +2,71 @@ require 'mongoid/time_with_named_zone'
|
|
2
2
|
|
3
3
|
describe Mongoid::TimeWithNamedZone do
|
4
4
|
describe '#mongoize' do
|
5
|
-
|
5
|
+
let(:time) { Time.utc(2010, 11, 19) }
|
6
|
+
subject { described_class.mongoize(time) }
|
7
|
+
|
8
|
+
it { is_expected.to eq(time: Time.utc(2010, 11, 19), zone: 'UTC') }
|
6
9
|
|
7
10
|
context 'time in Lima zone' do
|
8
|
-
let(:time) {
|
11
|
+
let(:time) { super().in_time_zone 'Lima' }
|
9
12
|
it { is_expected.to eq(time: Time.utc(2010, 11, 19), zone: 'Lima') }
|
10
13
|
end
|
11
14
|
|
12
|
-
context '
|
13
|
-
let(:time) { Time.utc(2010, 11, 19) }
|
14
|
-
it { is_expected.to eq(time: Time.utc(2010, 11, 19), zone: 'UTC') }
|
15
|
-
end
|
16
|
-
|
17
|
-
context 'Date object' do
|
15
|
+
context 'when time is an instance of `Date`' do
|
18
16
|
let(:time) { Date.new(2010, 11, 29) }
|
19
17
|
it { is_expected.to eq(time: Time.utc(2010, 11, 29), zone: 'UTC') }
|
20
18
|
end
|
21
19
|
|
22
|
-
context 'DateTime
|
20
|
+
context 'when time is an instanc of `DateTime`' do
|
23
21
|
let(:time) { DateTime.new(2010, 11, 29) }
|
24
22
|
it { is_expected.to eq(time: Time.utc(2010, 11, 29), zone: 'UTC') }
|
25
23
|
end
|
24
|
+
|
25
|
+
context 'when time is actually a `Hash`' do
|
26
|
+
let(:time) { { time: Time.utc(2010, 11, 29), zone: 'UTC' } }
|
27
|
+
it { is_expected.to eq(time: Time.utc(2010, 11, 29), zone: 'UTC') }
|
28
|
+
end
|
26
29
|
end
|
27
30
|
|
28
31
|
describe '.demongoize' do
|
32
|
+
let(:hash) { { 'time' => Time.utc(2010, 11, 19), 'zone' => 'Pacific/Auckland' } }
|
33
|
+
subject { described_class.demongoize(hash) }
|
34
|
+
|
29
35
|
it 'returns time in saved zone' do
|
30
|
-
|
31
|
-
|
32
|
-
|
36
|
+
expect(subject).to eq(Time.new(2010, 11, 19, '+13:00'))
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'When the hash has symbol keys' do
|
40
|
+
let(:hash) { super().stringify_keys }
|
41
|
+
|
42
|
+
it 'returns time in saved zone' do
|
43
|
+
expect(subject).to eq(Time.new(2010, 11, 19, '+13:00'))
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'When the payload is nil' do
|
48
|
+
let(:hash) { nil }
|
49
|
+
it { is_expected.to eq(nil) }
|
33
50
|
end
|
34
51
|
|
35
|
-
|
36
|
-
hash
|
37
|
-
|
38
|
-
expect(demongoized_value).to eq(Time.new(2010, 11, 19, '+13:00'))
|
52
|
+
context 'When the payload is empty' do
|
53
|
+
let(:hash) { {} }
|
54
|
+
it { is_expected.to eq(nil) }
|
39
55
|
end
|
40
56
|
|
41
|
-
|
42
|
-
|
57
|
+
context 'When the hash has no time' do
|
58
|
+
let(:hash) { super().tap { |h| h.delete('time') } }
|
59
|
+
it { is_expected.to eq(nil) }
|
43
60
|
end
|
44
61
|
|
45
|
-
|
46
|
-
|
62
|
+
context 'When the hash has no zone' do
|
63
|
+
let(:hash) { super().tap { |h| h.delete('zone') } }
|
64
|
+
it { is_expected.to eq(nil) }
|
47
65
|
end
|
48
66
|
|
49
|
-
|
50
|
-
|
67
|
+
context 'When the hash is actually an instance of `BSON::Document`' do
|
68
|
+
let(:hash) { BSON::Document.new(super()) }
|
69
|
+
it { is_expected.to eq(Time.new(2010, 11, 19, '+13:00')) }
|
51
70
|
end
|
52
71
|
end
|
53
72
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-time_with_named_zone
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Carnival Mobile
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2018-11-07 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: mongoid
|
@@ -105,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
105
|
version: '0'
|
106
106
|
requirements: []
|
107
107
|
rubyforge_project:
|
108
|
-
rubygems_version: 2.
|
108
|
+
rubygems_version: 2.7.7
|
109
109
|
signing_key:
|
110
110
|
specification_version: 4
|
111
111
|
summary: 'This gem works with Mongoid and transparently maps ActiveSupport::TimeWithZone
|