duration-formatter 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 719df39ecfaa4879706be6e1c4ab68cdb7a9b7df
4
- data.tar.gz: 6212e2a572bc0a72cde231bf1aa6db37a543e19f
3
+ metadata.gz: a39c32b6b33eb9cbceb2217076d2885c6cccc9d3
4
+ data.tar.gz: 605b7729fd220eb86475942fc703efb2fa57242a
5
5
  SHA512:
6
- metadata.gz: b73e3c5dfa20f653f594dae622379e892dc59638ae928bfa7f80f02f6d2e661113665c70ebbfe1fe07037b4cc34ce96384b77006c7fce5f0a7f67c135fcc9b5b
7
- data.tar.gz: 60734ab1ff638952b4f9e62832cc561085b63a1e91aaf6fc74eaeb580d04b45d46f91034dab7f591eb5bed403065510bb3e38bf83ecd5f6171d5f508c1e3245a
6
+ metadata.gz: 8959c57e5da03f7042907d9fc88fb30aca4ca26185839c39b27cac9c212f052aca70b98f420ae8919a3bcf734a78c4808a2d621b8d9488afc668de359de0222b
7
+ data.tar.gz: 246a2fc74441bdf3e6ef0b5b6c116e3d05867484bad1326135130acef7089fe441cba4323da5822b5be3d72bf1b1f1653b682ba8bcfce39f09058959615d52df
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- duration-formatter (0.1.1)
4
+ duration-formatter (1.0.0)
5
5
  ruby-duration (~> 3.2)
6
6
 
7
7
  GEM
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(11520, :days).format # => 15 days
41
+ FormattedDuration.new(80, :minutes).format # => 80 minutes
39
42
  ```
40
43
 
41
44
  ## TODO
@@ -1,3 +1,3 @@
1
1
  module DurationFormatter
2
- VERSION = '0.1.1'
2
+ VERSION = '1.0.0'
3
3
  end
@@ -1,13 +1,12 @@
1
1
  require 'ruby-duration'
2
2
 
3
3
  class FormattedDuration
4
- WEEKS_FORMAT = '%w %~w'
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
- @minutes = minutes % 60
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
- output << Duration.new(weeks: @weeks).format(WEEKS_FORMAT) if @weeks != 0
22
- output << Duration.new(days: weekless_days).format(DAYS_FORMAT) if weekless_days != 0
23
- output << Duration.new(hours: dayless_hours).format(HOURS_FORMAT) if dayless_hours != 0
24
- output << Duration.new(minutes: @minutes).format(MINUTES_FORMAT) if @minutes != 0
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: duration-formatter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Venneman