mongoid-time_with_named_zone 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/mongoid/time_with_named_zone.rb +27 -36
- data/lib/mongoid/time_with_named_zone/version.rb +2 -2
- data/spec/time_with_zone_spec.rb +23 -23
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f73cc1515f962a4f7b277a17338b4d4a3439c547
|
4
|
+
data.tar.gz: 6d63ec9f1e9f71815106d9bccd054f75e6b1f9a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91a6d4c816197825b2845cd230362e647f707a597b6bc797195ca02bd8b4a17b0e56bd95c01f6644f37c2f7a0086c3046539e781ac46aabd22ed176b95135cce
|
7
|
+
data.tar.gz: 867a786d45e980689b993ada146a13b4dd56cc583c5916d5c9c020e92214920a028a9e691f211662f35080bf1ea2f3481df7af77932772175b82922a3bf63542
|
data/README.md
CHANGED
@@ -1,51 +1,42 @@
|
|
1
|
+
require 'mongoid'
|
1
2
|
require 'mongoid/time_with_named_zone/version'
|
2
3
|
|
3
4
|
# Mongoid namespace from the Mongoid gem
|
4
5
|
module Mongoid
|
5
6
|
# Object that stores time and tz information as embedded object
|
6
7
|
# transparently works with AS:TwZ, supports mongoid serialization
|
7
|
-
|
8
|
-
attr_reader :time, :zone
|
9
|
-
|
10
|
-
def initialize(time, zone = 'UTC')
|
11
|
-
@time, @zone = time, zone
|
12
|
-
end
|
13
|
-
|
14
|
-
# Converts an object of this instance into a database friendly value.
|
15
|
-
def mongoize
|
16
|
-
TimeWithNamedZone.mongoize(self)
|
17
|
-
end
|
18
|
-
|
8
|
+
module TimeWithNamedZone
|
19
9
|
class << self
|
20
|
-
#
|
21
|
-
# this custom class from it.
|
10
|
+
# Convert the object from its mongo friendly ruby type to this type
|
22
11
|
def demongoize(object)
|
23
|
-
return nil unless object && object[
|
24
|
-
object[
|
12
|
+
return nil unless object && object['time'] && object['zone']
|
13
|
+
object['time'].in_time_zone(object['zone'])
|
25
14
|
end
|
26
15
|
|
16
|
+
# Turn the object from the ruby type we deal with to a Mongo friendly type
|
27
17
|
def mongoize(object)
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
hash.mongoize
|
18
|
+
case object
|
19
|
+
when ActiveSupport::TimeWithZone
|
20
|
+
{
|
21
|
+
time: object.utc.mongoize,
|
22
|
+
zone: object.time_zone.name.mongoize
|
23
|
+
}
|
24
|
+
when Time || DateTime
|
25
|
+
{
|
26
|
+
time: object.utc.mongoize,
|
27
|
+
zone: 'UTC'.mongoize
|
28
|
+
}
|
29
|
+
when Date
|
30
|
+
{
|
31
|
+
time: object.mongoize,
|
32
|
+
zone: 'UTC'.mongoize
|
33
|
+
}
|
34
|
+
else
|
35
|
+
nil
|
36
|
+
end
|
48
37
|
end
|
38
|
+
|
39
|
+
alias_method :evolve, :mongoize
|
49
40
|
end
|
50
41
|
end
|
51
42
|
end
|
data/spec/time_with_zone_spec.rb
CHANGED
@@ -1,41 +1,33 @@
|
|
1
|
-
require 'mongoid'
|
2
1
|
require 'mongoid/time_with_named_zone'
|
3
2
|
|
4
3
|
describe Mongoid::TimeWithNamedZone do
|
5
4
|
describe '#mongoize' do
|
6
|
-
|
7
|
-
let(:time) { Mongoid::TimeWithNamedZone.new(Time.utc(2010, 11, 19), 'Pacific/Auckland') }
|
8
|
-
let(:time_with_invalid_zone) { Mongoid::TimeWithNamedZone.new(Time.utc(2010, 11, 19), 'Invalid_timezone') }
|
5
|
+
subject { Mongoid::TimeWithNamedZone.mongoize(time) }
|
9
6
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
it 'raise an error for invalid timezone' do
|
15
|
-
expect { Mongoid::TimeWithNamedZone.mongoize(time_with_invalid_zone) }.to raise_error
|
16
|
-
end
|
7
|
+
context 'time in Lima zone' do
|
8
|
+
let(:time) { Time.utc(2010, 11, 19).in_time_zone 'Lima' }
|
9
|
+
it { is_expected.to eq(time: Time.utc(2010, 11, 19), zone: 'Lima') }
|
17
10
|
end
|
18
11
|
|
19
|
-
context '
|
20
|
-
let(:time) {
|
21
|
-
|
22
|
-
it 'returns hash with utc time and zone' do
|
23
|
-
expect(Mongoid::TimeWithNamedZone.mongoize(time)).to eq(time: Time.utc(2010, 11, 19), zone: 'Pacific/Auckland')
|
24
|
-
end
|
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') }
|
25
15
|
end
|
26
16
|
|
27
|
-
context '
|
28
|
-
let(:time) {
|
17
|
+
context 'Date object' do
|
18
|
+
let(:time) { Date.new(2010, 11, 29) }
|
19
|
+
it { is_expected.to eq(time: Time.utc(2010, 11, 29), zone: 'UTC') }
|
20
|
+
end
|
29
21
|
|
30
|
-
|
31
|
-
|
32
|
-
|
22
|
+
context 'DateTime object' do
|
23
|
+
let(:time) { DateTime.new(2010, 11, 29) }
|
24
|
+
it { is_expected.to eq(time: Time.utc(2010, 11, 29), zone: 'UTC') }
|
33
25
|
end
|
34
26
|
end
|
35
27
|
|
36
28
|
describe '.demongoize' do
|
37
29
|
it 'returns time in saved zone' do
|
38
|
-
hash = { time
|
30
|
+
hash = { 'time' => Time.utc(2010, 11, 19), 'zone' => 'Pacific/Auckland' }
|
39
31
|
demongoized_value = Mongoid::TimeWithNamedZone.demongoize(hash)
|
40
32
|
expect(demongoized_value).to eq(Time.new(2010, 11, 19, '+13:00'))
|
41
33
|
end
|
@@ -43,5 +35,13 @@ describe Mongoid::TimeWithNamedZone do
|
|
43
35
|
it 'returns nil for blank object' do
|
44
36
|
expect(Mongoid::TimeWithNamedZone.demongoize(nil)).to eq(nil)
|
45
37
|
end
|
38
|
+
|
39
|
+
it 'returns nil for empty hash' do
|
40
|
+
expect(Mongoid::TimeWithNamedZone.demongoize({})).to eq(nil)
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'returns nil for hash without time' do
|
44
|
+
expect(Mongoid::TimeWithNamedZone.demongoize('zone' => 'UTC')).to eq(nil)
|
45
|
+
end
|
46
46
|
end
|
47
47
|
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.
|
4
|
+
version: 0.2.0
|
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-
|
13
|
+
date: 2014-10-21 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: mongoid
|