human_duration 1.0.1 → 2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 102134bea90036b011d22347bc8f8470f4b4dd15772fd9b9cde82e99e2592245
4
- data.tar.gz: 667534f6929deba4722e44a773f7420ea8eab1b35f02df893cac4b2b7e70952b
3
+ metadata.gz: 986006379227db9abe3115b6d5e8a60eec3db6a7c475227051174b7680d76074
4
+ data.tar.gz: cf362347d7a8277e96fc164a8a7fb5488376f988cdb0e3fd4cb83a776435687b
5
5
  SHA512:
6
- metadata.gz: d79a19c23c24ae488e85a95a4d25303894e945575173f04fc1d754a5847aea58e96475bd36de0ba629a358367e1308aec8672a514ff17b686e539b3abdd6eda8
7
- data.tar.gz: dee5232b143c5a3a6d24a0c764a3d9374e40cd0d1d0433ff88117804bb0e2ae5862f090c345bf02d553ee8c0d05bbc5ade3acd44ae96027a7c5421458c280574
6
+ metadata.gz: 18e9f783a647a76c66452ca017e638163d7b90680037e8c5a7185a457d81b611e0b867e2adc734a638a3bbc860316faba101cecab4b79572c066b993368796b2
7
+ data.tar.gz: e4966f937a6820d76b8cbd5494520bb66265c39024347e3bc9dad0bca4d7f2db270e895eb355a91664ceaa88e9961d0f9aebfb54a734b9d1f444ac8f1568b977
data/.gitignore CHANGED
@@ -10,3 +10,5 @@
10
10
 
11
11
  # rspec failure tracking
12
12
  .rspec_status
13
+
14
+ *.gem
data/.travis.yml CHANGED
@@ -8,7 +8,7 @@ matrix:
8
8
  - language: ruby
9
9
  rvm: 2.4.4
10
10
  before_install:
11
- - gem install bundler -v 1.16.1
11
+ - gem install bundler -v 1.17.3
12
12
  - language: ruby
13
13
  env: SKIP_INTERPRETER=true
14
14
 
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
- #!/usr/bin/env ruby
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 'Class method access'
34
+ puts HumanDuration::Duration.human_duration(10)
47
35
 
48
- hd = HumanDuration.new
49
- test_value.each do |x|
50
- puts hd.humanize(x)
51
- end
36
+ # or
52
37
 
53
- puts 'Static method access'
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
- puts 'Config: default'
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 method access'
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", HumanDurationStatic.humanize(x)
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", HumanDurationStatic.humanize(x)
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", HumanDurationStatic.humanize(x)
23
+ printf "\t\t%s\n", HumanDuration::Duration.human_duration(x)
41
24
  end
42
25
 
43
- puts 'Direct method access'
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", humanize(x)
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", humanize(x)
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", humanize(x)
40
+ printf "\t\t%s\n", x.human_duration('full')
58
41
  end
@@ -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
- spec.files = `git ls-files`.split($/)
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
@@ -0,0 +1,9 @@
1
+ #
2
+ # Overload the integer class
3
+ #
4
+ class Integer
5
+ def human_duration(type = 'compact')
6
+ HumanDuration::Duration.display_type(type)
7
+ HumanDuration::Duration.human_duration(self)
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
- class HumanDuration
2
- VERSION = '1.0.1'.freeze
1
+ module HumanDuration
2
+ VERSION = '2.0.0'.freeze
3
3
  end
@@ -1,128 +1,107 @@
1
- require 'human_duration/version'
2
-
3
- # -------------------------------------------------------------------------------- #
4
- # Description #
5
- # -------------------------------------------------------------------------------- #
6
- # Display elapsed time in a more human readable way. #
7
- # -------------------------------------------------------------------------------- #
8
-
9
- # -------------------------------------------------------------------------------- #
10
- # Description #
11
- # -------------------------------------------------------------------------------- #
12
- # This is a simple global method to short cut the usage of the class below. #
13
- # -------------------------------------------------------------------------------- #
14
- def humanize(seconds, config = { 'type' => 'compact' })
15
- hd = HumanDuration.new(config)
16
- hd.humanize(seconds)
17
- end
18
-
19
- # -------------------------------------------------------------------------------- #
20
- # Description #
21
- # -------------------------------------------------------------------------------- #
22
- # This is a static access wrapper to the main HumanDuration class below. #
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
- return 'negative' if seconds < 0
49
- return 'now' if seconds.zero?
25
+ def self.human_duration(seconds)
26
+ reset
50
27
 
51
- split_seconds(seconds)
52
- count_items
53
- generate_output
28
+ return 'negative' if seconds < 0
29
+ return 'now' if seconds.zero?
54
30
 
55
- @output_buffer
56
- end
31
+ split_seconds(seconds)
32
+ count_items
33
+ generate_output
57
34
 
58
- private
35
+ $output_buffer
36
+ end
59
37
 
