humane 0.3.0 → 0.4.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: 433a88d823e187b4481fb3c2632ba816b487b3b8047f272b5c4b17a930b6227b
4
- data.tar.gz: 5c90d3a7ee778274382e8ca25d604d39e672333ddfb8985941b97e7ce345f335
3
+ metadata.gz: 572a6a4a29165d48eec68db31af6cb222a88de6f989099d353811225898c4673
4
+ data.tar.gz: cfc60adb65c39694c156f7b173d053431d469ec26d1bf98a584c83b05bfc5887
5
5
  SHA512:
6
- metadata.gz: 42ceb33a1bd570aa85b8cc5a9417204fcd9d6041e79e6e44c3b7a65469f9139c2897019afef22aab9269bd3956599c2e1ed430021a64f399a2fc6fac0e996c30
7
- data.tar.gz: fc9eaa4b043bf6547444631fde9fa2bba69c493ecc07d5e43375ac65f6a20c9f607c73776c7f3e535080a4d2c682747cb8bff8e017e5ddfe67a0f79e3869a4ae
6
+ metadata.gz: baa6946270a2a9bcaa4636993324df9cae89da3ff2a753552bc21bcdc5569a9fe10075dc48e4e2e910a2f50d811ff324bf7e7ce59168c03025cc2f2157626d5e
7
+ data.tar.gz: 4dc6ee4899fe1ae32d33c36307a5a01bef49cf34fb368668b28bd584cae6bce38d9a00be13d9aef532020015d40f00375c1c9ad8299380882bdcc29935469f78
data/README.md CHANGED
@@ -45,6 +45,24 @@ or in a `Gemfile`:
45
45
  gem "humane"
46
46
  ```
47
47
 
48
+ ## Beyond Foundation's defaults
49
+
50
+ Two options on `Humane::TimeFormatter`, both off by default so it matches
51
+ `RelativeDateTimeFormatter` exactly out of the box:
52
+
53
+ - `include_seconds` (default `false`): below a minute, collapses to "less than a
54
+ minute ago"/"in less than a minute" instead of an exact second count. Named after
55
+ ActionView's `include_seconds`, which defaults the same way.
56
+ - `approximate` (default `false`): prefixes "about"/"in about" on buckets of an hour
57
+ or larger, the way ActionView's `distance_of_time_in_words` does past that same
58
+ boundary -- for a render that can't refresh itself and shouldn't overstate its own
59
+ precision.
60
+
61
+ ```ruby
62
+ Humane::TimeFormatter.new(approximate: true).string(at: t - 15 * 3600, relative_to: t)
63
+ # => "about 15 hours ago"
64
+ ```
65
+
48
66
  ## Scope
49
67
 
50
68
  Finder's `.file` byte-count style, and a numeric (non-calendar-aware)
@@ -4,8 +4,10 @@ module Humane
4
4
  # Formats one time relative to another the way Finder-adjacent tools do.
5
5
  class TimeFormatter
6
6
  # include_seconds shows exact seconds under a minute instead of collapsing to "less than a minute ago/in less than a minute". Defaults to false, matching ActionView's include_seconds.
7
- def initialize(include_seconds: false)
7
+ # approximate prefixes "about"/"in about" on buckets of an hour or more, matching ActionView's distance_of_time_in_words past that same boundary. Defaults to false.
8
+ def initialize(include_seconds: false, approximate: false)
8
9
  @include_seconds = include_seconds
10
+ @approximate = approximate
9
11
  end
10
12
 
11
13
  # Returns the time at `at` relative to `relative_to` as a human-readable string.
@@ -29,6 +31,8 @@ module Humane
29
31
  pluralize((seconds / 86_400.0).round, "day")
30
32
  end
31
33
 
34
+ text = "about #{text}" if @approximate && seconds >= 3600
35
+
32
36
  future ? "in #{text}" : "#{text} ago"
33
37
  end
34
38
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Humane
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: humane
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Woodell