timing 0.0.11 → 0.1.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
  SHA1:
3
- metadata.gz: f4f1bcb93afbfa766ddafe4737e0fd197ecc0a5a
4
- data.tar.gz: 5d5f5fa1ff92269ec9911be451840fcf7c7f3f8e
3
+ metadata.gz: 360bd7d8b70b61144022f1ba72527e969bd89825
4
+ data.tar.gz: 948ef3019b6e63ded7ed4c8e0658ff36b00c26d7
5
5
  SHA512:
6
- metadata.gz: 6435646102eaa16483a6e2cf3d58fecea34c1019f8814ccbf14da60f2efce6269e85653fb1c9a2189f76464e4e9d363f23a404cb3a90639f9a5b5a092d002eb1
7
- data.tar.gz: 23b99900b3ceb825c6e4cefab489f9effc8a5dbf0c4607f8d119b65512f2429978da19bf0bbc5b05dbc0b830b0f9937458d6da72a13f039410e29ccbaa9144dc
6
+ metadata.gz: 5dd7d1e1340e44e1691ca26ab5ce51fd50bafdc627a45a64d4b7072be7cd2ed30b555d241e7d8e441fb6c73c72496ea569a7f7c125cf938cef5f93e3f248bcb5
7
+ data.tar.gz: 0ccdcb9ea732e7ed8a7c7e295c20a5f606e25126a9ecae8ae0e9c41d5ef7c85b0afddf7ea598d2ac87cb7a858bdd04e2aaf1baff123a651b3edc75b8a433666a
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/timing.svg)](https://rubygems.org/gems/timing)
4
4
  [![Build Status](https://travis-ci.org/gabynaiman/timing.svg?branch=master)](https://travis-ci.org/gabynaiman/timing)
5
- [![Coverage Status](https://coveralls.io/repos/gabynaiman/timing/badge.svg?branch=master)](https://coveralls.io/r/gabynaiman/timing?branch=master)
5
+ [![Coverage Status](https://coveralls.io/repos/github/gabynaiman/timing/badge.svg?branch=master)](https://coveralls.io/github/gabynaiman/timing?branch=master)
6
6
  [![Code Climate](https://codeclimate.com/github/gabynaiman/timing.svg)](https://codeclimate.com/github/gabynaiman/timing)
7
7
  [![Dependency Status](https://gemnasium.com/gabynaiman/timing.svg)](https://gemnasium.com/gabynaiman/timing)
8
8
 
@@ -77,6 +77,11 @@ interval.to_minutes # => 10080.0
77
77
  interval.to_hours # => 168.0
78
78
  interval.to_days # => 7.0
79
79
  interval.to_weeks # => 1.0
80
+
81
+ interval = Timing::Interval.seconds(1299785)
82
+ interval.to_human # => 2w 1d 1h 3m 5s
83
+ interval.to_human(biggest_unit: 'd') # => 15d 1h 3m 5s
84
+ interval.to_human(smallest_unit: 'm') # => 2w 1d 1h 3m
80
85
  ```
81
86
 
82
87
 
@@ -17,8 +17,28 @@ module Timing
17
17
  w: 60 * 60 * 24 * 7
18
18
  }
19
19
 
20
+ MULTIPLIER = {
21
+ s: 60,
22
+ m: 60,
23
+ h: 24,
24
+ d: 7,
25
+ w: 1
26
+ }
27
+
28
+ UNITS = UNITS_NAMES.map(&:first)
29
+
20
30
  REGEXP = /^([\d\.]+)([smhdw])$/
21
31
 
32
+ def self.parse(expression)
33
+ match = REGEXP.match expression.strip
34
+ raise "Invalid interval expression #{expression}" unless match
35
+ new match.captures[0].to_f * CONVERSIONS[match.captures[1].to_sym]
36
+ end
37
+
38
+ def self.between(time_1, time_2)
39
+ new (time_1 - time_2).round
40
+ end
41
+
22
42
  def initialize(seconds)
23
43
  raise ArgumentError, "#{seconds} is not a number" unless seconds.is_a? Numeric
24
44
  super seconds.abs
@@ -47,24 +67,38 @@ module Timing
47
67
  end
48
68
 
49
69
  def to_s
50
- integer = CONVERSIONS.map { |u,f| [to_f / f, "#{to_i / f}#{u}"] }
51
- .sort_by { |v,t| v }
52
- .detect { |v,t| v == v.to_i }
53
- integer ? integer[1] : "#{to_seconds}s"
70
+ representations = UNITS.map.with_index do |unit, i|
71
+ representation = to_representation(unit, false, false)
72
+ [representation, "#{representation.to_i}#{unit}"]
73
+ end
74
+ pair = representations.reverse.detect{ |value,representation| value == value.to_i }
75
+ pair && pair[1] || "#{to_seconds}s"
76
+ end
77
+
78
+ def to_human(options={})
79
+ biggest_unit = options.fetch(:biggest_unit, :w)
80
+ smallest_unit = options.fetch(:smallest_unit, :s)
81
+ last_index = UNITS.index(biggest_unit.to_sym)
82
+ first_index = UNITS.index(smallest_unit.to_sym)
83
+ units = UNITS[first_index..last_index]
84
+ representations = units.map.with_index do |unit, i|
85
+ acumulate = (i != last_index - first_index)
86
+ representation = to_representation(unit, acumulate)
87
+ [representation, "#{representation}#{unit}"]
88
+ end
89
+ representations.select{ |(value, string)| value > 0 }.map(&:last).reverse.join(' ')
54
90
  end
55
91
 
56
92
  def inspect
57
93
  "#{to_s} (#{to_seconds})"
58
94
  end
59
95
 
60
- def self.parse(expression)
61
- match = REGEXP.match expression.strip
62
- raise "Invalid interval expression #{expression}" unless match
63
- new match.captures[0].to_f * CONVERSIONS[match.captures[1].to_sym]
64
- end
96
+ protected
65
97
 
66
- def self.between(time_1, time_2)
67
- new (time_1 - time_2).round
98
+ def to_representation(unit, acumulate=false, truncate=true)
99
+ value = to_f / CONVERSIONS[unit]
100
+ value = value % MULTIPLIER[unit] if acumulate
101
+ truncate ? value.truncate : value
68
102
  end
69
103
 
70
104
  end
@@ -1,3 +1,3 @@
1
1
  module Timing
2
- VERSION = '0.0.11'
2
+ VERSION = '0.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-05 00:00:00.000000000 Z
11
+ date: 2017-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: treetop