human_duration 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 34fcb063f0ad7d76dc1d62a289263fdbd25e236e4f589a25490947735333264d
4
+ data.tar.gz: ee2ca9c18f07f7ccc27dd495471c5eac486781103f785652144db5dfe77e1cd4
5
+ SHA512:
6
+ metadata.gz: 0f3f5225dc736788e4489fddceed77e404f0545ed560a28dec47a6d6c6db95e966a7b5fb2da999647275d145066c964903d197720a63a6be62bebe8d0da9f179
7
+ data.tar.gz: a046d4634d2701080a08a61122b9cc3bcb32ca128f74fc38c26f95ce09f88058544fee18cb05e4b31b9ea5a1c091589cb149bdc11c62dfdb585baf4bdf449ecc
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,30 @@
1
+ Layout/IndentationWidth:
2
+ Width: 4
3
+
4
+ Metrics/AbcSize:
5
+ Max: 50
6
+
7
+ Metrics/LineLength:
8
+ Max: 180
9
+
10
+ Metrics/BlockLength:
11
+ Max: 40
12
+
13
+ Metrics/CyclomaticComplexity:
14
+ Max: 20
15
+
16
+ Metrics/MethodLength:
17
+ Max: 60
18
+
19
+ Metrics/PerceivedComplexity:
20
+ Max: 20
21
+
22
+ Style/GlobalVars:
23
+ Enabled: false
24
+
25
+ Style/RaiseArgs:
26
+ EnforcedStyle: compact
27
+
28
+ Style/WordArray:
29
+ EnforcedStyle: brackets
30
+
data/.travis.yml ADDED
@@ -0,0 +1,34 @@
1
+ sudo: required
2
+ matrix:
3
+ include:
4
+ - language: ruby
5
+ rvm: 2.2
6
+ before_install:
7
+ - gem install bundler -v 1.16.1
8
+ - language: ruby
9
+ rvm: 2.4.4
10
+ before_install:
11
+ - gem install bundler -v 1.16.1
12
+ - language: ruby
13
+ env: SKIP_INTERPRETER=true
14
+
15
+ rvm: 2.4.4
16
+ before_install:
17
+ - git clone https://github.com/TravisToolbox/rubocop-travis.git
18
+ install:
19
+ - ./rubocop-travis/install.sh
20
+ script:
21
+ - ./rubocop-travis/scan.sh
22
+ - language: ruby
23
+ rvm: 2.2
24
+ before_install:
25
+ - mkdir travis
26
+ - git clone https://github.com/TravisToolbox/awesomebot-travis.git travis/awesomebot
27
+ install:
28
+ - ./travis/awesomebot/install.sh
29
+ script:
30
+ - ./travis/awesomebot/scan.sh
31
+ notifications:
32
+ email: false
33
+ slack:
34
+ secure: HU/Sb2/+hsnpv4/ZTz/DQDCNPiiZykQ8IeHltdNrHiX3/WNHm1R93tvpx19BImor660q8IU79zTYfVBsBbylo1aHIG6ij1NUtOfIQVn5wl3Ywx5j4YYqrzCMK1oTmm+h/pXepkaCQSIs+W6VIc1TnxhwwXgWzq3DhKy/j4D6jZvRTUBNJ9rFGfubQ5iDKLLQi1GOCrJsMmrOXap9wSrg02ijtoNGjHdKFHpSHZqoPg1BdVA8er7Ou8+v7DSFWKWZWPM3MCKL7GPBUtWIK1wm5s+bAiwAYJHJM3d/2gK3WAyQRi456l9+PBn7SoUH72HAubTLuUS0NgbBfefh7gewuPd+5fBgKS3yAi+e9+HYqPYHcztiP7LRyNemweKX7merjRTQSyjrAULa+Bddn92I2gf+CB189FUtaCYgT13Pxhp8QHLUAZdy5JIszznHTB/eC4QADwZUAa0PA2Cq6dLug4m5KZZarsJ+63caU+oQQ7UnWfqtdR8UFagWo7ceRid1SeTIhOjZ9N8I2gBQ8Kegi71s2P0zFNQy6H20Z+ScT8GWPlM4KZcmWUgNKSCymINhYQWeo+kYQXXHc18aMzcp0iFtWBIA4PShqdDtx2m88G9uXmXjBRrrla4K1e81VwbXYPmszcyLSa6Qbx2spu7mqcf0U13D10zJtVWct8+PwE0=
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## 1.0.0 (January 17, 2019)
2
+
3
+ * Initial Release ([@TGWolf][])
4
+
5
+ [@TGWolf]: https://github.com/TGWolf
data/CODEOWNERS ADDED
@@ -0,0 +1,7 @@
1
+ # The codeowners file:
2
+ #
3
+ # For more information please read: https://help.github.com/articles/about-codeowners/
4
+
5
+ # These owners will be the default owners for everything in
6
+ # the repo. Unless a later match takes precedence,
7
+ * @wolfsoftware/reviewers
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at tim.gurney@timgurney.net. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in human_duration.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ human_duration (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.8.0)
12
+ rspec-core (~> 3.8.0)
13
+ rspec-expectations (~> 3.8.0)
14
+ rspec-mocks (~> 3.8.0)
15
+ rspec-core (3.8.0)
16
+ rspec-support (~> 3.8.0)
17
+ rspec-expectations (3.8.2)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.8.0)
20
+ rspec-mocks (3.8.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-support (3.8.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.16)
30
+ human_duration!
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Tim Gurney
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ [![Build Status](https://img.shields.io/travis/WolfSoftware/human_duration/master.svg)](https://travis-ci.org/WolfSoftware/human_duration)
2
+ [![Software License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE.md)
3
+ [![Release](https://img.shields.io/github/release/wolfsoftware/human_duration.svg)](https://github.com/wolfsoftware/human_duration/releases/latest)
4
+ [![Github commits (since latest release)](https://img.shields.io/github/commits-since/wolfsoftware/human_duration/latest.svg)](https://github.com/wolfsoftware/human_duration/commits)
5
+ [![GitHub repo size in bytes](https://img.shields.io/github/repo-size/wolfsoftware/human_duration.svg)](https://github.com/wolfsoftware/human_duration)
6
+ [![GitHub contributors](https://img.shields.io/github/contributors/wolfsoftware/human_duration.svg)](https://github.com/wolfsoftware/human_duration)
7
+
8
+ # HumanDuration
9
+
10
+ Human Durartion is a simple ruby gem to convert a number of seconds into a more human readable version.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'human_duration'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install human_duration
27
+
28
+ ## Usage
29
+
30
+ ```ruby
31
+ #!/usr/bin/env ruby
32
+
33
+ require 'human_duration'
34
+
35
+ test_value = [-10, 0, 9, 98, 987, 987_6, 987_65, 987_654, 987_654_3, 987_654_32, 987_654_321]
36
+
37
+ puts 'Class method access'
38
+
39
+ hd = HumanDuration.new
40
+ test_value.each do |x|
41
+ puts hd.humanize(x)
42
+ end
43
+
44
+ puts 'Static method access'
45
+
46
+ puts 'Config: default'
47
+ test_value.each do |x|
48
+ puts HumanDurationStatic.humanize(x)
49
+ end
50
+ ```
51
+
52
+ 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)
53
+
54
+ ## Development
55
+
56
+ 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.
57
+
58
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
59
+
60
+ ## Testing
61
+
62
+ For local testing make sure that you run `bundle exec rspec spec` and then `rake install` to install the gem locally.
63
+
64
+ ## Contributing
65
+
66
+ Bug reports and pull requests are welcome on GitHub at https://github.com/WolfSoftware/human_duration. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
67
+
68
+ ## License
69
+
70
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
71
+
72
+ ## Code of Conduct
73
+
74
+ Everyone interacting in the HumanDuration project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/WolfSoftware/human_duration/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "human_duration"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'human_duration'
4
+
5
+ test_value = [-10, 0, 9, 98, 987, 987_6, 987_65, 987_654, 987_654_3, 987_654_32, 987_654_321]
6
+
7
+ puts 'Class method access'
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
+
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
+ test_value.each do |x|
30
+ printf "\t\t%s\n", HumanDurationStatic.humanize(x)
31
+ end
32
+
33
+ printf "\tConfig: type = short\n"
34
+ test_value.each do |x|
35
+ printf "\t\t%s\n", HumanDurationStatic.humanize(x)
36
+ end
37
+
38
+ puts "\tConfig: type = full"
39
+ test_value.each do |x|
40
+ printf "\t\t%s\n", HumanDurationStatic.humanize(x)
41
+ end
@@ -0,0 +1,28 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "human_duration/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "human_duration"
7
+ spec.version = HumanDuration::VERSION
8
+ spec.authors = ["Tim Gurney aka Wolf"]
9
+ spec.email = ["wolf@tgwolf.com"]
10
+
11
+ spec.summary = %q{Display elapsed time in a more human readable way.}
12
+ spec.description = %q{Display elapsed time in a more human readable way.}
13
+ spec.homepage = "https://github.com/WolfSoftware/human_duration"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ # spec.files = Dir['lib/**/*.rb']
18
+ # spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ # f.match(%r{^(test|spec|features)/})
20
+ # end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.16"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
@@ -0,0 +1,3 @@
1
+ class HumanDuration
2
+ VERSION = '1.0.0'.freeze
3
+ end
@@ -0,0 +1,103 @@
1
+ require 'human_duration/version'
2
+
3
+ # Doc here
4
+ class HumanDurationStatic
5
+ def self.humanize(seconds, config = { 'type' => 'compact' })
6
+ hd = HumanDuration.new(config)
7
+ hd.humanize(seconds)
8
+ end
9
+ end
10
+
11
+ # Doc here
12
+ class HumanDuration
13
+ def initialize(config = { 'type' => 'compact' })
14
+ @config = config
15
+
16
+ @split_types = [[60, 'second'], [60, 'minute'], [24, 'hour'], [365, 'day'], [1000, 'year']]
17
+ @short_name_map = { 'second' => 's', 'minute' => 'm', 'hour' => 'h', 'day' => 'd', 'year' => 'y' }
18
+ end
19
+
20
+ def humanize(seconds)
21
+ reset
22
+
23
+ return 'negative' if seconds < 0
24
+ return 'now' if seconds.zero?
25
+
26
+ split_seconds(seconds)
27
+ count_items
28
+ generate_output
29
+
30
+ @output_buffer
31
+ end
32
+
33
+ private
34
+
35
+ def reset
36
+ @time_values = {}
37
+ @item_count = 0
38
+ @output_buffer = ''
39
+ end
40
+
41
+ def pluralize(number)
42
+ return 's' unless number == 1
43
+
44
+ ''
45
+ end
46
+
47
+ def comma_or_not(count)
48
+ return ', ' if count > 1
49
+ return ' & ' if count == 1 && @config['type'] == 'short'
50
+ return ' and ' if count == 1
51
+
52
+ ''
53
+ end
54
+
55
+ def add_time(value, name, plural = true)
56
+ return if value < 1 && @config['type'] != 'full'
57
+
58
+ @output_buffer << if plural
59
+ format('%<value>s %<name>s%<plural>s', value: value, name: name, plural: pluralize(value))
60
+ else
61
+ format('%<value>s %<name>s', value: value, name: name)
62
+ end
63
+ @item_count -= 1
64
+ @output_buffer << comma_or_not(@item_count)
65
+ end
66
+
67
+ def split_seconds(seconds)
68
+ @split_types.map do |count, name|
69
+ if seconds > 0
70
+ seconds, n = seconds.divmod(count)
71
+ @time_values[name] = n
72
+ else
73
+ @time_values[name] = 0
74
+ end
75
+ end
76
+ end
77
+
78
+ def count_items
79
+ @time_values.each do |_name, value|
80
+ @item_count += 1 if value > 0 || @config['type'] == 'full'
81
+ end
82
+ end
83
+
84
+ def add_time_long
85
+ @time_values.reverse_each do |name, value|
86
+ add_time(value, name)
87
+ end
88
+ end
89
+
90
+ def add_time_short
91
+ @time_values.reverse_each do |name, value|
92
+ add_time(value, @short_name_map[name], false)
93
+ end
94
+ end
95
+
96
+ def generate_output
97
+ if @config['type'] == 'short'
98
+ add_time_short
99
+ else
100
+ add_time_long
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,243 @@
1
+ # rubocop:disable Metrics/BlockLength
2
+
3
+ require 'human_duration'
4
+
5
+ RSpec.describe HumanDuration do
6
+ it 'has a version number' do
7
+ expect(HumanDuration::VERSION).not_to be nil
8
+ end
9
+
10
+ context 'Class method access' do
11
+ context 'Using default compact mode' do
12
+ before(:each) do
13
+ @hd = HumanDuration.new
14
+ end
15
+
16
+ it 'says negative for seconds < 0 (Negative value test)' do
17
+ expect(@hd.humanize(-1)).to eql('negative')
18
+ end
19
+
20
+ it 'says now for 0 seconds (Zero second test)' do
21
+ expect(@hd.humanize(0)).to eql('now')
22
+ end
23
+
24
+ it 'says \'1 second\' for 1 second (Singular test)' do
25
+ expect(@hd.humanize(1)).to eql('1 second')
26
+ end
27
+
28
+ it 'says \'10 seconds\' for 10 second (Plural test)' do
29
+ expect(@hd.humanize(10)).to eql('10 seconds')
30
+ end
31
+
32
+ it 'says \'1 minute\' for 60 seconds' do
33
+ expect(@hd.humanize(60)).to eql('1 minute')
34
+ end
35
+
36
+ it 'says \'1 hour\' for 3600 seconds' do
37
+ expect(@hd.humanize(360_0)).to eql('1 hour')
38
+ end
39
+
40
+ it 'says \'1 year\' for 31536000 seconds' do
41
+ expect(@hd.humanize(315_360_00)).to eql('1 year')
42
+ end
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')
46
+ end
47
+ end
48
+
49
+ context 'Using short mode' do
50
+ before(:each) do
51
+ @hd = HumanDuration.new('type' => 'short')
52
+ end
53
+
54
+ it 'says negative for seconds < 0 (Negative value test)' do
55
+ expect(@hd.humanize(-1)).to eql('negative')
56
+ end
57
+
58
+ it 'says now for 0 seconds (Zero second test)' do
59
+ expect(@hd.humanize(0)).to eql('now')
60
+ end
61
+
62
+ it 'says \'1 s\' for 1 second (Singular test)' do
63
+ expect(@hd.humanize(1)).to eql('1 s')
64
+ end
65
+
66
+ it 'says \'10 s\' for 10 second (Plural test)' do
67
+ expect(@hd.humanize(10)).to eql('10 s')
68
+ end
69
+
70
+ it 'says \'1 m\' for 60 seconds' do
71
+ expect(@hd.humanize(60)).to eql('1 m')
72
+ end
73
+
74
+ it 'says \'1 h\' for 3600 seconds' do
75
+ expect(@hd.humanize(360_0)).to eql('1 h')
76
+ end
77
+
78
+ it 'says \'1 y\' for 31536000 seconds' do
79
+ expect(@hd.humanize(315_360_00)).to eql('1 y')
80
+ end
81
+
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')
84
+ end
85
+ end
86
+
87
+ context 'Using full mode' do
88
+ before(:each) do
89
+ @hd = HumanDuration.new('type' => 'full')
90
+ end
91
+
92
+ it 'says negative for seconds < 0 (Negative value test)' do
93
+ expect(@hd.humanize(-1)).to eql('negative')
94
+ end
95
+
96
+ it 'says now for 0 seconds (Zero second test)' do
97
+ expect(@hd.humanize(0)).to eql('now')
98
+ end
99
+
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
+ end
103
+
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
+ end
107
+
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
+ end
111
+
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
+ end
115
+
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
+ end
119
+
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
+ end
123
+ end
124
+ end
125
+
126
+ context 'Static method access' do
127
+ 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')
134
+ end
135
+
136
+ it 'says now for 0 seconds (Zero second test)' do
137
+ expect(@hd.humanize(0)).to eql('now')
138
+ end
139
+
140
+ it 'says \'1 second\' for 1 second (Singular test)' do
141
+ expect(@hd.humanize(1)).to eql('1 second')
142
+ end
143
+
144
+ it 'says \'10 seconds\' for 10 second (Plural test)' do
145
+ expect(@hd.humanize(10)).to eql('10 seconds')
146
+ end
147
+
148
+ it 'says \'1 minute\' for 60 seconds' do
149
+ expect(@hd.humanize(60)).to eql('1 minute')
150
+ end
151
+
152
+ it 'says \'1 hour\' for 3600 seconds' do
153
+ expect(@hd.humanize(360_0)).to eql('1 hour')
154
+ end
155
+
156
+ it 'says \'1 year\' for 31536000 seconds' do
157
+ expect(@hd.humanize(315_360_00)).to eql('1 year')
158
+ end
159
+
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')
162
+ end
163
+ end
164
+
165
+ context 'Using short mode' do
166
+ before(:each) do
167
+ @hd = HumanDurationStatic
168
+ end
169
+
170
+ it 'says negative for seconds < 0 (Negative value test)' do
171
+ expect(@hd.humanize(-1, 'type' => 'short')).to eql('negative')
172
+ end
173
+
174
+ it 'says now for 0 seconds (Zero second test)' do
175
+ expect(@hd.humanize(0, 'type' => 'short')).to eql('now')
176
+ end
177
+
178
+ it 'says \'1 s\' for 1 second (Singular test)' do
179
+ expect(@hd.humanize(1, 'type' => 'short')).to eql('1 s')
180
+ end
181
+
182
+ it 'says \'10 s\' for 10 second (Plural test)' do
183
+ expect(@hd.humanize(10, 'type' => 'short')).to eql('10 s')
184
+ end
185
+
186
+ it 'says \'1 m\' for 60 seconds' do
187
+ expect(@hd.humanize(60, 'type' => 'short')).to eql('1 m')
188
+ end
189
+
190
+ it 'says \'1 h\' for 3600 seconds' do
191
+ expect(@hd.humanize(360_0, 'type' => 'short')).to eql('1 h')
192
+ end
193
+
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')
200
+ end
201
+ end
202
+
203
+ context 'Using full mode' do
204
+ before(:each) do
205
+ @hd = HumanDurationStatic
206
+ end
207
+
208
+ it 'says negative for seconds < 0 (Negative value test)' do
209
+ expect(@hd.humanize(-1, 'type' => 'full')).to eql('negative')
210
+ end
211
+
212
+ it 'says now for 0 seconds (Zero second test)' do
213
+ expect(@hd.humanize(0, 'type' => 'full')).to eql('now')
214
+ end
215
+
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')
218
+ end
219
+
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')
222
+ end
223
+
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')
226
+ end
227
+
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')
230
+ end
231
+
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')
238
+ end
239
+ end
240
+ end
241
+ end
242
+
243
+ # rubocop:enable Metrics/BlockLength
@@ -0,0 +1,14 @@
1
+ require 'bundler/setup'
2
+ require 'human_duration'
3
+
4
+ RSpec.configure do |config|
5
+ # Enable flags like --only-failures and --next-failure
6
+ config.example_status_persistence_file_path = '.rspec_status'
7
+
8
+ # Disable RSpec exposing methods globally on `Module` and `main`
9
+ config.disable_monkey_patching!
10
+
11
+ config.expect_with :rspec do |c|
12
+ c.syntax = :expect
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: human_duration
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Tim Gurney aka Wolf
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Display elapsed time in a more human readable way.
56
+ email:
57
+ - wolf@tgwolf.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".rubocop.yml"
65
+ - ".travis.yml"
66
+ - CHANGELOG.md
67
+ - CODEOWNERS
68
+ - CODE_OF_CONDUCT.md
69
+ - Gemfile
70
+ - Gemfile.lock
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - bin/console
75
+ - bin/setup
76
+ - example/example.rb
77
+ - human_duration.gemspec
78
+ - lib/human_duration.rb
79
+ - lib/human_duration/version.rb
80
+ - spec/human_duration_spec.rb
81
+ - spec/spec_helper.rb
82
+ homepage: https://github.com/WolfSoftware/human_duration
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubygems_version: 3.0.2
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Display elapsed time in a more human readable way.
105
+ test_files: []