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 +4 -4
- data/README.md +18 -0
- data/lib/humane/time_formatter.rb +5 -1
- data/lib/humane/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 572a6a4a29165d48eec68db31af6cb222a88de6f989099d353811225898c4673
|
|
4
|
+
data.tar.gz: cfc60adb65c39694c156f7b173d053431d469ec26d1bf98a584c83b05bfc5887
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
-
|
|
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
|
|
data/lib/humane/version.rb
CHANGED