mongoid-time_with_named_zone 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 0a57b966d50562922af60e9abeb585d031826bc4
4
- data.tar.gz: b17f8b520e92bdd9a47e23d38f5b07c69cca4b55
2
+ SHA256:
3
+ metadata.gz: cc8b913538b6ba3be4b79eca00c8b6a572c2c4973208297d5d67b77bc5841791
4
+ data.tar.gz: 71d10a6d60b1fdd00bc315bb73add49eb1531a3e82fabafec656d0f8127eac84
5
5
  SHA512:
6
- metadata.gz: 385f2dd028b7a8384ce61cea0480fd0410e317102f5ccd1ec549301e40d31df7fda7ac10812c79d7ddfb91787a697dd6b32c7863a12ce60dadd06f7af082b2a3
7
- data.tar.gz: e394289fc108e3b6341111ecddcdbea1c7d867643edfa8de9aec8cef3ec741f5a9979fdd78c60a24f0b797969162d99ceb9be827500648ac5962bb0b69080e91
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
- time: object.mongoize,
34
- zone: 'UTC'.mongoize
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
 
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module TimeWithNamedZone
3
- VERSION = '0.2.1'.freeze
3
+ VERSION = '0.2.2'.freeze
4
4
  end
5
5
  end
@@ -2,52 +2,71 @@ require 'mongoid/time_with_named_zone'
2
2
 
3
3
  describe Mongoid::TimeWithNamedZone do
4
4
  describe '#mongoize' do
5
- subject { Mongoid::TimeWithNamedZone.mongoize(time) }
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) { Time.utc(2010, 11, 19).in_time_zone 'Lima' }
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 'Time object' do
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 object' do
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
- hash = { 'time' => Time.utc(2010, 11, 19), 'zone' => 'Pacific/Auckland' }
31
- demongoized_value = Mongoid::TimeWithNamedZone.demongoize(hash)
32
- expect(demongoized_value).to eq(Time.new(2010, 11, 19, '+13:00'))
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
- it 'works with symbolized keys' do
36
- hash = { time: Time.utc(2010, 11, 19), zone: 'Pacific/Auckland' }
37
- demongoized_value = Mongoid::TimeWithNamedZone.demongoize(hash)
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
- it 'returns nil for blank object' do
42
- expect(Mongoid::TimeWithNamedZone.demongoize(nil)).to eq(nil)
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
- it 'returns nil for empty hash' do
46
- expect(Mongoid::TimeWithNamedZone.demongoize({})).to eq(nil)
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
- it 'returns nil for hash without time' do
50
- expect(Mongoid::TimeWithNamedZone.demongoize('zone' => 'UTC')).to eq(nil)
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.1
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: 2014-10-21 00:00:00.000000000 Z
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.2.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