duration-formatter 1.0.1 → 1.1.1

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: 507104638a4a8b8cf7adb5692c352c021a33275a
4
- data.tar.gz: 7f95937db867be224021df525f7b5c2eef7490c5
3
+ metadata.gz: 6c15a7267c925c45ad91c80e956db0d5fc57ee2a
4
+ data.tar.gz: d0a69ecac8179b8c1e0cd79931413ba69ae7f2c9
5
5
  SHA512:
6
- metadata.gz: ed666090e24594bbb499ce93e2fd000f24950c5e50a8f0de22d37163f37892471936e01257f49dd5a48bbb4cb9b2cab290a9a6e25c62696b5fad82ce6dc9ec85
7
- data.tar.gz: 5ea244b725e9334b8af6501122a6b9f42bfd6804b88081873aeedb2a9d8b12cab26142ac5f82ee1c66c4b3b01986ece413d7e7dbd0b7e7660d3e49401338f201
6
+ metadata.gz: afd682c2028e8f80d1986035eaa364d59a2fcfc6581ab04aaaf8ed25c681e6e2fa68d8d90835ad8249b502ab3a4c825ad803e370b662dfafeedfef8924d84197
7
+ data.tar.gz: fef1f3c1efa3825d34d15462cc317405779972356873e66ac9ee30b97a51d96702f56f7330610b5c95f9500e6abc8e61e643e27c8f4a72a043aca00205e909a8
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
- coverage
1
+ coverage
2
+ tmp
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- duration-formatter (1.0.1)
4
+ duration-formatter (1.1.1)
5
5
  ruby-duration (~> 3.2)
6
6
 
7
7
  GEM
@@ -78,6 +78,7 @@ DEPENDENCIES
78
78
  guard (~> 2.12)
79
79
  guard-minitest (~> 2.4)
80
80
  rake (~> 10.4)
81
+ simplecov (~> 0.9)
81
82
 
82
83
  BUNDLED WITH
83
- 1.10.3
84
+ 1.10.5
@@ -21,5 +21,6 @@ Gem::Specification.new do |s|
21
21
  s.add_development_dependency 'rake', '~> 10.4'
22
22
  s.add_development_dependency 'guard', '~> 2.12'
23
23
  s.add_development_dependency 'guard-minitest', '~> 2.4'
24
+ s.add_development_dependency 'simplecov', '~> 0.9'
24
25
  s.add_development_dependency 'codeclimate-test-reporter', '~> 0.4'
25
26
  end
@@ -1,3 +1,3 @@
1
1
  module DurationFormatter
2
- VERSION = '1.0.1'
2
+ VERSION = '1.1.1'
3
3
  end
@@ -2,44 +2,42 @@ require 'ruby-duration'
2
2
 
3
3
  class FormattedDuration
4
4
  CONSTRAINTS = [:minutes, :hours, :days, :weeks]
5
+ FORMATS = { weeks: '%w %~w', days: '%tdu', hours: '%thu', minutes: '%tmu' }
6
+ BARE_FORMATS = { days: '%d %~d', hours: '%h %~h', minutes: '%m %~m' }
7
+ BARE = { days: 7, hours: 24, minutes: 60 }
5
8
 
6
9
  def initialize(minutes, constraint = :weeks)
7
10
  @constraint = CONSTRAINTS.index(constraint)
8
-
9
11
  @minutes = minutes
10
12
  @hours = minutes / 60
11
13
  @days = @hours / 24
12
14
  @weeks = @days / 7
15
+ @output = []
13
16
  end
14
17
 
15
18
  def format
16
- weekless_days = @days % 7
17
- dayless_hours = @hours % 24
18
- hourless_minutes = @minutes % 60
19
- output = []
19
+ append(:weeks, @weeks, '%w %~w') if @weeks > 0 && @constraint > 2
20
+ add(:days, @days, 2)
21
+ add(:hours, @hours, 1)
22
+ add(:minutes, @minutes, 0)
20
23
 
21
- if @weeks != 0 && @constraint > 2
22
- output << Duration.new(weeks: @weeks).format('%w %~w')
23
- end
24
+ @output.join(', ')
25
+ end
24
26
 
25
- if @days > 0 && @constraint == 2
26
- output << Duration.new(days: @days).format('%tdu')
27
- elsif weekless_days != 0 && @constraint > 1
28
- output << Duration.new(days: weekless_days).format('%d %~d')
29
- end
27
+ def append(unit, value, format)
28
+ opts = {}
29
+ opts[unit] = value
30
30
 
31
- if @hours > 0 && @constraint == 1
32
- output << Duration.new(hours: @hours).format('%thu')
33
- elsif dayless_hours != 0 && @constraint > 0
34
- output << Duration.new(hours: dayless_hours).format('%h %~h')
35
- end
31
+ @output << Duration.new(opts).format(format)
32
+ end
36
33
 
37
- if @minutes > 0 && @constraint == 0
38
- output << Duration.new(minutes: @minutes).format('%tmu')
39
- elsif hourless_minutes != 0
40
- output << Duration.new(minutes: hourless_minutes).format('%m %~m')
41
- end
34
+ def add(unit, value, constraint)
35
+ bare = value % BARE[unit]
42
36
 
43
- output.join(', ')
37
+ if value > 0 && @constraint == constraint
38
+ append(unit, value, FORMATS[unit])
39
+ elsif bare > 0 && @constraint > constraint - 1
40
+ append(unit, bare, BARE_FORMATS[unit])
41
+ end
44
42
  end
45
43
  end
data/test/test_helper.rb CHANGED
@@ -1,6 +1,13 @@
1
1
  # Coverage
2
- require 'codeclimate-test-reporter'
3
- CodeClimate::TestReporter.start if ENV['CODECLIMATE_REPO_TOKEN']
2
+ if ENV['CODECLIMATE_REPO_TOKEN']
3
+ require 'codeclimate-test-reporter'
4
+ CodeClimate::TestReporter.start
5
+ elsif ENV['COVERAGE']
6
+ require 'simplecov'
7
+ SimpleCov.start do
8
+ add_group 'Libraries', 'lib'
9
+ end
10
+ end
4
11
 
5
12
  # Source files
6
13
  require './lib/formatted_duration'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: duration-formatter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Venneman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-08 00:00:00.000000000 Z
11
+ date: 2015-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-duration
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '2.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.9'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.9'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: codeclimate-test-reporter
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -95,7 +109,6 @@ files:
95
109
  - Guardfile
96
110
  - README.md
97
111
  - Rakefile
98
- - duration-formatter-1.0.0.gem
99
112
  - duration-formatter.gemspec
100
113
  - lib/duration-formatter.rb
101
114
  - lib/duration_formatter/version.rb
Binary file