60
- def reset
61
- @time_values = {}
62
- @item_count = 0
63
- @output_buffer = ''
64
- end
38
+ def self.reset
39
+ $time_values = {}
40
+ $item_count = 0
41
+ $output_buffer = ''
42
+ end
65
43
 
66
- def pluralize(number)
67
- return 's' unless number == 1
44
+ def self.pluralize(number)
45
+ return 's' unless number == 1
68
46
 
69
- ''
70
- end
47
+ ''
48
+ end
71
49
 
72
- def comma_or_not(count)
73
- return ', ' if count > 1
74
- return ' & ' if count == 1 && @config['type'] == 'short'
75
- return ' and ' if count == 1
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
- end
55
+ ''
56
+ end
79
57
 
80
- def add_time(value, name, plural = true)
81
- return if value < 1 && @config['type'] != 'full'
58
+ def self.add_time(value, name, plural = true)
59
+ return if value < 1 && $type != 'full'
82
60
 
83
- @output_buffer << if plural
84
- format('%<value>s %<name>s%<plural>s', value: value, name: name, plural: pluralize(value))
85
- else
86
- format('%<value>s %<name>s', value: value, name: name)
87
- end
88
- @item_count -= 1
89
- @output_buffer << comma_or_not(@item_count)
90
- end
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
- def split_seconds(seconds)
93
- @split_types.map do |count, name|
94
- if seconds > 0
95
- seconds, n = seconds.divmod(count)
96
- @time_values[name] = n
97
- else
98
- @time_values[name] = 0
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
- def count_items
104
- @time_values.each do |_name, value|
105
- @item_count += 1 if value > 0 || @config['type'] == 'full'
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
- def add_time_long
110
- @time_values.reverse_each do |name, value|
111
- add_time(value, name)
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
- def add_time_short
116
- @time_values.reverse_each do |name, value|
117
- add_time(value, @short_name_map[name], false)
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
- def generate_output
122
- if @config['type'] == 'short'
123
- add_time_short
124
- else
125
- add_time_long
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
@@ -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 method access' do
10
+ context 'Class Accessor Method' do
11
11
  context 'Using default compact mode' do
12
12
  before(:each) do
13
- @hd = HumanDuration.new
13
+ @hd = HumanDuration::Duration
14
14
  end
15
15
 
16
- it 'says negative for seconds < 0 (Negative value test)' do
17
- expect(@hd.humanize(-1)).to eql('negative')
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.humanize(0)).to eql('now')
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 \'1 second\' for 1 second (Singular test)' do
25
- expect(@hd.humanize(1)).to eql('1 second')
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 \'10 seconds\' for 10 second (Plural test)' do
29
- expect(@hd.humanize(10)).to eql('10 seconds')
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 \'1 minute\' for 60 seconds' do
33
- expect(@hd.humanize(60)).to eql('1 minute')
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 \'1 hour\' for 3600 seconds' do
37
- expect(@hd.humanize(360_0)).to eql('1 hour')
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 \'1 year\' for 31536000 seconds' do
41
- expect(@hd.humanize(315_360_00)).to eql('1 year')
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 \'1 year, 2 hours and 13 seconds\' for 31543213 seconds' do
45
- expect(@hd.humanize(315_432_13)).to eql('1 year, 2 hours and 13 seconds')
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.new('type' => 'short')
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.humanize(-1)).to eql('negative')
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.humanize(0)).to eql('now')
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 \'1 s\' for 1 second (Singular test)' do
63
- expect(@hd.humanize(1)).to eql('1 s')
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 \'10 s\' for 10 second (Plural test)' do
67
- expect(@hd.humanize(10)).to eql('10 s')
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 \'1 m\' for 60 seconds' do
71
- expect(@hd.humanize(60)).to eql('1 m')
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 \'1 h\' for 3600 seconds' do
75
- expect(@hd.humanize(360_0)).to eql('1 h')
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 \'1 y\' for 31536000 seconds' do
79
- expect(@hd.humanize(315_360_00)).to eql('1 y')
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 \'1 y, 2 h & 13 s\' for 31543213 seconds' do
83
- expect(@hd.humanize(315_432_13)).to eql('1 y, 2 h & 13 s')
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.new('type' => 'full')
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.humanize(-1)).to eql('negative')
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.humanize(0)).to eql('now')
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 \'0 years, 0 days, 0 hours, 0 minutes and 1 second\' for 1 second (Singular test)' do
101
- expect(@hd.humanize(1)).to eql('0 years, 0 days, 0 hours, 0 minutes and 1 second')
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 \'0 years, 0 days, 0 hours, 0 minutes and 10 seconds\' for 10 second (Plural test)' do
105
- expect(@hd.humanize(10)).to eql('0 years, 0 days, 0 hours, 0 minutes and 10 seconds')
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 \'0 years, 0 days, 0 hours, 1 minute and 0 seconds\' for 60 seconds' do
109
- expect(@hd.humanize(60)).to eql('0 years, 0 days, 0 hours, 1 minute and 0 seconds')
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 \'0 years, 0 days, 1 hour, 0 minutes and 0 seconds\' for 3600 seconds' do
113
- expect(@hd.humanize(360_0)).to eql('0 years, 0 days, 1 hour, 0 minutes and 0 seconds')
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 \'1 year, 0 days, 0 hours, 0 minutes and 0 seconds\' for 31536000 seconds' do
117
- expect(@hd.humanize(315_360_00)).to eql('1 year, 0 days, 0 hours, 0 minutes and 0 seconds')
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 \'1 year, 0 days, 2 hours, 0 minutes and 13 seconds\' for 31543213 seconds' do
121
- expect(@hd.humanize(315_432_13)).to eql('1 year, 0 days, 2 hours, 0 minutes and 13 seconds')
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 'Static method access' do
128
+ context 'String Accessor Method' do
127
129
  context 'Using default compact mode' do
