pretty_seconds 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '0998d7a9dda894f5f8eb87a9f9d992527434de91d3d4ed47652bbc0fa509cf37'
4
+ data.tar.gz: 3a9cfb53f091c3bd87b0b79b831883715ac3debaf7c7b87edc7cc7962058cc96
5
+ SHA512:
6
+ metadata.gz: cb5beb70e03dd9a99c33c4715b961386e32b85bce8d05ecaa8f71f02a9a6cdb76bc72e0a619a3c9921b51a5aceba1932be6c58d3203f4abe38cc72c43996c899
7
+ data.tar.gz: cb6ce78a0e5091b903d08f3fb075d7f79c1f9a1fe1a545c2c2f14aa9f439a8abd6e96bb2e16c71c5d033cfe39f9cb4d594ded895cda95e2c81d67866a7edc329
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in pretty_seconds.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
@@ -0,0 +1,21 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ pretty_seconds (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.13.0)
10
+ rake (12.3.2)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ minitest (~> 5.0)
17
+ pretty_seconds!
18
+ rake (~> 12.0)
19
+
20
+ BUNDLED WITH
21
+ 2.1.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Yukito Ito
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.
@@ -0,0 +1,50 @@
1
+ # PrettySeconds
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'pretty_seconds'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle install
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install pretty_seconds
18
+
19
+ ## Usage
20
+
21
+ Convert seconds to formatted style
22
+
23
+ ```ruby
24
+ PrettySeconds.convert(110)
25
+ => "1:50"
26
+
27
+ PrettySeconds.convert(9)
28
+ => "0:09"
29
+
30
+ PrettySeconds.convert(602)
31
+ => "10:02"
32
+
33
+ PrettySeconds.convert(3675)
34
+ => "1:1:15"
35
+ ```
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ 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).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ykpythemind/pretty_seconds.
46
+
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pretty_seconds"
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__)
@@ -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,11 @@
1
+ require "pretty_seconds/version"
2
+
3
+ module PrettySeconds
4
+ def convert(number, opts = {})
5
+ Converter.new(opts).convert(number)
6
+ end
7
+
8
+ extend self
9
+ end
10
+
11
+ require "pretty_seconds/converter"
@@ -0,0 +1,86 @@
1
+ module PrettySeconds
2
+ class Converter
3
+ def initialize(opts = {})
4
+ @truncate_config = (opts.fetch(:truncate) { :round } || :disabled).to_sym
5
+ @padding_config = opts.fetch(:padding) { true } # WIP
6
+ @keep_zero_config = (opts.fetch(:keep_zero) { :minute } || :disabled).to_sym
7
+ @nil_is_zero_config = true # WIP
8
+
9
+ validate_config!
10
+ end
11
+
12
+ def convert(number)
13
+ targets = []
14
+
15
+ return if number.nil? && !nil_is_zero_config
16
+
17
+ if number
18
+ hour = (number / HOUR_SCALE).round
19
+ rest = (number % HOUR_SCALE)
20
+ min = (rest / MINUTE_SCALE).round
21
+ second = rest % MINUTE_SCALE
22
+ else
23
+ hour = 0
24
+ min = 0
25
+ second = 0
26
+ end
27
+
28
+ if hour != 0 || (hour == 0 && keep_zero?(:hour))
29
+ targets << hour
30
+ end
31
+
32
+ if min != 0 || (min == 0 && second == 0 && keep_zero?(:hour, :minute))
33
+ targets << min
34
+ elsif min == 0 && second != 0 && keep_zero?(:hour, :minute)
35
+ targets << min
36
+ else
37
+ targets << 0 if keep_zero?(:hour, :minute)
38
+ end
39
+
40
+ if truncate_config == :round
41
+ second = second.round
42
+ elsif truncate_config == :floor
43
+ second = second.floor
44
+ else
45
+ second = second.round(2) # disable
46
+ end
47
+
48
+ targets << pad(second)
49
+
50
+ targets.join(':')
51
+ end
52
+
53
+ attr_reader :truncate_config,
54
+ :padding_config,
55
+ :keep_zero_config,
56
+ :nil_is_zero_config
57
+
58
+ MINUTE_SCALE = 60
59
+ HOUR_SCALE = 60 * 60
60
+
61
+ private
62
+
63
+ def keep_zero?(*styles)
64
+ styles.any? { |style| style == keep_zero_config }
65
+ end
66
+
67
+ def pad(num)
68
+ return num if num >= 10
69
+
70
+ "0#{num}"
71
+ end
72
+
73
+ KEEP_ZERO_CONFIGS = [:hour, :minute, :disabled]
74
+ TRUNCATE_CONFIGS = [:round, :floor, :disabled]
75
+
76
+ def validate_config!
77
+ unless KEEP_ZERO_CONFIGS.include?(keep_zero_config)
78
+ raise ArgumentError, "Keep zero config invalid: available configs are #{KEEP_ZERO_CONFIGS}"
79
+ end
80
+
81
+ unless TRUNCATE_CONFIGS.include?(truncate_config)
82
+ raise ArgumentError, "Truncate config invalid: available configs are #{TRUNCATE_CONFIGS}"
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ module PrettySeconds
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'lib/pretty_seconds/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "pretty_seconds"
5
+ spec.version = PrettySeconds::VERSION
6
+ spec.authors = ["Yukito Ito"]
7
+ spec.email = ["yukibukiyou@gmail.com"]
8
+
9
+ spec.summary = %q{Convert seconds to formatted style.}
10
+ spec.description = %q{Convert seconds to formatted style.}
11
+ spec.homepage = "https://github.com/ykpythemind/pretty_seconds"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/ykpythemind/pretty_seconds"
17
+ spec.metadata["changelog_uri"] = "https://github.com/ykpythemind/pretty_seconds/CHANGELOG.md"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pretty_seconds
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yukito Ito
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-07-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Convert seconds to formatted style.
14
+ email:
15
+ - yukibukiyou@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - Gemfile
22
+ - Gemfile.lock
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - bin/console
27
+ - bin/setup
28
+ - lib/pretty_seconds.rb
29
+ - lib/pretty_seconds/converter.rb
30
+ - lib/pretty_seconds/version.rb
31
+ - pretty_seconds.gemspec
32
+ homepage: https://github.com/ykpythemind/pretty_seconds
33
+ licenses:
34
+ - MIT
35
+ metadata:
36
+ homepage_uri: https://github.com/ykpythemind/pretty_seconds
37
+ source_code_uri: https://github.com/ykpythemind/pretty_seconds
38
+ changelog_uri: https://github.com/ykpythemind/pretty_seconds/CHANGELOG.md
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.3.0
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.1.2
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Convert seconds to formatted style.
58
+ test_files: []