time_boots 0.0.1 → 0.0.2

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
  SHA1:
3
- metadata.gz: 2d915342173e04dfa56b06a071ce00895bfd1f7c
4
- data.tar.gz: 224f704b9f2252a6d1915416972e935bb271ee0a
3
+ metadata.gz: bad3cf47e90daa9c45c5a0cae35d295cd0cf3100
4
+ data.tar.gz: c79c616cde97bc0e82e9517241ef30d311ce7135
5
5
  SHA512:
6
- metadata.gz: 75392ebf3e012f77a133ac50cc79bb18e510289574148fc47279c89a8df64264000c1a47e532c212bd1d6b8962a4a3d58c9da270a869372285a715efdd424e04
7
- data.tar.gz: 3ab029a28565166df7c4581f447e0a0a127d4eaec7e6024391bcd39374fff88398ef6f7dac61058b45e8d68674a762612867686405bf000b9c3ec4f01d47faf1
6
+ metadata.gz: 5d1eb2eb38316109d7c68219a2f9c4ef0701faa3b9bf3594edb1e539bf9b1a5573942760806339b553fff32d4c278004f4f307e032340f255a0e9bc470e77ccb
7
+ data.tar.gz: 2759571a3d4974b33f8e9a7a43aeb50505715f7ca04668e221b370da9020a1f7916ce2f9706f1e5b2233b5afecb7aba22a0ecba3b964ba304691d59e9c49c868
@@ -0,0 +1,5 @@
1
+ # TimeBoots changelog
2
+
3
+ # 0.0.2 (2016-05-27)
4
+
5
+ * Add support for `DateTime`.
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014-15 Victor 'Zverok' Shepelev
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 CHANGED
@@ -1,15 +1,28 @@
1
1
  # Time Boots
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/time_boots.svg)](http://badge.fury.io/rb/time_boots)
3
4
  [![Dependency Status](https://gemnasium.com/zverok/time_boots.svg)](https://gemnasium.com/zverok/time_boots)
4
5
  [![Code Climate](https://codeclimate.com/github/zverok/time_boots/badges/gpa.svg)](https://codeclimate.com/github/zverok/time_boots)
5
6
  [![Build Status](https://travis-ci.org/zverok/time_boots.svg?branch=master)](https://travis-ci.org/zverok/time_boots)
6
7
  [![Coverage Status](https://coveralls.io/repos/zverok/time_boots/badge.svg?branch=master)](https://coveralls.io/r/zverok/time_boots?branch=master)
7
8
 
8
- **TimeBoots** is a small, no-dependencies library attemting to make time
9
+ **TimeBoots** is a small, no-dependencies library attempting to make time
9
10
  steps easier. It provides you with simple, easy-to-remember API, without
10
11
  any monkey-patching of core Ruby classes, so it can be used alongside
11
12
  Rails or without it, for any purpose.
12
13
 
14
+ Install it like always:
15
+
16
+ ```
17
+ gem install time_boots
18
+ ```
19
+
20
+ or add to your Gemfile
21
+
22
+ ```
23
+ gem 'time_boots'
24
+ ```
25
+
13
26
  ## Simple time math
14
27
 
15
28
  ### Floor, ceil and round:
@@ -33,7 +46,7 @@ TimeBoots.month.floor(tm)
33
46
 
34
47
  TimeBoots.month.ceil(tm)
35
48
  # => 2015-04-01 00:00:00 +0300
36
- # Note the timezone change: our (Ukraine) DST was in March,
49
+ # Note the UTC offset change: our (Ukraine) DST was in March,
37
50
  # and TimeBoots plays perfectly well with it.
38
51
 
39
52
  TimeBoots.month.round(tm)
@@ -81,9 +94,9 @@ jump.after(tm)
81
94
 
82
95
  ```ruby
83
96
  TimeBoots.hour.range(tm, 5)
84
- # => 2015-03-05 10:08:00 +0200...2015-03-05 15:08:00 +0200
97
+ # => 2015-03-05 10:08:00 +0200...2015-03-05 15:08:00 +0200
85
98
  TimeBoots.hour.range_back(tm, 5)
86
- # => 2015-03-05 05:08:00 +0200...2015-03-05 10:08:00 +0200
99
+ # => 2015-03-05 05:08:00 +0200...2015-03-05 10:08:00 +0200
87
100
  ```
88
101
 
89
102
  ## Measuring time periods
@@ -111,7 +124,7 @@ puts "%{years}y %{months}m %{weeks}w %{days}d %{hours}h %{minutes}m %{seconds}s"
111
124
 
112
125
  # Option: measure without weeks
113
126
  TimeBoots.measure(birthday, Time.now, weeks: false)
114
- # => {:years=>32, :months=>2, :days=>17, :hours=>7, :minutes=>5, :seconds=>11}
127
+ # => {:years=>32, :months=>2, :days=>17, :hours=>7, :minutes=>5, :seconds=>11}
115
128
 
116
129
  # My full age in days, hours, minutes
117
130
  TimeBoots.measure(birthday, Time.now, max_step: :day)
@@ -212,3 +225,9 @@ And there are some plans for the future:
212
225
  * optional `core_ext`, providing methods like `4.months.ago` for the
213
226
  (Rails-less) rest of us;
214
227
  * your ideas?..
228
+
229
+ ## Alternatives
230
+
231
+ There's pretty small and useful [AS::Duration](https://github.com/janko-m/as-duration)
232
+ by Janko Marohnić, which is time durations, extracted from ActiveSupport,
233
+ but without any ActiveSupport bloat.
@@ -5,7 +5,7 @@ module TimeBoots
5
5
  # rubocop:disable Style/ModuleFunction
6
6
  extend self
7
7
  # rubocop:enable Style/ModuleFunction
8
-
8
+
9
9
  def steps
10
10
  Boot.steps
11
11
  end
@@ -13,7 +13,7 @@ module TimeBoots
13
13
  # NB: no fancy meta-programming here: we want YARD to be happy
14
14
 
15
15
  # Boot shortcuts
16
-
16
+
17
17
  def sec
18
18
  Boot.get(:sec)
19
19
  end
@@ -43,7 +43,7 @@ module TimeBoots
43
43
  end
44
44
 
45
45
  # Boot-less method shortcuts
46
-
46
+ # :nocov:
47
47
  def floor(step, tm)
48
48
  Boot.get(step).floor(tm)
49
49
  end
@@ -87,6 +87,7 @@ module TimeBoots
87
87
  def lace(step, from, to, options = {})
88
88
  Boot.get(step).lace(from, to, options)
89
89
  end
90
+ # :nocov:
90
91
  end
91
92
 
92
93
  require_relative './time_boots/boot'
@@ -14,8 +14,8 @@ module TimeBoots
14
14
  tm.hour,
15
15
  tm.min,
16
16
  tm.sec].first(step_idx + 1)
17
-
18
- Time.new(*components)
17
+
18
+ new_from_components(tm, *components)
19
19
  end
20
20
 
21
21
  def ceil(tm)
@@ -53,7 +53,7 @@ module TimeBoots
53
53
  end
54
54
 
55
55
  def measure(_from, _to)
56
- fail NotImplementedError, '#measure should be implemented in subclasses'
56
+ raise NotImplementedError, '#measure should be implemented in subclasses'
57
57
  end
58
58
 
59
59
  def measure_rem(from, to)
@@ -70,25 +70,38 @@ module TimeBoots
70
70
  end
71
71
 
72
72
  protected
73
-
74
- NATURAL_STEPS = [:year, :month, :day, :hour, :min, :sec]
73
+
74
+ NATURAL_STEPS = [:year, :month, :day, :hour, :min, :sec].freeze
75
+ DEFAULT_STEP_VALUES = [nil, 1, 1, 0, 0, 0].freeze
75
76
 
76
77
  def step_idx
77
78
  NATURAL_STEPS.index(step) or
78
- fail(NotImplementedError, "Can not be used for step #{step}")
79
+ raise NotImplementedError, "Can not be used for step #{step}"
79
80
  end
80
81
 
81
82
  def generate(tm, replacements = {})
82
- hash_to_tm(tm_to_hash(tm).merge(replacements))
83
+ hash_to_tm(tm, tm_to_hash(tm).merge(replacements))
83
84
  end
84
85
 
85
86
  def tm_to_hash(tm)
86
- Hash[*NATURAL_STEPS.flat_map{|s| [s, tm.send(s)]}]
87
+ Hash[*NATURAL_STEPS.flat_map { |s| [s, tm.send(s)] }]
87
88
  end
88
89
 
89
- def hash_to_tm(hash)
90
- components = NATURAL_STEPS.map{|s| hash[s] || 0}
91
- Time.new(*components)
90
+ def hash_to_tm(origin, hash)
91
+ components = NATURAL_STEPS.map { |s| hash[s] || 0 }
92
+ new_from_components(origin, *components)
93
+ end
94
+
95
+ def new_from_components(origin, *components)
96
+ components = DEFAULT_STEP_VALUES.zip(components).map { |d, c| c || d }
97
+ case origin
98
+ when Time
99
+ Time.mktime(*components.reverse, nil, nil, nil, origin.zone)
100
+ when DateTime
101
+ DateTime.new(*components, origin.zone)
102
+ else
103
+ raise ArgumentError, "Expected Time or DateTime, got #{origin.class}"
104
+ end
92
105
  end
93
106
 
94
107
  include TimeBoots # now we can use something like #day inside boots
@@ -98,21 +111,21 @@ module TimeBoots
98
111
  require_relative 'boot/week'
99
112
  require_relative 'boot/month'
100
113
  require_relative 'boot/year'
101
-
114
+
102
115
  BOOTS = {
103
116
  sec: SecBoot.new, min: MinBoot.new, hour: HourBoot.new,
104
117
  day: DayBoot.new, week: WeekBoot.new, month: MonthBoot.new,
105
118
  year: YearBoot.new
106
- }
119
+ }.freeze
107
120
 
108
121
  class << self
109
122
  def steps
110
123
  BOOTS.keys
111
124
  end
112
-
125
+
113
126
  def get(step)
114
127
  BOOTS[step] or
115
- fail(ArgumentError, "Unsupported step: #{step}")
128
+ raise ArgumentError, "Unsupported step: #{step}"
116
129
  end
117
130
  end
118
131
  end
@@ -16,6 +16,8 @@ module TimeBoots
16
16
  end
17
17
 
18
18
  def fix_dst(res, src)
19
+ return res unless res.is_a?(Time)
20
+
19
21
  if res.dst? && !src.dst?
20
22
  hour.decrease(res)
21
23
  elsif !res.dst? && src.dst?
@@ -29,11 +29,11 @@ module TimeBoots
29
29
  end
30
30
 
31
31
  def _advance(tm, steps)
32
- steps.times.inject(tm){|t| succ(t)}
32
+ steps.times.inject(tm) { |t| succ(t) }
33
33
  end
34
34
 
35
35
  def _decrease(tm, steps)
36
- steps.times.inject(tm){|t| prev(t)}
36
+ steps.times.inject(tm) { |t| prev(t) }
37
37
  end
38
38
 
39
39
  # fix for too far advance/insufficient decrease:
@@ -6,20 +6,31 @@ module TimeBoots
6
6
  end
7
7
 
8
8
  def measure(from, to)
9
- ((to - from) / to_seconds).to_i
9
+ ((to.to_time - from.to_time) / to_seconds).to_i
10
10
  end
11
11
 
12
12
  protected
13
13
 
14
14
  def _advance(tm, steps)
15
- tm + to_seconds(steps)
15
+ _shift(tm, to_seconds(steps))
16
16
  end
17
17
 
18
18
  def _decrease(tm, steps)
19
- tm - to_seconds(steps)
19
+ _shift(tm, -to_seconds(steps))
20
20
  end
21
21
 
22
- MULTIPLIERS = [12, 30, 24, 60, 60, 1]
22
+ def _shift(tm, seconds)
23
+ case tm
24
+ when Time
25
+ tm + seconds
26
+ when DateTime
27
+ tm + Rational(seconds, 86_400)
28
+ else
29
+ raise ArgumentError, "Expected Time or DateTime, got #{origin.class}"
30
+ end
31
+ end
32
+
33
+ MULTIPLIERS = [12, 30, 24, 60, 60, 1].freeze
23
34
  end
24
35
 
25
36
  class SecBoot < SimpleBoot
@@ -14,7 +14,7 @@ module TimeBoots
14
14
  end
15
15
 
16
16
  protected
17
-
17
+
18
18
  def _advance(tm, steps)
19
19
  generate(tm, year: tm.year + steps)
20
20
  end
@@ -16,8 +16,8 @@ module TimeBoots
16
16
  @boot.advance(tm, amount)
17
17
  end
18
18
 
19
- alias_method :ago, :before
20
- alias_method :from, :after
19
+ alias ago before
20
+ alias from after
21
21
 
22
22
  def ==(other)
23
23
  step == other.step && amount == other.amount
@@ -14,7 +14,7 @@ module TimeBoots
14
14
  def expand!
15
15
  @from = boot.floor(from)
16
16
  @to = boot.ceil(to)
17
-
17
+
18
18
  self
19
19
  end
20
20
 
@@ -31,7 +31,7 @@ module TimeBoots
31
31
 
32
32
  iter = cond_floor(boot.advance(iter), beginnings)
33
33
  end
34
-
34
+
35
35
  seq
36
36
  end
37
37
 
@@ -41,7 +41,7 @@ module TimeBoots
41
41
  end
42
42
 
43
43
  def pull_ranges(beginnings = false)
44
- pull_pairs(beginnings).map{|b, e| (b...e)}
44
+ pull_pairs(beginnings).map { |b, e| (b...e) }
45
45
  end
46
46
 
47
47
  def inspect
@@ -53,7 +53,7 @@ module TimeBoots
53
53
  def cond_floor(tm, should_floor)
54
54
  should_floor ? boot.floor(tm) : tm
55
55
  end
56
-
56
+
57
57
  attr_reader :boot
58
58
  end
59
59
  end
@@ -9,8 +9,8 @@ module TimeBoots
9
9
  hour: :hours,
10
10
  min: :minutes,
11
11
  sec: :seconds
12
- }
13
-
12
+ }.freeze
13
+
14
14
  def self.measure(from, to, options = {})
15
15
  select_steps(options).reverse.inject({}) do |res, step|
16
16
  span, from = Boot.get(step).measure_rem(from, to)
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module TimeBoots
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'.freeze
4
4
  end
@@ -29,8 +29,10 @@ Gem::Specification.new do |s|
29
29
  end
30
30
  s.require_paths = ["lib"]
31
31
 
32
- s.add_development_dependency 'rubocop', '~> 0.30'
33
- s.add_development_dependency 'rspec', '~> 3'
32
+ s.add_development_dependency 'rubocop', '>= 0.30'
33
+ s.add_development_dependency 'rspec', '>= 3'
34
34
  s.add_development_dependency 'rspec-its', '~> 1'
35
35
  s.add_development_dependency 'simplecov', '~> 0.9'
36
+ s.add_development_dependency 'rake'
37
+ s.add_development_dependency 'rubygems-tasks'
36
38
  end
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time_boots
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Shepelev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-04 00:00:00.000000000 Z
11
+ date: 2016-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0.30'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.30'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '3'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3'
41
41
  - !ruby/object:Gem::Dependency
@@ -66,6 +66,34 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0.9'
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: rubygems-tasks
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'
69
97
  description: |2
70
98
  TimeBoots is small, no-dependencies library attemting to make time
71
99
  steps easier. It provides you with simple, easy remembered API, without
@@ -76,6 +104,8 @@ executables: []
76
104
  extensions: []
77
105
  extra_rdoc_files: []
78
106
  files:
107
+ - CHANGELOG.md
108
+ - LICENSE.txt
79
109
  - README.md
80
110
  - lib/time_boots.rb
81
111
  - lib/time_boots/boot.rb
@@ -109,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
139
  version: '0'
110
140
  requirements: []
111
141
  rubyforge_project:
112
- rubygems_version: 2.4.6
142
+ rubygems_version: 2.4.8
113
143
  signing_key:
114
144
  specification_version: 4
115
145
  summary: Easy time steps math