activesupport-duration-human_string 0.1.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 +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Changelog.md +0 -0
- data/Gemfile +4 -0
- data/License +21 -0
- data/Rakefile +6 -0
- data/Readme.md +46 -0
- data/activesupport-duration-human_string.gemspec +38 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/active_support/duration/human_string.rb +90 -0
- data/lib/active_support/duration/human_string/version.rb +9 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7a7049bf99ad5f6ffbf07e5ae95ae38efea3b73b9b57dfaf9f655ac66ddb2338
|
4
|
+
data.tar.gz: ed4fa6793ec9549bd7379120b325e2a5b27c338dfc0d5728168c3d84abc407e6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b960130471d06f5cf4425b3e53158e23094703cbd4c5efbed04d4cac133628d2662ac3a8ad742f2fd1971e4edddbd54551d465c8d57b01986ce983ce3412623d
|
7
|
+
data.tar.gz: 8b1b76dbb70d9d31f7637b7f09c03857b15643b8eb8a3f35de1615e7df420dd18622674744ecea6fc77df865c56b7fe39d7f88b9c5bf2f602dc667cbe1d54c62
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Changelog.md
ADDED
File without changes
|
data/Gemfile
ADDED
data/License
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2019 Tyler Rick
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/Rakefile
ADDED
data/Readme.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# `ActiveSupport::Duration#human_string`
|
2
|
+
|
3
|
+
Convert [ActiveSupport::Duration](https://api.rubyonrails.org/classes/ActiveSupport/Duration.html) objects to human-friendly strings like `'2h 30m 17s'`.
|
4
|
+
|
5
|
+
- Like [`distance_of_time_in_words`](https://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html) helper but _exact_ rather than approximate.
|
6
|
+
- Like [`#inspect`](https://github.com/rails/rails/blob/b9ca94caea2ca6a6cc09abaffaad67b447134079/activesupport/lib/active_support/duration.rb#L372) but more concise and configurable.
|
7
|
+
- Like [`#iso8601`](https://api.rubyonrails.org/classes/ActiveSupport/Duration.html#method-i-iso8601) but more human readable rather than machine readable.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'activesupport-duration-human_string'
|
15
|
+
```
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
duration = ActiveSupport::Duration.build(65)
|
21
|
+
duration.human_str # => '1m 5s'
|
22
|
+
duration.human_str(use_2_digit_numbers: true) # => '1m 05s'
|
23
|
+
|
24
|
+
duration = ActiveSupport::Duration.build(3500)
|
25
|
+
duration.human_str # => '58m 20s'
|
26
|
+
duration.human_str(delimiter: '') # => '58m20s'
|
27
|
+
duration.human_str(separator: ' ') # => '58 m 20 s'
|
28
|
+
duration.human_str(delimiter: ', ', separator: ' ') # => '58 m, 20 s'
|
29
|
+
```
|
30
|
+
|
31
|
+
Also aliased as `human_string`, `to_human_s`.
|
32
|
+
|
33
|
+
## Development
|
34
|
+
|
35
|
+
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.
|
36
|
+
|
37
|
+
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).
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/TylerRick/activesupport-duration-human_string.
|
42
|
+
|
43
|
+
## Not really related
|
44
|
+
|
45
|
+
- https://github.com/josedonizetti/ruby-duration
|
46
|
+
- https://github.com/janko/as-duration
|
@@ -0,0 +1,38 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "active_support/duration/human_string/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "activesupport-duration-human_string"
|
8
|
+
spec.version = ActiveSupport::Duration::HumanString.version
|
9
|
+
spec.authors = ["Tyler Rick"]
|
10
|
+
spec.email = ["tyler@tylerrick.com"]
|
11
|
+
spec.license = "MIT"
|
12
|
+
|
13
|
+
spec.summary = %q{Convert Duration objects to human-friendly strings like '2h 30m 17s'}
|
14
|
+
spec.description = %q{Convert ActiveSupport::Duration objects to human-friendly strings like '2h 30m 17s'. }
|
15
|
+
%q{Like distance_of_time_in_words helper but exact rather than approximate. }
|
16
|
+
%q{Like #inspect but more concise. Like #iso8601 but more human readable rather than machine readable.}
|
17
|
+
spec.homepage = "https://github.com/TylerRick/activesupport-duration-human_string"
|
18
|
+
|
19
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
21
|
+
spec.metadata["changelog_uri"] = "#{spec.metadata["source_code_uri"]}/blob/master/Changelog.md"
|
22
|
+
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
26
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
spec.required_ruby_version = ">= 2.3.0"
|
33
|
+
spec.add_dependency "activesupport", [">= 4.2", "< 5.3"]
|
34
|
+
|
35
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
36
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
37
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
38
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "active_support/duration/human_string"
|
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,90 @@
|
|
1
|
+
require 'active_support/duration'
|
2
|
+
require 'active_support/duration/iso8601_serializer'
|
3
|
+
|
4
|
+
module ActiveSupport
|
5
|
+
class Duration
|
6
|
+
# Convert [ActiveSupport::Duration](https://api.rubyonrails.org/classes/ActiveSupport/Duration.html) objects to human-friendly strings like `'2h 30m 17s'`.
|
7
|
+
#
|
8
|
+
# Like [`distance_of_time_in_words`](https://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html) helper but _exact_ rather than approximate.
|
9
|
+
# Like `#inspect` but more concise. Like [`#iso8601`](https://api.rubyonrails.org/classes/ActiveSupport/Duration.html#method-i-iso8601) but more human readable rather than machine readable.
|
10
|
+
#
|
11
|
+
# Note that the unit 'm' is used for both months and minutes.
|
12
|
+
#
|
13
|
+
# ## Examples
|
14
|
+
#
|
15
|
+
# duration = ActiveSupport::Duration.build(65)
|
16
|
+
# duration.human_str # => '1m 5s'
|
17
|
+
# duration.human_str(use_2_digit_numbers: true) # => '1m 05s'
|
18
|
+
#
|
19
|
+
# duration = ActiveSupport::Duration.build(3500)
|
20
|
+
# duration.human_str # => '58m 20s'
|
21
|
+
# duration.human_str(delimiter: '') # => '58m20s'
|
22
|
+
# duration.human_str(separator: ' ') # => '58 m 20 s'
|
23
|
+
# duration.human_str(delimiter: ', ', separator: ' ') # => '58 m, 20 s'
|
24
|
+
#
|
25
|
+
# ## Options
|
26
|
+
#
|
27
|
+
# `:precision`: Precision of seconds (defaults to nil, which is no digits after decimal).
|
28
|
+
#
|
29
|
+
# `:separator`: The separator between the digits and units (defaults to '', giving for example '3h' with nothing between them).
|
30
|
+
#
|
31
|
+
# `:delimiter`: The delimiter between different parts like minutes and seconds (defaults to ' ').
|
32
|
+
#
|
33
|
+
# `use_2_digit_numbers`: Set to true if you want to pad 1-digit nubers to 2 digits ('3h 05m 07s'
|
34
|
+
# instead of '3h 5m 7s'). Never pads the first part of the duration, only later parts.
|
35
|
+
#
|
36
|
+
def human_str(precision: nil, separator: '', delimiter: ' ', use_2_digit_numbers: false)
|
37
|
+
HumanStringSerializer.new(
|
38
|
+
self,
|
39
|
+
precision: precision,
|
40
|
+
separator: separator,
|
41
|
+
delimiter: delimiter,
|
42
|
+
use_2_digit_numbers: use_2_digit_numbers,
|
43
|
+
).serialize
|
44
|
+
end
|
45
|
+
alias_method :human_string, :human_str
|
46
|
+
alias_method :to_human_s, :human_str
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
module ActiveSupport
|
51
|
+
class Duration
|
52
|
+
# Based on: active_support/duration/iso8601_serializer.rb
|
53
|
+
# Inherits: #normalize
|
54
|
+
class HumanStringSerializer < ISO8601Serializer
|
55
|
+
def initialize(duration, precision: nil, separator: '', delimiter: ' ', use_2_digit_numbers: false)
|
56
|
+
@duration = duration
|
57
|
+
@precision = precision
|
58
|
+
@separator = separator
|
59
|
+
@delimiter = delimiter
|
60
|
+
@use_2_digit_numbers = use_2_digit_numbers
|
61
|
+
end
|
62
|
+
|
63
|
+
# Builds and returns output_parts string.
|
64
|
+
def serialize
|
65
|
+
parts, sign = normalize
|
66
|
+
|
67
|
+
output_parts = []
|
68
|
+
output_parts << [parts[:years], 'y'] if parts.key?(:years)
|
69
|
+
output_parts << [parts[:months], 'm'] if parts.key?(:months)
|
70
|
+
output_parts << [parts[:weeks], 'w'] if parts.key?(:weeks)
|
71
|
+
output_parts << [parts[:days], 'd'] if parts.key?(:days)
|
72
|
+
output_parts << [parts[:hours], 'h'] if parts.key?(:hours)
|
73
|
+
output_parts << [parts[:minutes], 'm'] if parts.key?(:minutes)
|
74
|
+
if parts.key?(:seconds)
|
75
|
+
output_parts << [sprintf(@precision ? "%0.0#{@precision}f" : '%g', parts[:seconds]), 's']
|
76
|
+
end
|
77
|
+
|
78
|
+
output_parts.map!.with_index { |(n, units), i|
|
79
|
+
if @use_2_digit_numbers && i >= 1
|
80
|
+
n = sprintf('%02d', n)
|
81
|
+
end
|
82
|
+
"#{n}#{@separator}#{units}"
|
83
|
+
}
|
84
|
+
|
85
|
+
output = output_parts.join(@delimiter)
|
86
|
+
"#{sign}#{output}"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activesupport-duration-human_string
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tyler Rick
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-04-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5.3'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '4.2'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '5.3'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: bundler
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '2.0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '2.0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '10.0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '10.0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rspec
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '3.0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '3.0'
|
75
|
+
description: 'Convert ActiveSupport::Duration objects to human-friendly strings like
|
76
|
+
''2h 30m 17s''. '
|
77
|
+
email:
|
78
|
+
- tyler@tylerrick.com
|
79
|
+
executables: []
|
80
|
+
extensions: []
|
81
|
+
extra_rdoc_files: []
|
82
|
+
files:
|
83
|
+
- ".gitignore"
|
84
|
+
- ".rspec"
|
85
|
+
- ".travis.yml"
|
86
|
+
- Changelog.md
|
87
|
+
- Gemfile
|
88
|
+
- License
|
89
|
+
- Rakefile
|
90
|
+
- Readme.md
|
91
|
+
- activesupport-duration-human_string.gemspec
|
92
|
+
- bin/console
|
93
|
+
- bin/setup
|
94
|
+
- lib/active_support/duration/human_string.rb
|
95
|
+
- lib/active_support/duration/human_string/version.rb
|
96
|
+
homepage: https://github.com/TylerRick/activesupport-duration-human_string
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
metadata:
|
100
|
+
homepage_uri: https://github.com/TylerRick/activesupport-duration-human_string
|
101
|
+
source_code_uri: https://github.com/TylerRick/activesupport-duration-human_string
|
102
|
+
changelog_uri: https://github.com/TylerRick/activesupport-duration-human_string/blob/master/Changelog.md
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: 2.3.0
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
requirements: []
|
118
|
+
rubygems_version: 3.0.1
|
119
|
+
signing_key:
|
120
|
+
specification_version: 4
|
121
|
+
summary: Convert Duration objects to human-friendly strings like '2h 30m 17s'
|
122
|
+
test_files: []
|