timely 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6fd5bf30099265da88c4957079fd58a4db290fac8cd4ad239ddb395be546d5d3
4
- data.tar.gz: d263dca100292aae36f54b22cc2f3b1eebc1c1df0274fc3ef730ce991fc94c4a
3
+ metadata.gz: 23be3863188d0d617f9cb4c6d7aca28605d52e9e3a62cadb2935253395210f1c
4
+ data.tar.gz: 5b161da1238c424053b255cfb96e28dd3c9bd52e3846f1aa2ee602ccad52371d
5
5
  SHA512:
6
- metadata.gz: 784aede0c406afe8e0213f275150ee3bba930969c28acf9ac2e992a96ce42c7f30e042a6e0ae9779dc17f86e6a7744c4b0a07bf72e1d326d6e6176b3ac4b28c4
7
- data.tar.gz: a780a85c02adcf074c53376c7995739d609329256b76c370966bedaccf953e0e3cb7607cb0d381873a8a4585b48505c8d2d04f68bebdc94f0b49c947bab84d28
6
+ metadata.gz: 97e1202d4c04236c82e445c1cab715a96b68f862370d89eefb542789d621e7cf2249389a47d2a195df412fc328c97ef1e35ec5d77532549d3ec6152d77e47d5a
7
+ data.tar.gz: 5de7656ed5ecb847307412497c7149696d7e349120c933a3749ebffb9462e9bccc08573ddc62fd039f650d7d6993ef998274ce783ad75215eb12493fa31eeac0
data/.travis.yml CHANGED
@@ -1,6 +1,7 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 2.6
4
+ - 2.7
4
5
  matrix:
5
6
  fast_finish: true
6
7
  before_install:
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.7.0
4
+
5
+ * [TT-6441] Due to TimeDifference being unmaintained bring it into the timely library
6
+
3
7
  ## 0.6.0
4
8
 
5
9
  * [TT-6402] Require date group weekdays bit field to be not null/nil
@@ -4,5 +4,5 @@ source 'https://rubygems.org'
4
4
  gemspec path: '../'
5
5
 
6
6
  group :development, :test do
7
- gem 'activerecord', '~> 6.0.0.rc1'
7
+ gem 'activerecord', '~> 6.0'
8
8
  end
data/lib/timely/rails.rb CHANGED
@@ -12,3 +12,4 @@ require 'timely/rails/date_time'
12
12
  require 'timely/rails/date'
13
13
  require 'timely/rails/period'
14
14
  require 'timely/rails/time'
15
+ require 'timely/rails/time_difference'
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubygems'
4
+ require 'active_support/all'
5
+
6
+ module Timely
7
+ class TimeDifference
8
+ private_class_method :new
9
+
10
+ TIME_COMPONENTS = %i[years months weeks days hours minutes seconds].freeze
11
+
12
+ def self.between(start_time, end_time)
13
+ new(start_time, end_time)
14
+ end
15
+
16
+ def in_years
17
+ in_component(:years)
18
+ end
19
+
20
+ def in_months
21
+ (@time_diff / (1.day * 30.42)).round(2)
22
+ end
23
+
24
+ def in_weeks
25
+ in_component(:weeks)
26
+ end
27
+
28
+ def in_days
29
+ in_component(:days)
30
+ end
31
+
32
+ def in_hours
33
+ in_component(:hours)
34
+ end
35
+
36
+ def in_minutes
37
+ in_component(:minutes)
38
+ end
39
+
40
+ def in_seconds
41
+ @time_diff
42
+ end
43
+
44
+ def in_each_component
45
+ Hash[TIME_COMPONENTS.map do |time_component|
46
+ [time_component, public_send("in_#{time_component}")]
47
+ end]
48
+ end
49
+
50
+ def in_general
51
+ remaining = @time_diff
52
+ Hash[TIME_COMPONENTS.map do |time_component|
53
+ if remaining > 0
54
+ rounded_time_component = (remaining / 1.send(time_component).seconds).round(2).floor
55
+ remaining -= rounded_time_component.send(time_component)
56
+ [time_component, rounded_time_component]
57
+ else
58
+ [time_component, 0]
59
+ end
60
+ end]
61
+ end
62
+
63
+ def humanize
64
+ diff_parts = []
65
+ in_general.each do |part, quantity|
66
+ next if quantity <= 0
67
+
68
+ part = part.to_s.humanize
69
+
70
+ part = part.singularize if quantity <= 1
71
+
72
+ diff_parts << "#{quantity} #{part}"
73
+ end
74
+
75
+ last_part = diff_parts.pop
76
+ if diff_parts.empty?
77
+ last_part
78
+ else
79
+ [diff_parts.join(', '), last_part].join(' and ')
80
+ end
81
+ end
82
+
83
+ private
84
+
85
+ def initialize(start_time, end_time)
86
+ start_time = time_in_seconds(start_time)
87
+ end_time = time_in_seconds(end_time)
88
+
89
+ @time_diff = (end_time - start_time).abs
90
+ end
91
+
92
+ def time_in_seconds(time)
93
+ time.to_time.to_f
94
+ end
95
+
96
+ def in_component(component)
97
+ (@time_diff / 1.send(component)).round(2)
98
+ end
99
+ end
100
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Timely
4
- VERSION = '0.6.0'
4
+ VERSION = '0.7.0'
5
5
  end