128
- before(:each) do
129
- @hd = HumanDurationStatic
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(@hd.humanize(0)).to eql('now')
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 \'1 second\' for 1 second (Singular test)' do
141
- expect(@hd.humanize(1)).to eql('1 second')
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 \'10 seconds\' for 10 second (Plural test)' do
145
- expect(@hd.humanize(10)).to eql('10 seconds')
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 \'1 minute\' for 60 seconds' do
149
- expect(@hd.humanize(60)).to eql('1 minute')
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 \'1 hour\' for 3600 seconds' do
153
- expect(@hd.humanize(360_0)).to eql('1 hour')
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 \'1 year\' for 31536000 seconds' do
157
- expect(@hd.humanize(315_360_00)).to eql('1 year')
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 \'1 year, 2 hours and 13 seconds\' for 31543213 seconds' do
161
- expect(@hd.humanize(315_432_13)).to eql('1 year, 2 hours and 13 seconds')
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
- before(:each) do
167
- @hd = HumanDurationStatic
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 negative for seconds < 0 (Negative value test)' do
171
- expect(@hd.humanize(-1, 'type' => 'short')).to eql('negative')
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 now for 0 seconds (Zero second test)' do
175
- expect(@hd.humanize(0, 'type' => 'short')).to eql('now')
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 \'1 s\' for 1 second (Singular test)' do
179
- expect(@hd.humanize(1, 'type' => 'short')).to eql('1 s')
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 \'10 s\' for 10 second (Plural test)' do
183
- expect(@hd.humanize(10, 'type' => 'short')).to eql('10 s')
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 \'1 m\' for 60 seconds' do
187
- expect(@hd.humanize(60, 'type' => 'short')).to eql('1 m')
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 \'1 h\' for 3600 seconds' do
191
- expect(@hd.humanize(360_0, 'type' => 'short')).to eql('1 h')
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 \'1 y\' for 31536000 seconds' do
195
- expect(@hd.humanize(315_360_00, 'type' => 'short')).to eql('1 y')
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
- before(:each) do
205
- @hd = HumanDurationStatic
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 negative for seconds < 0 (Negative value test)' do
209
- expect(@hd.humanize(-1, 'type' => 'full')).to eql('negative')
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 now for 0 seconds (Zero second test)' do
213
- expect(@hd.humanize(0, 'type' => 'full')).to eql('now')
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 \'0 years, 0 days, 0 hours, 0 minutes and 1 second\' for 1 second (Singular test)' do
217
- expect(@hd.humanize(1, 'type' => 'full')).to eql('0 years, 0 days, 0 hours, 0 minutes and 1 second')
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 \'0 years, 0 days, 0 hours, 0 minutes and 10 seconds\' for 10 second (Plural test)' do
221
- expect(@hd.humanize(10, 'type' => 'full')).to eql('0 years, 0 days, 0 hours, 0 minutes and 10 seconds')
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 \'0 years, 0 days, 0 hours, 1 minute and 0 seconds\' for 60 seconds' do
225
- expect(@hd.humanize(60, 'type' => 'full')).to eql('0 years, 0 days, 0 hours, 1 minute and 0 seconds')
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 \'0 years, 0 days, 1 hour, 0 minutes and 0 seconds\' for 3600 seconds' do
229
- expect(@hd.humanize(360_0, 'type' => 'full')).to eql('0 years, 0 days, 1 hour, 0 minutes and 0 seconds')
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 \'1 year, 0 days, 0 hours, 0 minutes and 0 seconds\' for 31536000 seconds' do
233
- expect(@hd.humanize(315_360_00, 'type' => 'full')).to eql('1 year, 0 days, 0 hours, 0 minutes and 0 seconds')
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 'Direct method access' do
243
- context 'Using default compact mode' do
244
- it 'says negative for seconds < 0 (Negative value test)' do
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
- context 'Using short mode' do
278
- it 'says negative for seconds < 0 (Negative value test)' do
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: 1.0.1
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-17 00:00:00.000000000 Z
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