has_zone 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +39 -0
- data/Rakefile +7 -0
- data/has_zone.gemspec +27 -0
- data/lib/has_zone.rb +3 -0
- data/lib/has_zone/has_zone.rb +42 -0
- data/lib/has_zone/tzinfo.rb +60 -0
- data/lib/has_zone/version.rb +3 -0
- data/spec/has_zone_spec.rb +47 -0
- data/spec/spec_helper.rb +7 -0
- metadata +144 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 53371fa0b22046704af05bf67038f0d12b06965c
|
4
|
+
data.tar.gz: be7aff3caed35d3c9abb5f552a5f969803e2911f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0f49581efb0258ad9566ebfd07dfbe505f7b08195c4728eb702b31a4a6f20b9e204e68357a462c4192c23aad23baf5ec76ac807e19b2fa6ea84baf56783bda8f
|
7
|
+
data.tar.gz: ca81dbaf1c9ed288391f3d9955f41ee805958231a5b5c470c42136900ff4c077ec67ec292f8515455bc86c3df31ffc8aecd4916b3cb90bc052c34549b97d64dd
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jerry Luk
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# HasZone
|
2
|
+
|
3
|
+
Provides a way to convert from TZinfo time zone identifer to ActiveSupport TimeZone for your Rails application.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'has_zone'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install has_zone
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Assuming you have an attribute time_zone in your User model:
|
22
|
+
|
23
|
+
class User < ActiveRecord::Base
|
24
|
+
include HasZone
|
25
|
+
has_zone with: time_zone
|
26
|
+
end
|
27
|
+
|
28
|
+
Then you can use the time zone with:
|
29
|
+
|
30
|
+
user.time_zone = "America/Los_Angeles"
|
31
|
+
user.zone # returns ActiveSupport::TimeZone of "Pacific Time (US & Canada)"
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/has_zone.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'has_zone/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "has_zone"
|
8
|
+
spec.version = HasZone::VERSION
|
9
|
+
spec.authors = ["Jerry Luk"]
|
10
|
+
spec.email = ["jerryluk@gmail.com"]
|
11
|
+
spec.description = %q{Provides a way to convert from TZinfo time zone identifer to ActiveSupport TimeZone for your Rails application.}
|
12
|
+
spec.summary = %q{Provides a way to convert from TZinfo time zone identifer to ActiveSupport TimeZone for your Rails application.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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 "activesupport", ">= 4.0"
|
22
|
+
spec.add_dependency "activemodel", ">= 4.0"
|
23
|
+
spec.add_dependency "tzinfo"
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
end
|
data/lib/has_zone.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
module HasZone
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
|
6
|
+
validates_inclusion_of :time_zone, in: ::ActiveSupport::TimeZone.tzids_map.keys, allow_blank: true, if: :time_zone_changed?
|
7
|
+
|
8
|
+
# This allows the model has the ability to store TimeZone.
|
9
|
+
# Usage:
|
10
|
+
# has_zone with: :time_zone
|
11
|
+
#
|
12
|
+
# It defines the following methods:
|
13
|
+
#
|
14
|
+
# :zone
|
15
|
+
# Public: Returns the Rails Time Zone.
|
16
|
+
#
|
17
|
+
#
|
18
|
+
# :zone=(identifier)
|
19
|
+
# Public: Sets the TimeZone by TZinfo identifier.
|
20
|
+
#
|
21
|
+
# identifier - Time Zone identifier
|
22
|
+
#
|
23
|
+
def self.has_zone(options = { with: :time_zone })
|
24
|
+
define_method(:zone) do
|
25
|
+
return nil if send(options[:with]).nil?
|
26
|
+
@zone ||= ::ActiveSupport::TimeZone.tzids_map[send(options[:with])].try(:name)
|
27
|
+
end
|
28
|
+
|
29
|
+
define_method(:zone=) do |tz|
|
30
|
+
zone = tz.is_a?(String) ? ::ActiveSupport::TimeZone.new(tz) : tz
|
31
|
+
send "#{options[:with]}=".to_sym, zone.try(:identifier)
|
32
|
+
end
|
33
|
+
|
34
|
+
define_method("#{options[:with]}=".to_sym) do |tz|
|
35
|
+
# Since we memoize the time zone, we need to reset it if the time zone has changed
|
36
|
+
@zone = nil
|
37
|
+
write_attribute(options[:with], tz)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'tzinfo'
|
2
|
+
|
3
|
+
module TZInfoExtension
|
4
|
+
module ClassMethods
|
5
|
+
def abbreviations_map
|
6
|
+
@abbreviations_map ||= begin
|
7
|
+
(abbreviations_map = {}).tap do |am|
|
8
|
+
all.each do |tz|
|
9
|
+
tz.abbreviations.each do |abb|
|
10
|
+
if am[abb].present?
|
11
|
+
am[abb] << tz
|
12
|
+
else
|
13
|
+
am[abb] = [tz]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
module InstanceMethods
|
22
|
+
def next_period(period)
|
23
|
+
period_end = period.utc_end
|
24
|
+
period_end ? period_for_utc(period_end + 1.day) : nil
|
25
|
+
end
|
26
|
+
|
27
|
+
def abbreviations
|
28
|
+
date = Time.now.utc.beginning_of_year
|
29
|
+
abbreviations = []
|
30
|
+
period = period_for_local(date)
|
31
|
+
while not(abbreviations.include? period.abbreviation)
|
32
|
+
abbreviations << period.abbreviation
|
33
|
+
break unless (period = next_period(period))
|
34
|
+
end
|
35
|
+
abbreviations
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class TZInfo::Timezone
|
41
|
+
extend TZInfoExtension::ClassMethods
|
42
|
+
include TZInfoExtension::InstanceMethods
|
43
|
+
end
|
44
|
+
|
45
|
+
class ActiveSupport::TimeZone
|
46
|
+
extend TZInfoExtension::ClassMethods
|
47
|
+
include TZInfoExtension::InstanceMethods
|
48
|
+
|
49
|
+
def self.tzids_map
|
50
|
+
@tzids_map ||= begin
|
51
|
+
(tzids_map = {}).tap do |tm|
|
52
|
+
zones_map.each { |n, z| tm[z.identifier] ||= z }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def identifier
|
58
|
+
self.tzinfo.try(:identifier)
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HasZone do
|
4
|
+
class Resource
|
5
|
+
include ActiveModel::Validations
|
6
|
+
include HasZone
|
7
|
+
attr_accessor :time_zone
|
8
|
+
has_zone with: :time_zone
|
9
|
+
|
10
|
+
def write_attribute(attribute, value)
|
11
|
+
instance_variable_set("@#{attribute}".to_sym, value)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:resource) { Resource.new }
|
16
|
+
|
17
|
+
it "returns nil if time_zone is nil" do
|
18
|
+
resource.time_zone = nil
|
19
|
+
resource.zone.should be_nil
|
20
|
+
end
|
21
|
+
|
22
|
+
it "returns TimeZone if time_zone is not nil" do
|
23
|
+
resource.time_zone = "America/New_York"
|
24
|
+
resource.zone.should_not be_nil
|
25
|
+
end
|
26
|
+
|
27
|
+
it "returns updated TimeZone if time_zone is updated" do
|
28
|
+
resource.time_zone = "America/New_York"
|
29
|
+
expect {
|
30
|
+
resource.time_zone = "America/Los_Angeles"
|
31
|
+
}.to change(resource, :zone)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "sets TimeZone from string" do
|
35
|
+
resource.zone = "Pacific Time (US & Canada)"
|
36
|
+
resource.zone.should == ActiveSupport::TimeZone.new("Pacific Time (US & Canada)").name
|
37
|
+
resource.time_zone.should == "America/Los_Angeles"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "sets TimeZone from TimeZone" do
|
41
|
+
zone = ActiveSupport::TimeZone.new("Pacific Time (US & Canada)")
|
42
|
+
resource.zone = zone
|
43
|
+
resource.zone.should == "Pacific Time (US & Canada)"
|
44
|
+
resource.time_zone.should == "America/Los_Angeles"
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: has_zone
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jerry Luk
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activemodel
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: tzinfo
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Provides a way to convert from TZinfo time zone identifer to ActiveSupport
|
98
|
+
TimeZone for your Rails application.
|
99
|
+
email:
|
100
|
+
- jerryluk@gmail.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- .gitignore
|
106
|
+
- Gemfile
|
107
|
+
- LICENSE.txt
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- has_zone.gemspec
|
111
|
+
- lib/has_zone.rb
|
112
|
+
- lib/has_zone/has_zone.rb
|
113
|
+
- lib/has_zone/tzinfo.rb
|
114
|
+
- lib/has_zone/version.rb
|
115
|
+
- spec/has_zone_spec.rb
|
116
|
+
- spec/spec_helper.rb
|
117
|
+
homepage: ''
|
118
|
+
licenses:
|
119
|
+
- MIT
|
120
|
+
metadata: {}
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - '>='
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - '>='
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 2.0.5
|
138
|
+
signing_key:
|
139
|
+
specification_version: 4
|
140
|
+
summary: Provides a way to convert from TZinfo time zone identifer to ActiveSupport
|
141
|
+
TimeZone for your Rails application.
|
142
|
+
test_files:
|
143
|
+
- spec/has_zone_spec.rb
|
144
|
+
- spec/spec_helper.rb
|