@@ -0,0 +1,185 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Timely::TimeDifference do
6
+ def self.with_each_class(&block)
7
+ classes = [Time, Date, DateTime]
8
+
9
+ classes.each do |clazz|
10
+ context "with a #{clazz.name} class" do
11
+ instance_exec clazz, &block
12
+ end
13
+ end
14
+ end
15
+
16
+ describe '.between' do
17
+ with_each_class do |clazz|
18
+ it 'returns a new TimeDifference instance in each component' do
19
+ start_time = clazz.new(2011, 1)
20
+ end_time = clazz.new(2011, 12)
21
+
22
+ expect(Timely::TimeDifference.between(start_time, end_time)).to be_a(Timely::TimeDifference)
23
+ end
24
+ end
25
+ end
26
+
27
+ describe '#in_each_component' do
28
+ with_each_class do |clazz|
29
+ it 'returns time difference in each component' do
30
+ start_time = clazz.new(2011, 1)
31
+ end_time = clazz.new(2011, 12)
32
+
33
+ expect(Timely::TimeDifference.between(start_time, end_time).in_each_component).to eql(years: 0.91, months: 10.98, weeks: 47.71, days: 334.0, hours: 8016.0, minutes: 480_960.0, seconds: 28_857_600.0)
34
+ end
35
+ end
36
+ end
37
+
38
+ describe '#in_general' do
39
+ with_each_class do |clazz|
40
+ it 'returns time difference in general that matches the total seconds' do
41
+ start_time = clazz.new(2009, 11)
42
+ end_time = clazz.new(2011, 1)
43
+
44
+ expect(Timely::TimeDifference.between(start_time, end_time).in_general).to eql(years: 1, months: 2, weeks: 0, days: 0, hours: 0, minutes: 0, seconds: 0)
45
+ end
46
+ end
47
+ end
48
+
49
+ describe '#humanize' do
50
+ with_each_class do |clazz|
51
+ it 'returns a string representing the time difference from in_general' do
52
+ start_time = clazz.new(2009, 11)
53
+ end_time = clazz.new(2011, 1)
54
+
55
+ expect(Timely::TimeDifference.between(start_time, end_time).humanize).to eql('1 Year and 2 Months')
56
+ end
57
+ end
58
+ end
59
+
60
+ describe '#in_years' do
61
+ with_each_class do |clazz|
62
+ it 'returns time difference in years based on Wolfram Alpha' do
63
+ start_time = clazz.new(2011, 1)
64
+ end_time = clazz.new(2011, 12)
65
+
66
+ expect(Timely::TimeDifference.between(start_time, end_time).in_years).to eql(0.91)
67
+ end
68
+
69
+ it 'returns an absolute difference' do
70
+ start_time = clazz.new(2011, 12)
71
+ end_time = clazz.new(2011, 1)
72
+
73
+ expect(Timely::TimeDifference.between(start_time, end_time).in_years).to eql(0.91)
74
+ end
75
+ end
76
+ end
77
+
78
+ describe '#in_months' do
79
+ with_each_class do |clazz|
80
+ it 'returns time difference in months based on Wolfram Alpha' do
81
+ start_time = clazz.new(2011, 1)
82
+ end_time = clazz.new(2011, 12)
83
+
84
+ expect(Timely::TimeDifference.between(start_time, end_time).in_months).to eql(10.98)
85
+ end
86
+
87
+ it 'returns an absolute difference' do
88
+ start_time = clazz.new(2011, 12)
89
+ end_time = clazz.new(2011, 1)
90
+
91
+ expect(Timely::TimeDifference.between(start_time, end_time).in_months).to eql(10.98)
92
+ end
93
+ end
94
+ end
95
+
96
+ describe '#in_weeks' do
97
+ with_each_class do |clazz|
98
+ it 'returns time difference in weeks based on Wolfram Alpha' do
99
+ start_time = clazz.new(2011, 1)
100
+ end_time = clazz.new(2011, 12)
101
+
102
+ expect(Timely::TimeDifference.between(start_time, end_time).in_weeks).to eql(47.71)
103
+ end
104
+
105
+ it 'returns an absolute difference' do
106
+ start_time = clazz.new(2011, 12)
107
+ end_time = clazz.new(2011, 1)
108
+
109
+ expect(Timely::TimeDifference.between(start_time, end_time).in_weeks).to eql(47.71)
110
+ end
111
+ end
112
+ end
113
+
114
+ describe '#in_days' do
115
+ with_each_class do |clazz|
116
+ it 'returns time difference in weeks based on Wolfram Alpha' do
117
+ start_time = clazz.new(2011, 1)
118
+ end_time = clazz.new(2011, 12)
119
+
120
+ expect(Timely::TimeDifference.between(start_time, end_time).in_days).to eql(334.0)
121
+ end
122
+
123
+ it 'returns an absolute difference' do
124
+ start_time = clazz.new(2011, 12)
125
+ end_time = clazz.new(2011, 1)
126
+
127
+ expect(Timely::TimeDifference.between(start_time, end_time).in_days).to eql(334.0)
128
+ end
129
+ end
130
+ end
131
+
132
+ describe '#in_hours' do
133
+ with_each_class do |clazz|
134
+ it 'returns time difference in hours based on Wolfram Alpha' do
135
+ start_time = clazz.new(2011, 1)
136
+ end_time = clazz.new(2011, 12)
137
+
138
+ expect(Timely::TimeDifference.between(start_time, end_time).in_hours).to eql(8016.0)
139
+ end
140
+
141
+ it 'returns an absolute difference' do
142
+ start_time = clazz.new(2011, 12)
143
+ end_time = clazz.new(2011, 1)
144
+
145
+ expect(Timely::TimeDifference.between(start_time, end_time).in_hours).to eql(8016.0)
146
+ end
147
+ end
148
+ end
149
+
150
+ describe '#in_minutes' do
151
+ with_each_class do |clazz|
152
+ it 'returns time difference in minutes based on Wolfram Alpha' do
153
+ start_time = clazz.new(2011, 1)
154
+ end_time = clazz.new(2011, 12)
155
+
156
+ expect(Timely::TimeDifference.between(start_time, end_time).in_minutes).to eql(480_960.0)
157
+ end
158
+
159
+ it 'returns an absolute difference' do
160
+ start_time = clazz.new(2011, 12)
161
+ end_time = clazz.new(2011, 1)
162
+
163
+ expect(Timely::TimeDifference.between(start_time, end_time).in_minutes).to eql(480_960.0)
164
+ end
165
+ end
166
+ end
167
+
168
+ describe '#in_seconds' do
169
+ with_each_class do |clazz|
170
+ it 'returns time difference in seconds based on Wolfram Alpha' do
171
+ start_time = clazz.new(2011, 1)
172
+ end_time = clazz.new(2011, 12)
173
+
174
+ expect(Timely::TimeDifference.between(start_time, end_time).in_seconds).to eql(28_857_600.0)
175
+ end
176
+
177
+ it 'returns an absolute difference' do
178
+ start_time = clazz.new(2011, 12)
179
+ end_time = clazz.new(2011, 1)
180
+
181
+ expect(Timely::TimeDifference.between(start_time, end_time).in_seconds).to eql(28_857_600.0)
182
+ end
183
+ end
184
+ end
185
+ end
@@ -3,4 +3,4 @@
3
3
  require 'simplecov-rcov'
