human_duration 1.0.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.travis.yml +1 -1
- data/CHANGELOG.md +10 -0
- data/README.md +8 -32
- data/example/example.rb +10 -27
- data/human_duration.gemspec +6 -1
- data/lib/human_duration/constants.rb +5 -0
- data/lib/human_duration/overloads.rb +9 -0
- data/lib/human_duration/version.rb +2 -2
- data/lib/human_duration.rb +83 -104
- data/spec/human_duration_spec.rb +108 -212
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 986006379227db9abe3115b6d5e8a60eec3db6a7c475227051174b7680d76074
|
4
|
+
data.tar.gz: cf362347d7a8277e96fc164a8a7fb5488376f988cdb0e3fd4cb83a776435687b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 18e9f783a647a76c66452ca017e638163d7b90680037e8c5a7185a457d81b611e0b867e2adc734a638a3bbc860316faba101cecab4b79572c066b993368796b2
|
7
|
+
data.tar.gz: e4966f937a6820d76b8cbd5494520bb66265c39024347e3bc9dad0bca4d7f2db270e895eb355a91664ceaa88e9961d0f9aebfb54a734b9d1f444ac8f1568b977
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
## 2.0.0 (January 25, 2019)
|
2
|
+
|
3
|
+
IMPROVEMENTS:
|
4
|
+
|
5
|
+
* Move the code inside of a module namespace. ([@TGWolf][])
|
6
|
+
* Extend the integer class so that int.human_duration works. ([@TGWolf][])
|
7
|
+
* Update the documentation. ([@TGWolf][])
|
8
|
+
* Renamed the internal function human_duration to match the module name. ([@TGWolf][])
|
9
|
+
* Removed the old static and global access methods as they were a hack. ([@TGWolf][])
|
10
|
+
|
1
11
|
## 1.0.1 (January 17, 2019)
|
2
12
|
|
3
13
|
CHANGES:
|
data/README.md
CHANGED
@@ -28,46 +28,22 @@ Or install it yourself as:
|
|
28
28
|
|
29
29
|
## Usage
|
30
30
|
|
31
|
-
The main function that needs to be called is the 'humanize' function but there are 3 different ways to do this (as show in the code snippet below)
|
32
|
-
|
33
|
-
* Class method access - `hd = HumanDuration.new` followed by `hd.humanize(x)`
|
34
|
-
* Static method access - `HumanDurationStatic.humanize(x)`
|
35
|
-
* Direct method access - `humanize(x)`
|
36
|
-
|
37
|
-
The first method is the main/original method for accessing this function, the other 2 are simple wrappers to the first.
|
38
|
-
|
39
31
|
```ruby
|
40
|
-
|
41
|
-
|
42
|
-
require 'human_duration'
|
43
|
-
|
44
|
-
test_value = [-10, 0, 9, 98, 987, 987_6, 987_65, 987_654, 987_654_3, 987_654_32, 987_654_321]
|
32
|
+
require 'human_size'
|
45
33
|
|
46
|
-
puts
|
34
|
+
puts HumanDuration::Duration.human_duration(10)
|
47
35
|
|
48
|
-
|
49
|
-
test_value.each do |x|
|
50
|
-
puts hd.humanize(x)
|
51
|
-
end
|
36
|
+
# or
|
52
37
|
|
53
|
-
puts
|
54
|
-
|
55
|
-
puts 'Config: default'
|
56
|
-
test_value.each do |x|
|
57
|
-
puts HumanDurationStatic.humanize(x)
|
58
|
-
end
|
59
|
-
|
60
|
-
puts 'Direct method access'
|
38
|
+
puts 10.human_duration
|
39
|
+
```
|
61
40
|
|
62
|
-
|
63
|
-
test_value.each do |x|
|
64
|
-
puts humanize(x)
|
65
|
-
end
|
41
|
+
It is possible to configure the output using one of the options 'compact', 'small' and 'full', 'compact' is the default. For more detailed example please refer to [example/example.rb](https://github.com/WolfSoftware/human_duration/blob/master/example/example.rb)
|
66
42
|
|
43
|
+
```ruby
|
44
|
+
HumanDuration::Duration.display_type(output_type)
|
67
45
|
```
|
68
46
|
|
69
|
-
It is possible to configure the output using one of the options 'compact', 'small' and 'full', 'compact' isthe default. For more detailed example please refer to [example/example.rb](https://github.com/WolfSoftware/human_duration/blob/master/example/example.rb)
|
70
|
-
|
71
47
|
## Development
|
72
48
|
|
73
49
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/example/example.rb
CHANGED
@@ -4,55 +4,38 @@ require 'human_duration'
|
|
4
4
|
|
5
5
|
test_value = [-10, 0, 9, 98, 987, 987_6, 987_65, 987_654, 987_654_3, 987_654_32, 987_654_321]
|
6
6
|
|
7
|
-
puts 'Class
|
7
|
+
puts 'Class Accessor Method'
|
8
8
|
printf "\tConfig: type = conpact [default]\n"
|
9
|
-
hd = HumanDuration.new
|
10
|
-
test_value.each do |x|
|
11
|
-
printf "\t\t%s\n", hd.humanize(x)
|
12
|
-
end
|
13
|
-
|
14
|
-
printf "\tConfig: type = short\n"
|
15
|
-
hd = HumanDuration.new('type' => 'short')
|
16
|
-
test_value.each do |x|
|
17
|
-
printf "\t\t%s\n", hd.humanize(x)
|
18
|
-
end
|
19
9
|
|
20
|
-
puts "\tConfig: type = full"
|
21
|
-
hd = HumanDuration.new('type' => 'full')
|
22
|
-
test_value.each do |x|
|
23
|
-
printf "\t\t%s\n", hd.humanize(x)
|
24
|
-
end
|
25
|
-
|
26
|
-
puts 'Static method access'
|
27
|
-
|
28
|
-
printf "\tConfig: type = conpact [default]\n"
|
29
10
|
test_value.each do |x|
|
30
|
-
printf "\t\t%s\n",
|
11
|
+
printf "\t\t%s\n", HumanDuration::Duration.human_duration(x)
|
31
12
|
end
|
32
13
|
|
33
14
|
printf "\tConfig: type = short\n"
|
15
|
+
HumanDuration::Duration.display_type('short')
|
34
16
|
test_value.each do |x|
|
35
|
-
printf "\t\t%s\n",
|
17
|
+
printf "\t\t%s\n", HumanDuration::Duration.human_duration(x)
|
36
18
|
end
|
37
19
|
|
38
20
|
puts "\tConfig: type = full"
|
21
|
+
HumanDuration::Duration.display_type('full')
|
39
22
|
test_value.each do |x|
|
40
|
-
printf "\t\t%s\n",
|
23
|
+
printf "\t\t%s\n", HumanDuration::Duration.human_duration(x)
|
41
24
|
end
|
42
25
|
|
43
|
-
puts '
|
26
|
+
puts 'String Accessor Method'
|
44
27
|
|
45
28
|
printf "\tConfig: type = conpact [default]\n"
|
46
29
|
test_value.each do |x|
|
47
|
-
printf "\t\t%s\n",
|
30
|
+
printf "\t\t%s\n", x.human_duration
|
48
31
|
end
|
49
32
|
|
50
33
|
printf "\tConfig: type = short\n"
|
51
34
|
test_value.each do |x|
|
52
|
-
printf "\t\t%s\n",
|
35
|
+
printf "\t\t%s\n", x.human_duration('short')
|
53
36
|
end
|
54
37
|
|
55
38
|
puts "\tConfig: type = full"
|
56
39
|
test_value.each do |x|
|
57
|
-
printf "\t\t%s\n",
|
40
|
+
printf "\t\t%s\n", x.human_duration('full')
|
58
41
|
end
|
data/human_duration.gemspec
CHANGED
@@ -13,7 +13,12 @@ Gem::Specification.new do |spec|
|
|
13
13
|
spec.homepage = "https://github.com/WolfSoftware/human_duration"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
|
-
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
|
18
|
+
#spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
# `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
#end
|
21
|
+
|
17
22
|
spec.bindir = "exe"
|
18
23
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
24
|
spec.require_paths = ["lib"]
|
@@ -0,0 +1,5 @@
|
|
1
|
+
module HumanDuration
|
2
|
+
SPLIT_TYPES = [[60, 'second'], [60, 'minute'], [24, 'hour'], [365, 'day'], [1000, 'year']].freeze
|
3
|
+
SHORT_NAME_MAP = { 'second' => 's', 'minute' => 'm', 'hour' => 'h', 'day' => 'd', 'year' => 'y' }.freeze
|
4
|
+
VALID_TYPES = ['compact', 'short', 'full'].freeze
|
5
|
+
end
|
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
VERSION = '
|
1
|
+
module HumanDuration
|
2
|
+
VERSION = '2.0.0'.freeze
|
3
3
|
end
|
data/lib/human_duration.rb
CHANGED
@@ -1,128 +1,107 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
class HumanDurationStatic
|
25
|
-
def self.humanize(seconds, config = { 'type' => 'compact' })
|
26
|
-
hd = HumanDuration.new(config)
|
27
|
-
hd.humanize(seconds)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
# -------------------------------------------------------------------------------- #
|
32
|
-
# Description #
|
33
|
-
# -------------------------------------------------------------------------------- #
|
34
|
-
# This is the main class and the one that does all the real work and is called by #
|
35
|
-
# the static access class and the direct method access above. #
|
36
|
-
# -------------------------------------------------------------------------------- #
|
37
|
-
class HumanDuration
|
38
|
-
def initialize(config = { 'type' => 'compact' })
|
39
|
-
@config = config
|
40
|
-
|
41
|
-
@split_types = [[60, 'second'], [60, 'minute'], [24, 'hour'], [365, 'day'], [1000, 'year']]
|
42
|
-
@short_name_map = { 'second' => 's', 'minute' => 'm', 'hour' => 'h', 'day' => 'd', 'year' => 'y' }
|
43
|
-
end
|
44
|
-
|
45
|
-
def humanize(seconds)
|
46
|
-
reset
|
1
|
+
require_relative 'human_duration/constants'
|
2
|
+
require_relative 'human_duration/overloads'
|
3
|
+
require_relative 'human_duration/version'
|
4
|
+
|
5
|
+
#
|
6
|
+
# docs to do
|
7
|
+
#
|
8
|
+
module HumanDuration
|
9
|
+
#
|
10
|
+
# docs to do
|
11
|
+
#
|
12
|
+
class Duration
|
13
|
+
$time_values = {}
|
14
|
+
$item_count = 0
|
15
|
+
$output_buffer = ''
|
16
|
+
|
17
|
+
$type = 'compact'
|
18
|
+
|
19
|
+
def self.display_type(type = 'compact')
|
20
|
+
raise ArgumentError.new('Invalid type - Valid options are:- compact, short and full') unless VALID_TYPES.include? type
|
21
|
+
|
22
|
+
$type = type
|
23
|
+
end
|
47
24
|
|
48
|
-
|
49
|
-
|
25
|
+
def self.human_duration(seconds)
|
26
|
+
reset
|
50
27
|
|
51
|
-
|
52
|
-
|
53
|
-
generate_output
|
28
|
+
return 'negative' if seconds < 0
|
29
|
+
return 'now' if seconds.zero?
|
54
30
|
|
55
|
-
|
56
|
-
|
31
|
+
split_seconds(seconds)
|
32
|
+
count_items
|
33
|
+
generate_output
|
57
34
|
|
58
|
-
|
35
|
+
$output_buffer
|
36
|
+
end
|
59
37
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
38
|
+
def self.reset
|
39
|
+
$time_values = {}
|
40
|
+
$item_count = 0
|
41
|
+
$output_buffer = ''
|
42
|
+
end
|
65
43
|
|
66
|
-
|
67
|
-
|
44
|
+
def self.pluralize(number)
|
45
|
+
return 's' unless number == 1
|
68
46
|
|
69
|
-
|
70
|
-
|
47
|
+
''
|
48
|
+
end
|
71
49
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
50
|
+
def self.comma_or_not(count)
|
51
|
+
return ', ' if count > 1
|
52
|
+
return ' & ' if count == 1 && $type == 'short'
|
53
|
+
return ' and ' if count == 1
|
76
54
|
|
77
|
-
|
78
|
-
|
55
|
+
''
|
56
|
+
end
|
79
57
|
|
80
|
-
|
81
|
-
|
58
|
+
def self.add_time(value, name, plural = true)
|
59
|
+
return if value < 1 && $type != 'full'
|
82
60
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
61
|
+
$output_buffer << if plural
|
62
|
+
format('%<value>s %<name>s%<plural>s', value: value, name: name, plural: pluralize(value))
|
63
|
+
else
|
64
|
+
format('%<value>s %<name>s', value: value, name: name)
|
65
|
+
end
|
66
|
+
$item_count -= 1
|
67
|
+
$output_buffer << comma_or_not($item_count)
|
68
|
+
end
|
91
69
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
70
|
+
def self.split_seconds(seconds)
|
71
|
+
SPLIT_TYPES.map do |count, name|
|
72
|
+
if seconds > 0
|
73
|
+
seconds, n = seconds.divmod(count)
|
74
|
+
$time_values[name] = n
|
75
|
+
else
|
76
|
+
$time_values[name] = 0
|
77
|
+
end
|
99
78
|
end
|
100
79
|
end
|
101
|
-
end
|
102
80
|
|
103
|
-
|
104
|
-
|
105
|
-
|
81
|
+
def self.count_items
|
82
|
+
$time_values.each do |_name, value|
|
83
|
+
$item_count += 1 if value > 0 || $type == 'full'
|
84
|
+
end
|
106
85
|
end
|
107
|
-
end
|
108
86
|
|
109
|
-
|
110
|
-
|
111
|
-
|
87
|
+
def self.add_time_long
|
88
|
+
$time_values.reverse_each do |name, value|
|
89
|
+
add_time(value, name)
|
90
|
+
end
|
112
91
|
end
|
113
|
-
end
|
114
92
|
|
115
|
-
|
116
|
-
|
117
|
-
|
93
|
+
def self.add_time_short
|
94
|
+
$time_values.reverse_each do |name, value|
|
95
|
+
add_time(value, SHORT_NAME_MAP[name], false)
|
96
|
+
end
|
118
97
|
end
|
119
|
-
end
|
120
98
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
99
|
+
def self.generate_output
|
100
|
+
if $type == 'short'
|
101
|
+
add_time_short
|
102
|
+
else
|
103
|
+
add_time_long
|
104
|
+
end
|
126
105
|
end
|
127
106
|
end
|
128
107
|
end
|
data/spec/human_duration_spec.rb
CHANGED
@@ -7,339 +7,235 @@ RSpec.describe HumanDuration do
|
|
7
7
|
expect(HumanDuration::VERSION).not_to be nil
|
8
8
|
end
|
9
9
|
|
10
|
-
context 'Class
|
10
|
+
context 'Class Accessor Method' do
|
11
11
|
context 'Using default compact mode' do
|
12
12
|
before(:each) do
|
13
|
-
@hd = HumanDuration
|
13
|
+
@hd = HumanDuration::Duration
|
14
14
|
end
|
15
15
|
|
16
|
-
it 'says negative for seconds < 0 (Negative value test)' do
|
17
|
-
expect(@hd.
|
16
|
+
it 'says "negative" for seconds < 0 (Negative value test)' do
|
17
|
+
expect(@hd.human_duration(-1)).to eql('negative')
|
18
18
|
end
|
19
19
|
|
20
|
-
it 'says now for 0 seconds (Zero second test)' do
|
21
|
-
expect(@hd.
|
20
|
+
it 'says "now" for 0 seconds (Zero second test)' do
|
21
|
+
expect(@hd.human_duration(0)).to eql('now')
|
22
22
|
end
|
23
23
|
|
24
|
-
it 'says
|
25
|
-
expect(@hd.
|
24
|
+
it 'says "1 second" for 1 second (Singular test)' do
|
25
|
+
expect(@hd.human_duration(1)).to eql('1 second')
|
26
26
|
end
|
27
27
|
|
28
|
-
it 'says
|
29
|
-
expect(@hd.
|
28
|
+
it 'says "10 seconds" for 10 second (Plural test)' do
|
29
|
+
expect(@hd.human_duration(10)).to eql('10 seconds')
|
30
30
|
end
|
31
31
|
|
32
|
-
it 'says
|
33
|
-
expect(@hd.
|
32
|
+
it 'says "1 minute" for 60 seconds' do
|
33
|
+
expect(@hd.human_duration(60)).to eql('1 minute')
|
34
34
|
end
|
35
35
|
|
36
|
-
it 'says
|
37
|
-
expect(@hd.
|
36
|
+
it 'says "1 hour" for 3600 seconds' do
|
37
|
+
expect(@hd.human_duration(360_0)).to eql('1 hour')
|
38
38
|
end
|
39
39
|
|
40
|
-
it 'says
|
41
|
-
expect(@hd.
|
40
|
+
it 'says "1 year" for 31536000 seconds' do
|
41
|
+
expect(@hd.human_duration(315_360_00)).to eql('1 year')
|
42
42
|
end
|
43
43
|
|
44
|
-
it 'says
|
45
|
-
expect(@hd.
|
44
|
+
it 'says "1 year, 2 hours and 13 seconds" for 31543213 seconds' do
|
45
|
+
expect(@hd.human_duration(315_432_13)).to eql('1 year, 2 hours and 13 seconds')
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
49
|
context 'Using short mode' do
|
50
50
|
before(:each) do
|
51
|
-
@hd = HumanDuration
|
51
|
+
@hd = HumanDuration::Duration
|
52
|
+
@hd.display_type('short')
|
52
53
|
end
|
53
54
|
|
54
|
-
it 'says negative for seconds < 0 (Negative value test)' do
|
55
|
-
expect(@hd.
|
55
|
+
it 'says "negative" for seconds < 0 (Negative value test)' do
|
56
|
+
expect(@hd.human_duration(-1)).to eql('negative')
|
56
57
|
end
|
57
58
|
|
58
|
-
it 'says now for 0 seconds (Zero second test)' do
|
59
|
-
expect(@hd.
|
59
|
+
it 'says "now" for 0 seconds (Zero second test)' do
|
60
|
+
expect(@hd.human_duration(0)).to eql('now')
|
60
61
|
end
|
61
62
|
|
62
|
-
it 'says
|
63
|
-
expect(@hd.
|
63
|
+
it 'says "1 s" for 1 second (Singular test)' do
|
64
|
+
expect(@hd.human_duration(1)).to eql('1 s')
|
64
65
|
end
|
65
66
|
|
66
|
-
it 'says
|
67
|
-
expect(@hd.
|
67
|
+
it 'says "10 s" for 10 second (Plural test)' do
|
68
|
+
expect(@hd.human_duration(10)).to eql('10 s')
|
68
69
|
end
|
69
70
|
|
70
|
-
it 'says
|
71
|
-
expect(@hd.
|
71
|
+
it 'says "1 m" for 60 seconds' do
|
72
|
+
expect(@hd.human_duration(60)).to eql('1 m')
|
72
73
|
end
|
73
74
|
|
74
|
-
it 'says
|
75
|
-
expect(@hd.
|
75
|
+
it 'says "1 h" for 3600 seconds' do
|
76
|
+
expect(@hd.human_duration(360_0)).to eql('1 h')
|
76
77
|
end
|
77
78
|
|
78
|
-
it 'says
|
79
|
-
expect(@hd.
|
79
|
+
it 'says "1 y" for 31536000 seconds' do
|
80
|
+
expect(@hd.human_duration(315_360_00)).to eql('1 y')
|
80
81
|
end
|
81
82
|
|
82
|
-
it 'says
|
83
|
-
expect(@hd.
|
83
|
+
it 'says "1 y, 2 h & 13 s" for 31543213 seconds' do
|
84
|
+
expect(@hd.human_duration(315_432_13)).to eql('1 y, 2 h & 13 s')
|
84
85
|
end
|
85
86
|
end
|
86
87
|
|
87
88
|
context 'Using full mode' do
|
88
89
|
before(:each) do
|
89
|
-
@hd = HumanDuration
|
90
|
+
@hd = HumanDuration::Duration
|
91
|
+
@hd.display_type('full')
|
90
92
|
end
|
91
93
|
|
92
|
-
it 'says negative for seconds < 0 (Negative value test)' do
|
93
|
-
expect(@hd.
|
94
|
+
it 'says "negative" for seconds < 0 (Negative value test)' do
|
95
|
+
expect(@hd.human_duration(-1)).to eql('negative')
|
94
96
|
end
|
95
97
|
|
96
|
-
it 'says now for 0 seconds (Zero second test)' do
|
97
|
-
expect(@hd.
|
98
|
+
it 'says "now" for 0 seconds (Zero second test)' do
|
99
|
+
expect(@hd.human_duration(0)).to eql('now')
|
98
100
|
end
|
99
101
|
|
100
|
-
it 'says
|
101
|
-
expect(@hd.
|
102
|
+
it 'says "0 years, 0 days, 0 hours, 0 minutes and 1 second" for 1 second (Singular test)' do
|
103
|
+
expect(@hd.human_duration(1)).to eql('0 years, 0 days, 0 hours, 0 minutes and 1 second')
|
102
104
|
end
|
103
105
|
|
104
|
-
it 'says
|
105
|
-
expect(@hd.
|
106
|
+
it 'says "0 years, 0 days, 0 hours, 0 minutes and 10 seconds" for 10 second (Plural test)' do
|
107
|
+
expect(@hd.human_duration(10)).to eql('0 years, 0 days, 0 hours, 0 minutes and 10 seconds')
|
106
108
|
end
|
107
109
|
|
108
|
-
it 'says
|
109
|
-
expect(@hd.
|
110
|
+
it 'says "0 years, 0 days, 0 hours, 1 minute and 0 seconds" for 60 seconds' do
|
111
|
+
expect(@hd.human_duration(60)).to eql('0 years, 0 days, 0 hours, 1 minute and 0 seconds')
|
110
112
|
end
|
111
113
|
|
112
|
-
it 'says
|
113
|
-
expect(@hd.
|
114
|
+
it 'says "0 years, 0 days, 1 hour, 0 minutes and 0 seconds" for 3600 seconds' do
|
115
|
+
expect(@hd.human_duration(360_0)).to eql('0 years, 0 days, 1 hour, 0 minutes and 0 seconds')
|
114
116
|
end
|
115
117
|
|
116
|
-
it 'says
|
117
|
-
expect(@hd.
|
118
|
+
it 'says "1 year, 0 days, 0 hours, 0 minutes and 0 seconds" for 31536000 seconds' do
|
119
|
+
expect(@hd.human_duration(315_360_00)).to eql('1 year, 0 days, 0 hours, 0 minutes and 0 seconds')
|
118
120
|
end
|
119
121
|
|
120
|
-
it 'says
|
121
|
-
expect(@hd.
|
122
|
+
it 'says "1 year, 0 days, 2 hours, 0 minutes and 13 seconds" for 31543213 seconds' do
|
123
|
+
expect(@hd.human_duration(315_432_13)).to eql('1 year, 0 days, 2 hours, 0 minutes and 13 seconds')
|
122
124
|
end
|
123
125
|
end
|
124
126
|
end
|
125
127
|
|
126
|
-
context '
|
128
|
+
context 'String Accessor Method' do
|
127
129
|
context 'Using default compact mode' do
|
128
|
-
|
129
|
-
|
130
|
-
end
|
131
|
-
|
132
|
-
it 'says negative for seconds < 0 (Negative value test)' do
|
133
|
-
expect(@hd.humanize(-1)).to eql('negative')
|
130
|
+
it 'says "negative" for seconds < 0 (Negative value test)' do
|
131
|
+
expect(-1.human_duration).to eql('negative')
|
134
132
|
end
|
135
133
|
|
136
|
-
it 'says now for 0 seconds (Zero second test)' do
|
137
|
-
expect(
|
134
|
+
it 'says "now" for 0 seconds (Zero second test)' do
|
135
|
+
expect(0.human_duration).to eql('now')
|
138
136
|
end
|
139
137
|
|
140
|
-
it 'says
|
141
|
-
expect(
|
138
|
+
it 'says "1 second" for 1 second (Singular test)' do
|
139
|
+
expect(1.human_duration).to eql('1 second')
|
142
140
|
end
|
143
141
|
|
144
|
-
it 'says
|
145
|
-
expect(
|
142
|
+
it 'says "10 seconds" for 10 second (Plural test)' do
|
143
|
+
expect(10.human_duration).to eql('10 seconds')
|
146
144
|
end
|
147
145
|
|
148
|
-
it 'says
|
149
|
-
expect(
|
146
|
+
it 'says "1 minute" for 60 seconds' do
|
147
|
+
expect(60.human_duration).to eql('1 minute')
|
150
148
|
end
|
151
149
|
|
152
|
-
it 'says
|
153
|
-
expect(
|
150
|
+
it 'says "1 hour" for 3600 seconds' do
|
151
|
+
expect(360_0.human_duration).to eql('1 hour')
|
154
152
|
end
|
155
153
|
|
156
|
-
it 'says
|
157
|
-
expect(
|
154
|
+
it 'says "1 year" for 31536000 seconds' do
|
155
|
+
expect(315_360_00.human_duration).to eql('1 year')
|
158
156
|
end
|
159
157
|
|
160
|
-
it 'says
|
161
|
-
expect(
|
158
|
+
it 'says "1 year, 2 hours and 13 seconds" for 31543213 seconds' do
|
159
|
+
expect(315_432_13.human_duration).to eql('1 year, 2 hours and 13 seconds')
|
162
160
|
end
|
163
161
|
end
|
164
162
|
|
165
163
|
context 'Using short mode' do
|
166
|
-
|
167
|
-
|
164
|
+
it 'says "negative" for seconds < 0 (Negative value test)' do
|
165
|
+
expect(-1.human_duration('short')).to eql('negative')
|
168
166
|
end
|
169
167
|
|
170
|
-
it 'says
|
171
|
-
expect(
|
168
|
+
it 'says "now" for 0 seconds (Zero second test)' do
|
169
|
+
expect(0.human_duration('short')).to eql('now')
|
172
170
|
end
|
173
171
|
|
174
|
-
it 'says
|
175
|
-
expect(
|
172
|
+
it 'says "1 s" for 1 second (Singular test)' do
|
173
|
+
expect(1.human_duration('short')).to eql('1 s')
|
176
174
|
end
|
177
175
|
|
178
|
-
it 'says
|
179
|
-
expect(
|
176
|
+
it 'says "10 s" for 10 second (Plural test)' do
|
177
|
+
expect(10.human_duration('short')).to eql('10 s')
|
180
178
|
end
|
181
179
|
|
182
|
-
it 'says
|
183
|
-
expect(
|
180
|
+
it 'says "1 m" for 60 seconds' do
|
181
|
+
expect(60.human_duration('short')).to eql('1 m')
|
184
182
|
end
|
185
183
|
|
186
|
-
it 'says
|
187
|
-
expect(
|
184
|
+
it 'says "1 h" for 3600 seconds' do
|
185
|
+
expect(360_0.human_duration('short')).to eql('1 h')
|
188
186
|
end
|
189
187
|
|
190
|
-
it 'says
|
191
|
-
expect(
|
188
|
+
it 'says "1 y" for 31536000 seconds' do
|
189
|
+
expect(315_360_00.human_duration('short')).to eql('1 y')
|
192
190
|
end
|
193
191
|
|
194
|
-
it 'says
|
195
|
-
expect(
|
196
|
-
end
|
197
|
-
|
198
|
-
it 'says \'1 y, 2 h & 13 s\' for 31543213 seconds' do
|
199
|
-
expect(@hd.humanize(315_432_13, 'type' => 'short')).to eql('1 y, 2 h & 13 s')
|
192
|
+
it 'says "1 y, 2 h & 13 s" for 31543213 seconds' do
|
193
|
+
expect(315_432_13.human_duration('short')).to eql('1 y, 2 h & 13 s')
|
200
194
|
end
|
201
195
|
end
|
202
196
|
|
203
197
|
context 'Using full mode' do
|
204
|
-
|
205
|
-
|
198
|
+
it 'says "negative" for seconds < 0 (Negative value test)' do
|
199
|
+
expect(-1.human_duration('full')).to eql('negative')
|
206
200
|
end
|
207
201
|
|
208
|
-
it 'says
|
209
|
-
expect(
|
202
|
+
it 'says "now" for 0 seconds (Zero second test)' do
|
203
|
+
expect(0.human_duration('full')).to eql('now')
|
210
204
|
end
|
211
205
|
|
212
|
-
it 'says
|
213
|
-
expect(
|
206
|
+
it 'says "0 years, 0 days, 0 hours, 0 minutes and 1 second" for 1 second (Singular test)' do
|
207
|
+
expect(1.human_duration('full')).to eql('0 years, 0 days, 0 hours, 0 minutes and 1 second')
|
214
208
|
end
|
215
209
|
|
216
|
-
it 'says
|
217
|
-
expect(
|
210
|
+
it 'says "0 years, 0 days, 0 hours, 0 minutes and 10 seconds" for 10 second (Plural test)' do
|
211
|
+
expect(10.human_duration('full')).to eql('0 years, 0 days, 0 hours, 0 minutes and 10 seconds')
|
218
212
|
end
|
219
213
|
|
220
|
-
it 'says
|
221
|
-
expect(
|
214
|
+
it 'says "0 years, 0 days, 0 hours, 1 minute and 0 seconds" for 60 seconds' do
|
215
|
+
expect(60.human_duration('full')).to eql('0 years, 0 days, 0 hours, 1 minute and 0 seconds')
|
222
216
|
end
|
223
217
|
|
224
|
-
it 'says
|
225
|
-
expect(
|
218
|
+
it 'says "0 years, 0 days, 1 hour, 0 minutes and 0 seconds" for 3600 seconds' do
|
219
|
+
expect(360_0.human_duration('full')).to eql('0 years, 0 days, 1 hour, 0 minutes and 0 seconds')
|
226
220
|
end
|
227
221
|
|
228
|
-
it 'says
|
229
|
-
expect(
|
222
|
+
it 'says "1 year, 0 days, 0 hours, 0 minutes and 0 seconds" for 31536000 seconds' do
|
223
|
+
expect(315_360_00.human_duration('full')).to eql('1 year, 0 days, 0 hours, 0 minutes and 0 seconds')
|
230
224
|
end
|
231
225
|
|
232
|
-
it 'says
|
233
|
-
expect(
|
234
|
-
end
|
235
|
-
|
236
|
-
it 'says \'1 year, 0 days, 2 hours, 0 minutes and 13 seconds\' for 31543213 seconds' do
|
237
|
-
expect(@hd.humanize(315_432_13, 'type' => 'full')).to eql('1 year, 0 days, 2 hours, 0 minutes and 13 seconds')
|
226
|
+
it 'says "1 year, 0 days, 2 hours, 0 minutes and 13 seconds" for 31543213 seconds' do
|
227
|
+
expect(315_432_13.human_duration('full')).to eql('1 year, 0 days, 2 hours, 0 minutes and 13 seconds')
|
238
228
|
end
|
239
229
|
end
|
240
230
|
end
|
241
231
|
|
242
|
-
context '
|
243
|
-
|
244
|
-
|
245
|
-
expect(humanize(-1)).to eql('negative')
|
246
|
-
end
|
247
|
-
|
248
|
-
it 'says now for 0 seconds (Zero second test)' do
|
249
|
-
expect(humanize(0)).to eql('now')
|
250
|
-
end
|
251
|
-
|
252
|
-
it 'says \'1 second\' for 1 second (Singular test)' do
|
253
|
-
expect(humanize(1)).to eql('1 second')
|
254
|
-
end
|
255
|
-
|
256
|
-
it 'says \'10 seconds\' for 10 second (Plural test)' do
|
257
|
-
expect(humanize(10)).to eql('10 seconds')
|
258
|
-
end
|
259
|
-
|
260
|
-
it 'says \'1 minute\' for 60 seconds' do
|
261
|
-
expect(humanize(60)).to eql('1 minute')
|
262
|
-
end
|
263
|
-
|
264
|
-
it 'says \'1 hour\' for 3600 seconds' do
|
265
|
-
expect(humanize(360_0)).to eql('1 hour')
|
266
|
-
end
|
267
|
-
|
268
|
-
it 'says \'1 year\' for 31536000 seconds' do
|
269
|
-
expect(humanize(315_360_00)).to eql('1 year')
|
270
|
-
end
|
271
|
-
|
272
|
-
it 'says \'1 year, 2 hours and 13 seconds\' for 31543213 seconds' do
|
273
|
-
expect(humanize(315_432_13)).to eql('1 year, 2 hours and 13 seconds')
|
274
|
-
end
|
232
|
+
context 'Test Error Handling' do
|
233
|
+
it 'Test invalid display type raises an exception direct call' do
|
234
|
+
expect { HumanDuration::Duration.display_type('invalid') }.to raise_error(ArgumentError, 'Invalid type - Valid options are:- compact, short and full')
|
275
235
|
end
|
276
236
|
|
277
|
-
|
278
|
-
|
279
|
-
expect(humanize(-1, 'type' => 'short')).to eql('negative')
|
280
|
-
end
|
281
|
-
|
282
|
-
it 'says now for 0 seconds (Zero second test)' do
|
283
|
-
expect(humanize(0, 'type' => 'short')).to eql('now')
|
284
|
-
end
|
285
|
-
|
286
|
-
it 'says \'1 s\' for 1 second (Singular test)' do
|
287
|
-
expect(humanize(1, 'type' => 'short')).to eql('1 s')
|
288
|
-
end
|
289
|
-
|
290
|
-
it 'says \'10 s\' for 10 second (Plural test)' do
|
291
|
-
expect(humanize(10, 'type' => 'short')).to eql('10 s')
|
292
|
-
end
|
293
|
-
|
294
|
-
it 'says \'1 m\' for 60 seconds' do
|
295
|
-
expect(humanize(60, 'type' => 'short')).to eql('1 m')
|
296
|
-
end
|
297
|
-
|
298
|
-
it 'says \'1 h\' for 3600 seconds' do
|
299
|
-
expect(humanize(360_0, 'type' => 'short')).to eql('1 h')
|
300
|
-
end
|
301
|
-
|
302
|
-
it 'says \'1 y\' for 31536000 seconds' do
|
303
|
-
expect(humanize(315_360_00, 'type' => 'short')).to eql('1 y')
|
304
|
-
end
|
305
|
-
|
306
|
-
it 'says \'1 y, 2 h & 13 s\' for 31543213 seconds' do
|
307
|
-
expect(humanize(315_432_13, 'type' => 'short')).to eql('1 y, 2 h & 13 s')
|
308
|
-
end
|
309
|
-
end
|
310
|
-
|
311
|
-
context 'Using full mode' do
|
312
|
-
it 'says negative for seconds < 0 (Negative value test)' do
|
313
|
-
expect(humanize(-1, 'type' => 'full')).to eql('negative')
|
314
|
-
end
|
315
|
-
|
316
|
-
it 'says now for 0 seconds (Zero second test)' do
|
317
|
-
expect(humanize(0, 'type' => 'full')).to eql('now')
|
318
|
-
end
|
319
|
-
|
320
|
-
it 'says \'0 years, 0 days, 0 hours, 0 minutes and 1 second\' for 1 second (Singular test)' do
|
321
|
-
expect(humanize(1, 'type' => 'full')).to eql('0 years, 0 days, 0 hours, 0 minutes and 1 second')
|
322
|
-
end
|
323
|
-
|
324
|
-
it 'says \'0 years, 0 days, 0 hours, 0 minutes and 10 seconds\' for 10 second (Plural test)' do
|
325
|
-
expect(humanize(10, 'type' => 'full')).to eql('0 years, 0 days, 0 hours, 0 minutes and 10 seconds')
|
326
|
-
end
|
327
|
-
|
328
|
-
it 'says \'0 years, 0 days, 0 hours, 1 minute and 0 seconds\' for 60 seconds' do
|
329
|
-
expect(humanize(60, 'type' => 'full')).to eql('0 years, 0 days, 0 hours, 1 minute and 0 seconds')
|
330
|
-
end
|
331
|
-
|
332
|
-
it 'says \'0 years, 0 days, 1 hour, 0 minutes and 0 seconds\' for 3600 seconds' do
|
333
|
-
expect(humanize(360_0, 'type' => 'full')).to eql('0 years, 0 days, 1 hour, 0 minutes and 0 seconds')
|
334
|
-
end
|
335
|
-
|
336
|
-
it 'says \'1 year, 0 days, 0 hours, 0 minutes and 0 seconds\' for 31536000 seconds' do
|
337
|
-
expect(humanize(315_360_00, 'type' => 'full')).to eql('1 year, 0 days, 0 hours, 0 minutes and 0 seconds')
|
338
|
-
end
|
339
|
-
|
340
|
-
it 'says \'1 year, 0 days, 2 hours, 0 minutes and 13 seconds\' for 31543213 seconds' do
|
341
|
-
expect(humanize(315_432_13, 'type' => 'full')).to eql('1 year, 0 days, 2 hours, 0 minutes and 13 seconds')
|
342
|
-
end
|
237
|
+
it 'Test invalid display type raises an exception indirect call' do
|
238
|
+
expect { 0.human_duration('invalid') }.to raise_error(ArgumentError, 'Invalid type - Valid options are:- compact, short and full')
|
343
239
|
end
|
344
240
|
end
|
345
241
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: human_duration
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tim Gurney aka Wolf
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-01-
|
11
|
+
date: 2019-01-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -76,6 +76,8 @@ files:
|
|
76
76
|
- example/example.rb
|
77
77
|
- human_duration.gemspec
|
78
78
|
- lib/human_duration.rb
|
79
|
+
- lib/human_duration/constants.rb
|
80
|
+
- lib/human_duration/overloads.rb
|
79
81
|
- lib/human_duration/version.rb
|
80
82
|
- spec/human_duration_spec.rb
|
81
83
|
- spec/spec_helper.rb
|