local_date_time_attributes 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2c32b770854306fb5b91ca41a48c0452657dc7f64753c9db4894858d4b9d03f8
4
+ data.tar.gz: 5a68e821cb30fef498e1a50c9e97af4a4a54e2df5da7ab9866acf7bb718e3b11
5
+ SHA512:
6
+ metadata.gz: 89e8bbb4feee7621cba35ec4f83e84e8a01a4dd46e389afdad15d254a087d6b49acfea5891e64b875bf63339b437cce2ded224f3076fc850927b9f6b90b4e97e
7
+ data.tar.gz: 241ffa8f7852b5020c2cbe15de9618c9132bdce0c69c91c4e2a0349bfcfcbb35f6beb36e69e8dfcf141d8346bba837c293f317a27ca273799e36a6367a28c6ad
@@ -0,0 +1,30 @@
1
+ name: Ruby Gem
2
+
3
+ on:
4
+ push:
5
+ branches: [ master ]
6
+ pull_request:
7
+ branches: [ master ]
8
+
9
+ jobs:
10
+ build:
11
+ name: Build + Publish
12
+ runs-on: ubuntu-latest
13
+
14
+ steps:
15
+ - uses: actions/checkout@v2
16
+ - name: Set up Ruby 2.6
17
+ uses: actions/setup-ruby@v1
18
+ with:
19
+ version: 2.6.x
20
+
21
+ - name: Publish to RubyGems
22
+ run: |
23
+ mkdir -p $HOME/.gem
24
+ touch $HOME/.gem/credentials
25
+ chmod 0600 $HOME/.gem/credentials
26
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
27
+ gem build *.gemspec
28
+ gem push *.gem
29
+ env:
30
+ GEM_HOST_API_KEY: ${{secrets.RUBYGEMS_AUTH_TOKEN}}
@@ -0,0 +1,24 @@
1
+ name: Ruby
2
+
3
+ on:
4
+ push:
5
+ branches: [ master ]
6
+ pull_request:
7
+ branches: [ master ]
8
+
9
+ jobs:
10
+ build:
11
+
12
+ runs-on: ubuntu-latest
13
+
14
+ steps:
15
+ - uses: actions/checkout@v2
16
+ - name: Set up Ruby 2.6
17
+ uses: actions/setup-ruby@v1
18
+ with:
19
+ ruby-version: 2.6.x
20
+ - name: Build and test with Rake
21
+ run: |
22
+ gem install bundler
23
+ bundle install --jobs 4 --retry 3
24
+ bundle exec rake
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor/
10
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --require spec_helper
2
+ --color
3
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in local_date_time_attributes.gemspec
6
+ gemspec
@@ -0,0 +1,58 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ local_date_time_attributes (0.1.0)
5
+ activerecord (>= 5.0)
6
+ activesupport (>= 5.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (6.0.2.2)
12
+ activesupport (= 6.0.2.2)
13
+ activerecord (6.0.2.2)
14
+ activemodel (= 6.0.2.2)
15
+ activesupport (= 6.0.2.2)
16
+ activesupport (6.0.2.2)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ zeitwerk (~> 2.2)
22
+ concurrent-ruby (1.1.6)
23
+ diff-lcs (1.3)
24
+ i18n (1.8.2)
25
+ concurrent-ruby (~> 1.0)
26
+ minitest (5.14.0)
27
+ rake (13.0.1)
28
+ rspec (3.9.0)
29
+ rspec-core (~> 3.9.0)
30
+ rspec-expectations (~> 3.9.0)
31
+ rspec-mocks (~> 3.9.0)
32
+ rspec-core (3.9.1)
33
+ rspec-support (~> 3.9.1)
34
+ rspec-expectations (3.9.1)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.9.0)
37
+ rspec-mocks (3.9.1)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.9.0)
40
+ rspec-support (3.9.2)
41
+ thread_safe (0.3.6)
42
+ timecop (0.9.1)
43
+ tzinfo (1.2.6)
44
+ thread_safe (~> 0.1)
45
+ zeitwerk (2.3.0)
46
+
47
+ PLATFORMS
48
+ ruby
49
+
50
+ DEPENDENCIES
51
+ bundler (~> 2.0)
52
+ local_date_time_attributes!
53
+ rake (~> 13.0)
54
+ rspec
55
+ timecop
56
+
57
+ BUNDLED WITH
58
+ 2.1.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 azohoori
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,71 @@
1
+ # LocalDateTimeAttributes
2
+
3
+ ![Ruby](https://github.com/Cisco-AMP/local_date_time_attributes/workflows/Ruby/badge.svg) ![Ruby Gem](https://github.com/Cisco-AMP/local_date_time_attributes/workflows/Ruby%20Gem/badge.svg) [![Gem Version](https://badge.fury.io/rb/local_date_time_attributes.svg)](https://badge.fury.io/rb/local_date_time_attributes)
4
+
5
+ Leverages the active_record attributes api to define a custom active_record type `local_date_time`. Normally, active_record will convert a Time,DateTime,Date which has a timezone into the active_record's base timezone - performing conversion of the time in the process. This custom attribute will ignore the timezone component of the `Time` attribute.
6
+
7
+ Sometimes it is preferrable to ignore the timezone defined by [Time.zone](https://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html) before persisting or loading from persistence. In otherwords, the time_zone component is ignored prior to saving to the database and after loading from database
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'local_date_time_attributes'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install local_date_time_attributes
24
+
25
+
26
+ ## Usage
27
+
28
+ On an active_record model, just add an attribute with the type `:local_date_time`
29
+
30
+ class Interview < ApplicationRecord
31
+ attribute :start_at, :local_date_time
32
+ end
33
+
34
+ We will have a date_time attribute on ProductUpdate.start_at that ignores timezone prior to persisting and after loading
35
+
36
+
37
+
38
+ $ Time.zone = 'Eastern Time (US & Canada)'
39
+ => "Eastern Time (US & Canada)"
40
+
41
+ # active_record is setup to use a different timezone
42
+ $ ActiveRecord::Base.default_timezone
43
+ => :utc
44
+
45
+ $ time = Time.zone.local(2007, 2, 10, 15, 30, 45)
46
+ => Sat, 10 Feb 2007 15:30:45 EST -05:00
47
+
48
+ # Timezone is ignored when saving to a database
49
+ $ Interview.new(start_at: time).save
50
+ (0.4ms) BEGIN
51
+ SQL (4.7ms) UPDATE `interviews` SET `start_at` = '2007-02-10 15:30:45', `updated_at` = '2020-03-25 18:06:11' WHERE `product_updates`.`id` = 1
52
+ (1.5ms) COMMIT
53
+
54
+ $ interview = Interview.first
55
+
56
+ $ interview.start_at
57
+ => Sat, 10 Feb 2007 15:30:45 EST -05:00
58
+
59
+ # timezone is ignored when loading
60
+ $ Time.use_zone('Asia/Singapore') { product_update.reload.start_at }
61
+ => Sat, 10 Feb 2007 15:30:45 +08 +08:00
62
+
63
+ ## Development
64
+
65
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
66
+
67
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
68
+
69
+ ## Contributing
70
+
71
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Cisco-AMP/local_date_time_attributes.
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ begin
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ rescue LoadError
6
+ end
7
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "local_date_time_attributes"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,9 @@
1
+ require "local_date_time_attributes/version"
2
+ require 'delegate'
3
+ require 'active_support'
4
+ require 'active_support/time'
5
+ require 'active_support/time_with_zone'
6
+
7
+ require 'local_date_time_attributes/local_date_time'
8
+ require 'local_date_time_attributes/local_date_time_type'
9
+ require 'local_date_time_attributes/railtie' if defined?(Rails)
@@ -0,0 +1,28 @@
1
+ require 'delegate'
2
+ require 'active_support'
3
+ require 'active_support/time'
4
+ require 'active_support/time_with_zone'
5
+ module LocalDateTimeAttributes
6
+ class LocalDateTime < SimpleDelegator
7
+ def initialize(date_time)
8
+ super(from_local(date_time))
9
+ end
10
+
11
+ # Returns a datetime in the timezone specified without changing the time
12
+ def to_local(time_zone = Time.zone.try(:name))
13
+ return if __getobj__.nil? || time_zone.nil?
14
+ ActiveSupport::TimeZone.new(time_zone).local_to_utc(__getobj__).in_time_zone(time_zone)
15
+ end
16
+
17
+ private
18
+
19
+ def from_local(date_time)
20
+ return if date_time.nil?
21
+ ActiveSupport::TimeZone.new(active_record_timezone).local_to_utc(date_time).in_time_zone(active_record_timezone)
22
+ end
23
+
24
+ def active_record_timezone
25
+ ActiveRecord::Base.default_timezone == :UTC ? 'UTC' : Rails.configuration.time_zone
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,20 @@
1
+ require 'active_record/type'
2
+ module LocalDateTimeAttributes
3
+ class LocalDateTimeType < ActiveRecord::Type::DateTime
4
+ def cast(value)
5
+ if value.instance_of? LocalDateTime
6
+ return value
7
+ elsif !value.nil?
8
+ LocalDateTime.new(value)
9
+ end
10
+ end
11
+
12
+ def serialize(value)
13
+ value.try(:__getobj__)
14
+ end
15
+
16
+ def deserialize(value)
17
+ cast(value).try(:to_local)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,8 @@
1
+ require 'local_date_time_attributes/local_date_time_type'
2
+ module LocalDateTimeAttributes
3
+ class Railtie < Rails::Railtie
4
+ initializer "local_date_time_attributes.types" do
5
+ ActiveRecord::Type.register(:local_date_time, LocalDateTimeAttributes::LocalDateTimeType)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module LocalDateTimeAttributes
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,43 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "local_date_time_attributes/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "local_date_time_attributes"
8
+ spec.version = LocalDateTimeAttributes::VERSION
9
+ spec.authors = ["Samson Nguyen"]
10
+ spec.email = ["samsnguy@cisco.com"]
11
+
12
+ spec.summary = %q{ ActiveRecord attributes api to define a DateTime attribute that ignores Time.zone}
13
+ spec.description = %q{Introduces a custom ActiveRecord attribute type which will ignore the timezone set by Time.zone. This is useful in the event we are modifying the current thread timezone}
14
+ spec.homepage = "https://github.com/Cisco-AMP/local_date_time_attributes"
15
+
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ if spec.respond_to?(:metadata)
18
+ #spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
19
+
20
+ #spec.metadata["homepage_uri"] = spec.homepage
21
+ #spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
22
+ #spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against " \
25
+ "public gem pushes."
26
+ end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib"]
36
+
37
+ spec.add_development_dependency 'bundler', '~>2.0'
38
+ spec.add_development_dependency 'rake', '~>13.0'
39
+ spec.add_development_dependency 'timecop'
40
+ spec.add_development_dependency 'rspec'
41
+ spec.add_dependency 'activesupport', '>= 5.0'
42
+ spec.add_dependency 'activerecord', '>= 5.0'
43
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: local_date_time_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Samson Nguyen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-03-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: timecop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activerecord
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '5.0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '5.0'
97
+ description: Introduces a custom ActiveRecord attribute type which will ignore the
98
+ timezone set by Time.zone. This is useful in the event we are modifying the current
99
+ thread timezone
100
+ email:
101
+ - samsnguy@cisco.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".github/workflows/gempush.yml"
107
+ - ".github/workflows/ruby.yml"
108
+ - ".gitignore"
109
+ - ".rspec"
110
+ - Gemfile
111
+ - Gemfile.lock
112
+ - LICENSE.txt
113
+ - README.md
114
+ - Rakefile
115
+ - bin/console
116
+ - bin/setup
117
+ - lib/local_date_time_attributes.rb
118
+ - lib/local_date_time_attributes/local_date_time.rb
119
+ - lib/local_date_time_attributes/local_date_time_type.rb
120
+ - lib/local_date_time_attributes/railtie.rb
121
+ - lib/local_date_time_attributes/version.rb
122
+ - local_date_time_attributes.gemspec
123
+ homepage: https://github.com/Cisco-AMP/local_date_time_attributes
124
+ licenses: []
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubygems_version: 3.0.3
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: ActiveRecord attributes api to define a DateTime attribute that ignores Time.zone
145
+ test_files: []