4
4
  require 'coveralls'
5
5
  require 'coverage/kit'
6
- Coverage::Kit.setup(minimum_coverage: 70.15)
6
+ Coverage::Kit.setup(minimum_coverage: 72.0)
data/timely.gemspec CHANGED
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency 'actionpack'
24
24
  spec.add_development_dependency 'activerecord'
25
25
  spec.add_development_dependency 'activesupport'
26
- spec.add_development_dependency 'bundler', '~> 2.0.1'
26
+ spec.add_development_dependency 'bundler', '~> 2.1.0'
27
27
  spec.add_development_dependency 'coverage-kit'
28
28
  spec.add_development_dependency 'coveralls'
29
29
  spec.add_development_dependency 'rake'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timely
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Noack
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-27 00:00:00.000000000 Z
11
+ date: 2019-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 2.0.1
61
+ version: 2.1.0
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 2.0.1
68
+ version: 2.1.0
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: coverage-kit
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -254,6 +254,7 @@ files:
254
254
  - lib/timely/rails/period.rb
255
255
  - lib/timely/rails/season.rb
256
256
  - lib/timely/rails/time.rb
257
+ - lib/timely/rails/time_difference.rb
257
258
  - lib/timely/railtie.rb
258
259
  - lib/timely/range.rb
259
260
  - lib/timely/string.rb
@@ -276,6 +277,7 @@ files:
276
277
  - spec/rails/date_spec.rb
277
278
  - spec/rails/date_time_spec.rb
278
279
  - spec/rails/period_spec.rb
280
+ - spec/rails/time_difference_spec.rb
279
281
  - spec/rails/time_spec.rb
280
282
  - spec/schema.rb
281
283
  - spec/season_spec.rb
@@ -320,6 +322,7 @@ test_files:
320
322
  - spec/rails/date_spec.rb
321
323
  - spec/rails/date_time_spec.rb
322
324
  - spec/rails/period_spec.rb
325
+ - spec/rails/time_difference_spec.rb
323
326
  - spec/rails/time_spec.rb
324
327
  - spec/schema.rb
325
328
  - spec/season_spec.rb