gb_date 1.0.0

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: 65cefc159a0e11ca42654ef5e944f0563f1c8dbb
4
+ data.tar.gz: 41a5848ac1646faf4c28ecbf08b53bfb3e7a8924
5
+ SHA512:
6
+ metadata.gz: b4f45e8afbe2e23c632998a28d69c0d7f146ba51f25783feee238e6fdfb9a9cecc05d3a14adb1deac64d29f4d6c066d40d30d8b88268b87114834ed45b5a1e88
7
+ data.tar.gz: a7488e82e4115f439f3885918f3d4043d223fbb2c56fbbcc7aef96e0a8e0b89543d793444cb1f86c16633aadcd8dad3e5e5fd19a2b52359e2649f497d93a8785
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.idea/
11
+ /bin/console
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gb_date.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Kacper Kawecki
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.
data/README.md ADDED
@@ -0,0 +1,113 @@
1
+ # GBDate
2
+
3
+ GBDate is a gem extending rails to accept dates as float or numeric.
4
+ It means that http client can send date or time in format of `1732131232.236732` instead of a date string.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'gb_date'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install gb_date
21
+
22
+ ## Usage
23
+
24
+ Basic usage is out of a box inserted into rails.
25
+ On top of that you get few extensions.
26
+
27
+ ```ruby
28
+ object = MyModel.create date_field: '1458068508.965463'
29
+ puts object.date_field
30
+ ```
31
+ Will result with `2016-03-15 19:01:48 UTC`
32
+
33
+ ### Api
34
+
35
+ For api use, default time are extended with following methods:
36
+
37
+ ```ruby
38
+ Time.now.to_api_decimal
39
+ ```
40
+ Returns `BigDecimal` with precision to microseconds
41
+
42
+ ```ruby
43
+ Time.now.to_api_f
44
+ ```
45
+ Returns `Float` with precision to microseconds
46
+
47
+ ```ruby
48
+ Time.now.to_api_s
49
+ ```
50
+ Returns `String` representation of decimal number with precision to microseconds
51
+
52
+ ### Numeric
53
+
54
+ You can convert numbers to Time
55
+
56
+ ```ruby
57
+ 1234343.to_time
58
+ 1234343.232.to_time
59
+ ```
60
+ with time zone
61
+
62
+ ```ruby
63
+ 1234343.in_time_zone(zone)
64
+ ```
65
+ or Date
66
+
67
+ ```ruby
68
+ 1234343.to_date
69
+ ```
70
+
71
+ or DateTime
72
+
73
+ ```ruby
74
+ 1234343.to_datetime
75
+ ```
76
+
77
+ ### Date
78
+
79
+ Convert to beginning of date in utc time
80
+
81
+ ```ruby
82
+ Date.today.to_utc_time
83
+ ```
84
+
85
+ ### Time
86
+
87
+ Convert to DateTime
88
+
89
+ ```ruby
90
+ Time.now.to_datetime
91
+ ```
92
+
93
+ Convert to BigDecimal
94
+
95
+ ```ruby
96
+ Time.now.to_decimal
97
+ ```
98
+
99
+ ## Development
100
+
101
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
102
+
103
+ 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).
104
+
105
+ ## Contributing
106
+
107
+ Bug reports and pull requests are welcome on GitHub at https://github.com/GenieBelt/gb-date-rails.
108
+
109
+
110
+ ## License
111
+
112
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
113
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "gb_date"
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
data/gb_date.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gb_date/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'gb_date'
8
+ spec.version = GBDate::VERSION
9
+ spec.authors = ['Kacper Kawecki']
10
+ spec.email = ['kacper@geniebelt.com']
11
+
12
+ spec.summary = %q{Add to rails parsing date and time from float or integer}
13
+ spec.description = %q{Add to rails parsing date and time from float or integer}
14
+ spec.homepage = 'https://github.com/GenieBelt/gb-date-rails'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_dependency 'activerecord', '>= 4.0'
24
+ spec.add_dependency 'activesupport', '>= 4.0'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.10'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.4'
29
+ spec.add_development_dependency 'sqlite3'
30
+ spec.add_development_dependency 'pg'
31
+ end
@@ -0,0 +1,10 @@
1
+ require 'active_record'
2
+ module ActiveRecord
3
+ module ConnectionAdapters
4
+ class Column
5
+ module Format
6
+ NUMERIC_TIME = /\A\d+(\.\d+)?\z/
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,23 @@
1
+ require 'gb_date/active_record/connection_adapters/column'
2
+ module GBDate
3
+ module ActiveRecord
4
+ module Type
5
+ module Date
6
+ def fast_string_to_date(string)
7
+ if string =~ ::ActiveRecord::ConnectionAdapters::Column::Format::NUMERIC_TIME
8
+ ::Time.at(string.to_f).utc.to_date
9
+ else
10
+ super(string)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ begin
19
+ require 'active_record/type/date'
20
+ ActiveRecord::Type::Date.send :prepend, GBDate::ActiveRecord::Type::Date
21
+ rescue
22
+ puts 'Cannot load rails Date extension'
23
+ end
@@ -0,0 +1,33 @@
1
+ require 'gb_date/active_record/connection_adapters/column'
2
+ module GBDate
3
+ module ActiveRecord
4
+ module Type
5
+ module DateTime
6
+ def cast_value(value)
7
+ return ::Time.at(value) if value.is_a? Numeric
8
+ super value
9
+ end
10
+
11
+ def type_cast_for_database(value)
12
+ value = ::Time.at(value) if value.is_a? Numeric
13
+ super value
14
+ end
15
+
16
+ def fast_string_to_time(string)
17
+ if string =~ ::ActiveRecord::ConnectionAdapters::Column::Format::NUMERIC_TIME
18
+ ::Time.at(string.to_f)
19
+ else
20
+ super string
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ begin
28
+ ActiveRecord::Type::DateTime.send :prepend, GBDate::ActiveRecord::Type::DateTime
29
+ require 'active_record/connection_adapters/postgresql_adapter'
30
+ ActiveRecord::ConnectionAdapters::PostgreSQL::OID::DateTime.send :prepend, GBDate::ActiveRecord::Type::DateTime
31
+ rescue Exception => e
32
+ puts e.message
33
+ end
@@ -0,0 +1,37 @@
1
+ require 'gb_date/active_record/connection_adapters/column'
2
+ module GBDate
3
+ module ActiveRecord
4
+ module Type
5
+ module Time
6
+ SECONDS_A_DAY = 86400
7
+ DISTANCE_TO_DEFAULT_DAY = 946684800
8
+ def cast_value(value)
9
+ return ::Time.at(calculate_float_value value) if value.is_a? Numeric
10
+ if value.is_a?(::String) && value =~ ::ActiveRecord::ConnectionAdapters::Column::Format::NUMERIC_TIME
11
+ return ::Time.at(calculate_float_value value)
12
+ end
13
+ super value
14
+ end
15
+
16
+ def fast_string_to_time(string)
17
+ if string =~ ::ActiveRecord::ConnectionAdapters::Column::Format::NUMERIC_TIME
18
+ ::Time.at(string.to_f)
19
+ else
20
+ super string
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def calculate_float_value(value)
27
+ DISTANCE_TO_DEFAULT_DAY + ( value.to_f % 86400.0)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ begin
34
+ ActiveRecord::Type::Time.send :prepend, GBDate::ActiveRecord::Type::Time
35
+ rescue
36
+ puts 'Cannot load rails Time extension'
37
+ end
@@ -0,0 +1,17 @@
1
+ class Date
2
+ # @return [Time]
3
+ def to_utc_time
4
+ begin
5
+ self.to_time(:utc)
6
+ rescue
7
+ self.to_time + self.to_time.utc_offset
8
+ end
9
+ end
10
+ end
11
+
12
+ class DateTime
13
+ # @return [Time]
14
+ def to_utc_time
15
+ self.to_time.utc
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ class Numeric
2
+ def to_date
3
+ Time.at(self).utc.to_date
4
+ end
5
+
6
+ def to_time
7
+ Time.at(self).utc
8
+ end
9
+
10
+ def to_datetime
11
+ Time.at(self).utc.to_datetime
12
+ end
13
+
14
+ def in_time_zone(*args)
15
+ Time.at(self).in_time_zone(*args)
16
+ end
17
+ end
@@ -0,0 +1,26 @@
1
+ require 'gb_date/active_record/connection_adapters/column'
2
+ module GBDate
3
+ module String
4
+ module Time
5
+ def to_time
6
+ if self =~ ::ActiveRecord::ConnectionAdapters::Column::Format::NUMERIC_TIME
7
+ ::Time.at(self.to_f)
8
+ else
9
+ super
10
+ end
11
+ end
12
+
13
+ def in_time_zone(zone = ::Time.zone)
14
+ if zone && self =~ ::ActiveRecord::ConnectionAdapters::Column::Format::NUMERIC_TIME
15
+ ::Time.find_zone!(zone).at(self.to_f)
16
+ else
17
+ super
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ class String
25
+ prepend GBDate::String::Time
26
+ end
@@ -0,0 +1,37 @@
1
+ class Time
2
+ # Seconds as BigDecimal
3
+ # @return [BigDecimal]
4
+ def to_decimal
5
+ BigDecimal.new("#{self.to_i}.#{sprintf('%09d',self.nsec)}")
6
+ end
7
+
8
+ # Seconds as BigDecimal with precision to microseconds
9
+ # @return [BigDecimal]
10
+ def to_api_decimal
11
+ BigDecimal.new("#{self.to_i}.#{sprintf('%06d',self.nsec/1000)}")
12
+ end
13
+
14
+ # Seconds as Float with precision to microseconds
15
+ # @return [BigDecimal]
16
+ def to_api_f
17
+ self.to_api_decimal.to_f
18
+ end
19
+
20
+ # Return string with date represented by seconds with precision to microseconds
21
+ # @return [String]
22
+ def to_api_s
23
+ sprintf('%.6f', to_api_decimal)
24
+ end
25
+
26
+ # Converts time to DateTime
27
+ # @return DateTime
28
+ def to_datetime
29
+ # Convert seconds + microseconds into a fractional number of seconds
30
+ seconds = sec + Rational(usec, 10**6)
31
+
32
+ # Convert a UTC offset measured in minutes to one measured in a
33
+ # fraction of a day.
34
+ offset = Rational(utc_offset, 60 * 60 * 24)
35
+ DateTime.new(year, month, day, hour, min, seconds, offset)
36
+ end
37
+ end
@@ -0,0 +1,28 @@
1
+ require 'active_support'
2
+ class ActiveSupport::TimeWithZone
3
+
4
+ # Seconds as BigDecimal
5
+ # With precision to nanoseconds
6
+ # @return [BigDecimal]
7
+ def to_decimal
8
+ BigDecimal.new("#{self.to_i}.#{sprintf('%09d',self.nsec)}")
9
+ end
10
+
11
+ # Seconds as BigDecimal with precision to microseconds
12
+ # @return [BigDecimal]
13
+ def to_api_decimal
14
+ BigDecimal.new("#{self.to_i}.#{sprintf('%06d',self.nsec/1000)}")
15
+ end
16
+
17
+ # Seconds as Float with precision to microseconds
18
+ # @return [BigDecimal]
19
+ def to_api_f
20
+ self.to_api_decimal.to_f
21
+ end
22
+
23
+ # Return string with date represented by seconds with precision to microseconds
24
+ # @return [String]
25
+ def to_api_s
26
+ sprintf('%.6f', self.to_api_decimal)
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module GBDate
2
+ VERSION = '1.0.0'
3
+ end
data/lib/gb_date.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'gb_date/version'
2
+ require 'gb_date/active_record/connection_adapters/column'
3
+ require 'gb_date/active_record/type/date'
4
+ require 'gb_date/active_record/type/date_time'
5
+ require 'gb_date/active_record/type/time'
6
+ require 'gb_date/date'
7
+ require 'gb_date/numeric'
8
+ require 'gb_date/string'
9
+ require 'gb_date/time'
10
+ require 'gb_date/time_with_zone'
11
+
12
+ module GBDate
13
+
14
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gb_date
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kacper Kawecki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
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: activesupport
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '3.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '3.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
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
+ - !ruby/object:Gem::Dependency
98
+ name: pg
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Add to rails parsing date and time from float or integer
112
+ email:
113
+ - kacper@geniebelt.com
114
+ executables:
115
+ - console
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - .gitignore
120
+ - .rspec
121
+ - .travis.yml
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - gb_date.gemspec
128
+ - lib/gb_date.rb
129
+ - lib/gb_date/active_record/connection_adapters/column.rb
130
+ - lib/gb_date/active_record/type/date.rb
131
+ - lib/gb_date/active_record/type/date_time.rb
132
+ - lib/gb_date/active_record/type/time.rb
133
+ - lib/gb_date/date.rb
134
+ - lib/gb_date/numeric.rb
135
+ - lib/gb_date/string.rb
136
+ - lib/gb_date/time.rb
137
+ - lib/gb_date/time_with_zone.rb
138
+ - lib/gb_date/version.rb
139
+ homepage: https://github.com/GenieBelt/gb-date-rails
140
+ licenses:
141
+ - MIT
142
+ metadata: {}
143
+ post_install_message:
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - '>='
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - '>='
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 2.4.5
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: Add to rails parsing date and time from float or integer
163
+ test_files: []