mongoid-time_with_named_zone 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 79c13f8d281f31aa9f7322050df67573d4df3b58
4
+ data.tar.gz: 74bd30d3a7f9dd11adae14a25b0d7ae89c208f1e
5
+ SHA512:
6
+ metadata.gz: 7861710c85df3fc471c6533313316b66de70aedebb797ae74f98b06c3661a1758a67978837e9461f30ea1695d98cada93e142c5d65c8f1fc87379e9139835e43
7
+ data.tar.gz: 4d7e21565a27c84de7fbc511378f855ad4213af444c9da4433267ffcf6df3decc39f0161979ea92f1b10ac221f905aecd8848b53aabcdffbc33dc56845044129
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2014 Carnival Mobile
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,68 @@
1
+ # Mongoid Time With Zone
2
+
3
+ Timezones can be a developer's worst nightmare and introduce subtle bugs.
4
+
5
+ This gem provides a [Mongoid](mongoid.org) wrapper for Time objects that includes and retains the timezone name as a string.
6
+
7
+ Reading from the database will use the `ActiveSupport::TimeWithZone`'s monkey patched `#in_time_zone` (monkey patched by Mongoid) to convert the time to being a TimeWithZone.
8
+
9
+ This gem does not apply any monkey patches.
10
+
11
+ This is necessary because MongoDB can save ISO8601 formatted date strings by using a [wrapping type ISODate()](http://docs.mongodb.org/manual/core/shell-types/#date),
12
+ but these don't include the strings representing the name of the timezone. This gem stores timezone string in the database.
13
+
14
+ ## Usage
15
+
16
+ with Bundler, add this gem to your `Gemfile`:
17
+
18
+ ```ruby
19
+ gem 'mongoid-time_with_named_zone'
20
+ ```
21
+
22
+ requiring manually:
23
+
24
+ ```ruby
25
+ require 'mongoid/time_with_named_zone'
26
+ ```
27
+
28
+ Define your Mongoid models with field types of `Mongoid::TimeWithNamedZone`:
29
+
30
+ e.g.:
31
+
32
+ ```ruby
33
+
34
+ class BlogPost
35
+
36
+ field :published_at, type: Mongoid::TimeWithNamedZone
37
+ field :body, type: String
38
+
39
+ end
40
+
41
+ ```
42
+
43
+ and assign attributes to Time instances:
44
+
45
+ ```ruby
46
+ post = BlogPost.new
47
+ post.published_at = Time.now.in_time_zone('Pacific/Auckland')
48
+
49
+ ```
50
+
51
+ ## Testing
52
+
53
+ Run `rake spec`
54
+
55
+ ## Contributing changes
56
+
57
+ Once you've made your great commits:
58
+
59
+ 1. Fork time-with-zone
60
+ 2. Create a topic branch - git checkout -b my_branch
61
+ 3. Push to your branch - git push origin my_branch
62
+ 4. Open a Pull Request to discuss your changes
63
+
64
+ That's it!
65
+
66
+ ## License
67
+
68
+ See [LICENSE](LICENSE)
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => [:spec]
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ class TimeWithNamedZone
3
+ VERSION = '0.1.1'.freeze
4
+ end
5
+ end
@@ -0,0 +1,50 @@
1
+ require 'mongoid/time_with_named_zone/version'
2
+
3
+ # Mongoid namespace from the Mongoid gem
4
+ module Mongoid
5
+ # Object that stores time and tz information as embedded object
6
+ # 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
+
19
+ class << self
20
+ # Get the object as it was stored in the database, and instantiate`
21
+ # this custom class from it.
22
+ def demongoize(object)
23
+ object[:time].in_time_zone(object[:zone]) if object
24
+ end
25
+
26
+ def mongoize(object)
27
+ hash = case object
28
+ when self
29
+ {
30
+ time: object.time,
31
+ zone: ActiveSupport::TimeZone.new(object.zone).name
32
+ }
33
+ when ActiveSupport::TimeWithZone
34
+ {
35
+ time: object.utc,
36
+ zone: object.time_zone
37
+ }
38
+ when Time
39
+ {
40
+ time: object.utc,
41
+ zone: 'UTC'
42
+ }
43
+ else
44
+ nil
45
+ end
46
+ hash.mongoize
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1 @@
1
+ require 'mongoid/time_with_named_zone'
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mongoid/time_with_named_zone/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'mongoid-time_with_named_zone'
8
+ spec.version = Mongoid::TimeWithNamedZone::VERSION
9
+ spec.authors = ['Carnival Mobile', 'Arthur Evstifeev', 'Artur Khantimirov']
10
+ spec.email = ['toby@carnivallabs.com']
11
+ spec.summary = %q{A Mongoid wrapper for Time objects that retains the timezone name}
12
+ spec.description = %q{This gem works transparently with ActiveSupport::TimeWithZone Times. See more: https://github.com/carnivalmobile/time-with-zone}
13
+ spec.homepage = 'https://github.com/carnivalmobile/time-with-zone'
14
+ spec.license = 'Apache 2'
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_dependency 'mongoid', '>= 3'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.7'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rspec', '~> 3.1.0'
26
+ end
@@ -0,0 +1,47 @@
1
+ require 'mongoid'
2
+ require 'mongoid/time_with_named_zone'
3
+
4
+ describe Mongoid::TimeWithNamedZone do
5
+ 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') }
9
+
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
17
+ end
18
+
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
25
+ end
26
+
27
+ context 'Time' do
28
+ let(:time) { Time.utc(2010, 11, 19) }
29
+
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
33
+ end
34
+ end
35
+
36
+ describe '.demongoize' do
37
+ it 'returns time in saved zone' do
38
+ hash = { time: Time.utc(2010, 11, 19), zone: 'Pacific/Auckland' }
39
+ demongoized_value = Mongoid::TimeWithNamedZone.demongoize(hash)
40
+ expect(demongoized_value).to eq(Time.new(2010, 11, 19, '+13:00'))
41
+ end
42
+
43
+ it 'returns nil for blank object' do
44
+ expect(Mongoid::TimeWithNamedZone.demongoize(nil)).to eq(nil)
45
+ end
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-time_with_named_zone
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Carnival Mobile
8
+ - Arthur Evstifeev
9
+ - Artur Khantimirov
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2014-10-20 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: mongoid
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: '3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '3'
29
+ - !ruby/object:Gem::Dependency
30
+ name: bundler
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: '1.7'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '1.7'
43
+ - !ruby/object:Gem::Dependency
44
+ name: rake
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '10.0'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '10.0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: rspec
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: 3.1.0
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: 3.1.0
71
+ description: 'This gem works transparently with ActiveSupport::TimeWithZone Times.
72
+ See more: https://github.com/carnivalmobile/time-with-zone'
73
+ email:
74
+ - toby@carnivallabs.com
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - ".gitignore"
80
+ - Gemfile
81
+ - LICENSE
82
+ - README.md
83
+ - Rakefile
84
+ - lib/mongoid-time_with_named_zone.rb
85
+ - lib/mongoid/time_with_named_zone.rb
86
+ - lib/mongoid/time_with_named_zone/version.rb
87
+ - mongoid-time_with_named_zone.gemspec
88
+ - spec/time_with_zone_spec.rb
89
+ homepage: https://github.com/carnivalmobile/time-with-zone
90
+ licenses:
91
+ - Apache 2
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - "/Users/Caleb/Projects/time-with-zone/lib"
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.2.2
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: A Mongoid wrapper for Time objects that retains the timezone name
113
+ test_files:
114
+ - spec/time_with_zone_spec.rb