duration-formatter 0.1.1 → 1.0.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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +5 -2
- data/lib/duration_formatter/version.rb +1 -1
- data/lib/formatted_duration.rb +27 -10
- data/test/lib/formatted_duration_test.rb +28 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a39c32b6b33eb9cbceb2217076d2885c6cccc9d3
|
4
|
+
data.tar.gz: 605b7729fd220eb86475942fc703efb2fa57242a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8959c57e5da03f7042907d9fc88fb30aca4ca26185839c39b27cac9c212f052aca70b98f420ae8919a3bcf734a78c4808a2d621b8d9488afc668de359de0222b
|
7
|
+
data.tar.gz: 246a2fc74441bdf3e6ef0b5b6c116e3d05867484bad1326135130acef7089fe441cba4323da5822b5be3d72bf1b1f1653b682ba8bcfce39f09058959615d52df
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -31,11 +31,14 @@ FormattedDuration.new(1450).format # => 1 day, 10 minutes
|
|
31
31
|
FormattedDuration.new(241).format # => 4 hours, 1 minute
|
32
32
|
```
|
33
33
|
|
34
|
+
### Constraints
|
35
|
+
|
34
36
|
You can also force a constraint upon the format.
|
35
37
|
|
36
|
-
```
|
38
|
+
```ruby
|
39
|
+
FormattedDuration.new(11520, :days).format # => 15 days (not "2 weeks, 1 day")
|
37
40
|
FormattedDuration.new(1440, :hours).format # => 24 hours
|
38
|
-
FormattedDuration.new(
|
41
|
+
FormattedDuration.new(80, :minutes).format # => 80 minutes
|
39
42
|
```
|
40
43
|
|
41
44
|
## TODO
|
data/lib/formatted_duration.rb
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
require 'ruby-duration'
|
2
2
|
|
3
3
|
class FormattedDuration
|
4
|
-
|
5
|
-
DAYS_FORMAT = '%d %~d'
|
6
|
-
HOURS_FORMAT = '%h %~h'
|
7
|
-
MINUTES_FORMAT = '%m %~m'
|
4
|
+
CONSTRAINTS = [:minutes, :hours, :days, :weeks]
|
8
5
|
|
9
|
-
def initialize(minutes)
|
10
|
-
@
|
6
|
+
def initialize(minutes, constraint = :weeks)
|
7
|
+
@constraint = CONSTRAINTS.index(constraint)
|
8
|
+
|
9
|
+
@minutes = minutes
|
11
10
|
@hours = minutes / 60
|
12
11
|
@days = @hours / 24
|
13
12
|
@weeks = @days / 7
|
@@ -16,12 +15,30 @@ class FormattedDuration
|
|
16
15
|
def format
|
17
16
|
weekless_days = @days % 7
|
18
17
|
dayless_hours = @hours % 24
|
18
|
+
hourless_minutes = @minutes % 60
|
19
19
|
output = []
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
if @weeks != 0 && @constraint > 2
|
22
|
+
output << Duration.new(weeks: @weeks).format('%w %~w')
|
23
|
+
end
|
24
|
+
|
25
|
+
if weekless_days != 0 && @constraint > 1
|
26
|
+
output << Duration.new(days: weekless_days).format('%d %~d')
|
27
|
+
elsif @days > 0 && @constraint == 2
|
28
|
+
output << Duration.new(days: @days).format('%tdu')
|
29
|
+
end
|
30
|
+
|
31
|
+
if dayless_hours != 0 && @constraint > 0
|
32
|
+
output << Duration.new(hours: dayless_hours).format('%h %~h')
|
33
|
+
elsif @hours > 0 && @constraint == 1
|
34
|
+
output << Duration.new(hours: @hours).format('%thu')
|
35
|
+
end
|
36
|
+
|
37
|
+
if hourless_minutes != 0
|
38
|
+
output << Duration.new(minutes: hourless_minutes).format('%m %~m')
|
39
|
+
elsif @minutes > 0 && @constraint == 0
|
40
|
+
output << Duration.new(minutes: @minutes).format('%tmu')
|
41
|
+
end
|
25
42
|
|
26
43
|
output.join(', ')
|
27
44
|
end
|
@@ -1,6 +1,9 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class FormattedDurationTest < Minitest::Test
|
4
|
+
|
5
|
+
# Full units
|
6
|
+
|
4
7
|
def test_weeks_only_duration
|
5
8
|
text = '1 week'
|
6
9
|
output = FormattedDuration.new(10080).format
|
@@ -29,6 +32,8 @@ class FormattedDurationTest < Minitest::Test
|
|
29
32
|
assert_equal text, output
|
30
33
|
end
|
31
34
|
|
35
|
+
# Composition
|
36
|
+
|
32
37
|
def test_weeks_and_days_duration
|
33
38
|
text = '1 week, 4 days'
|
34
39
|
output = FormattedDuration.new(15840).format
|
@@ -49,4 +54,27 @@ class FormattedDurationTest < Minitest::Test
|
|
49
54
|
|
50
55
|
assert_equal text, output
|
51
56
|
end
|
57
|
+
|
58
|
+
# Constraints
|
59
|
+
|
60
|
+
def test_days_constraint
|
61
|
+
text = '7 days'
|
62
|
+
output = FormattedDuration.new(10080, :days).format
|
63
|
+
|
64
|
+
assert_equal text, output
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_hours_constraint
|
68
|
+
text = '24 hours'
|
69
|
+
output = FormattedDuration.new(1440, :hours).format
|
70
|
+
|
71
|
+
assert_equal text, output
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_minutes_constraint
|
75
|
+
text = '60 minutes'
|
76
|
+
output = FormattedDuration.new(60, :minutes).format
|
77
|
+
|
78
|
+
assert_equal text, output
|
79
|
+
end
|
52
80
|
end
|