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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 47c61ecde1e0577a2e05de0fd9eec9fcc5d88826
4
- data.tar.gz: dba4846f6b9cbac1566827f62da6f99095060676
3
+ metadata.gz: f73cc1515f962a4f7b277a17338b4d4a3439c547
4
+ data.tar.gz: 6d63ec9f1e9f71815106d9bccd054f75e6b1f9a1
5
5
  SHA512:
6
- metadata.gz: 1aa8c656e02b29a268e230246d3016f4cc5166bb2afe975de582bb673c7eb7c5222bddff0fce61b922abfad7e5bff63e5a70f8a012f283cb96450a50d74db7e4
7
- data.tar.gz: e57395d13a27a7f28d6cc7efefb5f7c4be2c080da58deaa2f0135a325e2df4c8050724885d83c30bb4903b42f027ec77ab3e58a8a85c2d08f108d443fc2f5e25
6
+ metadata.gz: 91a6d4c816197825b2845cd230362e647f707a597b6bc797195ca02bd8b4a17b0e56bd95c01f6644f37c2f7a0086c3046539e781ac46aabd22ed176b95135cce
7
+ data.tar.gz: 867a786d45e980689b993ada146a13b4dd56cc583c5916d5c9c020e92214920a028a9e691f211662f35080bf1ea2f3481df7af77932772175b82922a3bf63542
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Mongoid Time With Zone
1
+ # Mongoid Time With Named Zone
2
2
 
3
3
  Timezones can be a developer's worst nightmare and introduce subtle bugs.
4
4
 
@@ -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
- class TimeWithNamedZone
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
- # Get the object as it was stored in the database, and instantiate`
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[:time] && object[:zone]
24
- object[:time].in_time_zone(object[:zone])
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
- hash = case object
29
- when self
30
- {
31
- time: object.time,
32
- zone: ActiveSupport::TimeZone.new(object.zone).name
33
- }
34
- when ActiveSupport::TimeWithZone
35
- {
36
- time: object.utc,
37
- zone: object.time_zone
38
- }
39
- when Time
40
- {
41
- time: object.utc,
42
- zone: 'UTC'
43
- }
44
- else
45
- nil
46
- end
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
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
- class TimeWithNamedZone
3
- VERSION = '0.1.4'.freeze
2
+ module TimeWithNamedZone
3
+ VERSION = '0.2.0'.freeze
4
4
  end
5
5
  end
@@ -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
- context 'TimeWithNamedZone' do
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
- it 'returns hash with utc time and zone' do
11
- expect(Mongoid::TimeWithNamedZone.mongoize(time)).to eq(time: Time.utc(2010, 11, 19), zone: 'Pacific/Auckland')
12
- end
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 'ActiveSupport::TimeWithZone' do
20
- let(:time) { ActiveSupport::TimeWithZone.new(Time.utc(2010, 11, 19), 'Pacific/Auckland') }
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 'Time' do
28
- let(:time) { Time.utc(2010, 11, 19) }
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
- it 'returns hash with utc time and utc zone' do
31
- expect(Mongoid::TimeWithNamedZone.mongoize(time)).to eq(time: Time.utc(2010, 11, 19), zone: 'UTC')
32
- end
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: Time.utc(2010, 11, 19), zone: 'Pacific/Auckland' }
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.1.4
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-20 00:00:00.000000000 Z
13
+ date: 2014-10-21 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: mongoid