activesupport-duration-human_string 0.1.0 → 0.1.1

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: 7a7049bf99ad5f6ffbf07e5ae95ae38efea3b73b9b57dfaf9f655ac66ddb2338
4
- data.tar.gz: ed4fa6793ec9549bd7379120b325e2a5b27c338dfc0d5728168c3d84abc407e6
3
+ metadata.gz: 4f26808d70339292160cc34e94c6c4bf3d4b76a344269e51a714da471706d45f
4
+ data.tar.gz: b00aa1e03d68ece37ba93ab7ae765d3dca9c11bd5da32b8d2055083a4a14981e
5
5
  SHA512:
6
- metadata.gz: b960130471d06f5cf4425b3e53158e23094703cbd4c5efbed04d4cac133628d2662ac3a8ad742f2fd1971e4edddbd54551d465c8d57b01986ce983ce3412623d
7
- data.tar.gz: 8b1b76dbb70d9d31f7637b7f09c03857b15643b8eb8a3f35de1615e7df420dd18622674744ecea6fc77df865c56b7fe39d7f88b9c5bf2f602dc667cbe1d54c62
6
+ metadata.gz: c032fe5ce288994bb132f3896de00d3e6a45169bc57eaf9b5fb441c6049d593b105bf4d47ee3847405c3e40f378b4cf7c5df13321549d870055f28406f9a75d9
7
+ data.tar.gz: 9707cfa9cbeff415f31f67f8fd2955cb660874c6c160df0e3a67b60e6a85ff43cca23d951de356af11a4be12480ca04c7619c1c5c3d96e14c0416d145cab201c
data/Readme.md CHANGED
@@ -1,10 +1,21 @@
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.
1
+ # ActiveSupport::Duration#human_string [![Gem Version](https://badge.fury.io/rb/activesupport-duration-human_string.svg)](https://badge.fury.io/rb/activesupport-duration-human_string)
2
+
3
+ Convert [ActiveSupport::Duration](https://api.rubyonrails.org/classes/ActiveSupport/Duration.html)
4
+ objects to human-friendly strings like `'2h 30m 17s'` or `'3y 6m 4d 12h 30m 5s'`.
5
+
6
+ - Like [`distance_of_time_in_words`](https://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-distance_of_time_in_words)
7
+ helper but _exact_ rather than approximate
8
+ - `distance_of_time_in_words` is somewhat configurable but only gives an approximation (`'over 3
9
+ years'`), with no option to print the exact number of each units.
10
+ - Like [`#inspect`](https://github.com/rails/rails/blob/b9ca94caea2ca6a6cc09abaffaad67b447134079/activesupport/lib/active_support/duration.rb#L372)
11
+ but more concise and configurable.
12
+ - `#inspect` is long (`'3 years, 6 months, 4 days, 12 hours, 30 minutes, and 5 seconds'`) and not
13
+ at all configurable.
14
+ - Like [`#iso8601`](https://api.rubyonrails.org/classes/ActiveSupport/Duration.html#method-i-iso8601)
15
+ but more human readable rather than machine readable.
16
+ - `#iso8601` is concise and machine-readable, but not very human-readable (`'P3Y6M4DT12H30M5S'`)!
17
+ - Unlike [`#to_s`](https://api.rubyonrails.org/classes/ActiveSupport/Duration.html#method-i-to_s),
18
+ which is very concise (`'110839937'`) but not at all human-readable for large durations
8
19
 
9
20
  ## Installation
10
21
 
@@ -17,15 +28,18 @@ gem 'activesupport-duration-human_string'
17
28
  ## Usage
18
29
 
19
30
  ```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)
31
+ duration = 3500.seconds
25
32
  duration.human_str # => '58m 20s'
26
33
  duration.human_str(delimiter: '') # => '58m20s'
27
34
  duration.human_str(separator: ' ') # => '58 m 20 s'
28
35
  duration.human_str(delimiter: ', ', separator: ' ') # => '58 m, 20 s'
36
+
37
+ duration = ActiveSupport::Duration.parse "P3Y6M4DT12H30M5S"
38
+ # => 3 years, 6 months, 4 days, 12 hours, 30 minutes, and 5 seconds
39
+
40
+ duration.human_str # => "3y 6m 4d 12h 30m 5s"
41
+ (duration - 4.days).human_str # => "3y 6m 12h 30m 5s"
42
+ duration.human_str(delimiter: ', ') # => "3y, 6m, 4d, 12h, 30m, 5s"
29
43
  ```
30
44
 
31
45
  Also aliased as `human_string`, `to_human_s`.
@@ -3,25 +3,32 @@ require 'active_support/duration/iso8601_serializer'
3
3
 
4
4
  module ActiveSupport
5
5
  class Duration
6
- # Convert [ActiveSupport::Duration](https://api.rubyonrails.org/classes/ActiveSupport/Duration.html) objects to human-friendly strings like `'2h 30m 17s'`.
6
+ # Convert [ActiveSupport::Duration](https://api.rubyonrails.org/classes/ActiveSupport/Duration.html)
7
+ # objects to human-friendly strings like `'2h 30m 17s'` or `'3y 6m 4d 12h 30m 5s'`.
7
8
  #
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.
9
+ # - Like [`distance_of_time_in_words`](https://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html) helper but _exact_ rather than approximate.
10
+ # - Like [`#inspect`](https://github.com/rails/rails/blob/b9ca94caea2ca6a6cc09abaffaad67b447134079/activesupport/lib/active_support/duration.rb#L372) but more concise and configurable.
11
+ # - Like [`#iso8601`](https://api.rubyonrails.org/classes/ActiveSupport/Duration.html#method-i-iso8601) but more human readable rather than machine readable.
10
12
  #
11
13
  # Note that the unit 'm' is used for both months and minutes.
12
14
  #
13
15
  # ## Examples
14
16
  #
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)
17
+ # ```ruby
18
+ # duration = 3500.seconds
20
19
  # duration.human_str # => '58m 20s'
21
20
  # duration.human_str(delimiter: '') # => '58m20s'
22
21
  # duration.human_str(separator: ' ') # => '58 m 20 s'
23
22
  # duration.human_str(delimiter: ', ', separator: ' ') # => '58 m, 20 s'
24
23
  #
24
+ # duration = ActiveSupport::Duration.parse "P3Y6M4DT12H30M5S"
25
+ # # => 3 years, 6 months, 4 days, 12 hours, 30 minutes, and 5 seconds
26
+ #
27
+ # duration.human_str # => "3y 6m 4d 12h 30m 5s"
28
+ # (duration - 4.days).human_str # => "3y 6m 12h 30m 5s"
29
+ # duration.human_str(delimiter: ', ') # => "3y, 6m, 4d, 12h, 30m, 5s"
30
+ # ```
31
+ #
25
32
  # ## Options
26
33
  #
27
34
  # `:precision`: Precision of seconds (defaults to nil, which is no digits after decimal).
@@ -2,7 +2,7 @@ module ActiveSupport
2
2
  class Duration
3
3
  module HumanString
4
4
  def self.version
5
- "0.1.0"
5
+ "0.1.1"
6
6
  end
7
7
  end
8
8
  end
@@ -0,0 +1 @@
1
+ require 'active_support/duration/human_string'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activesupport-duration-human_string
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Rick
@@ -93,6 +93,7 @@ files:
93
93
  - bin/setup
94
94
  - lib/active_support/duration/human_string.rb
95
95
  - lib/active_support/duration/human_string/version.rb
96
+ - lib/activesupport-duration-human_string.rb
96
97
  homepage: https://github.com/TylerRick/activesupport-duration-human_string
97
98
  licenses:
98
99
  - MIT