humane 0.2.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: 69b5f7271b78dcda7542ff765d7b10ee4ed7fd74b37416c9a0654c2d29ee2e26
4
- data.tar.gz: bb6f7d13fb51171c3721f43739253019483510df40b3515d656c1adab0180e73
3
+ metadata.gz: 572a6a4a29165d48eec68db31af6cb222a88de6f989099d353811225898c4673
4
+ data.tar.gz: cfc60adb65c39694c156f7b173d053431d469ec26d1bf98a584c83b05bfc5887
5
5
  SHA512:
6
- metadata.gz: 0b259b53ee06c2868de3f30cb1e0adef2094e49e5c9096d4bfb68c591578792d42fb813e3457aac686f020041c5d4425b334fc1fe6131ce1bd2e43b08fc30528
7
- data.tar.gz: 74f6c96c9bdb1c1f16430cf0698572db5473ef328e56282b7b006365006616475035a5e9218724f5893b9972bf0bf62adf7d5d08badc0dfa3a69f1cf324fc11c
6
+ metadata.gz: baa6946270a2a9bcaa4636993324df9cae89da3ff2a753552bc21bcdc5569a9fe10075dc48e4e2e910a2f50d811ff324bf7e7ce59168c03025cc2f2157626d5e
7
+ data.tar.gz: 4dc6ee4899fe1ae32d33c36307a5a01bef49cf34fb368668b28bd584cae6bce38d9a00be13d9aef532020015d40f00375c1c9ad8299380882bdcc29935469f78
data/README.md CHANGED
@@ -1,14 +1,37 @@
1
1
  # humane-ruby
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/humane.svg)](https://rubygems.org/gems/humane)
4
3
  [![Ruby](https://img.shields.io/badge/Ruby-3.1-red.svg)](https://www.ruby-lang.org/)
5
4
  [![CI](https://github.com/woodie/humane-ruby/actions/workflows/ci.yml/badge.svg)](https://github.com/woodie/humane-ruby/actions/workflows/ci.yml)
6
5
  [![Release](https://img.shields.io/github/v/release/woodie/humane-ruby.svg)](https://github.com/woodie/humane-ruby/releases/latest)
7
6
  [![License](https://img.shields.io/github/license/woodie/humane-ruby.svg)](LICENSE)
8
7
 
9
- Swift's file sizes and relative dates for Ruby
8
+ Getting human-readable file sizes with 1000-based math
9
+ (as the Mac Finder displays) and relative times worded the way Swift's
10
+ `RelativeDateTimeFormatter` does turned out to be a real challenge to get
11
+ both right and simple. The `humane` library exists so a Ruby application can share
12
+ consistent size and time formatting with a Swift application, instead of
13
+ reaching for a library whose output doesn't match Swift's or that's
14
+ complicated to drop in.
10
15
 
11
- Finder-accurate file sizes and relative dates for Ruby, modeled on Swift's [`ByteCountFormatter`](https://developer.apple.com/documentation/foundation/bytecountformatter) and [`RelativeDateTimeFormatter`](https://developer.apple.com/documentation/foundation/relativedatetimeformatter) -- not literal ports (both are closed-source), but the same idea and the same wording: a small, configurable formatter object instead of a bare helper method.
16
+ ```ruby
17
+ require "humane"
18
+
19
+ Humane::SizeFormatter.new.string(from_byte_count: 225_935) # "226 KB"
20
+
21
+ time_formatter = Humane::TimeFormatter.new
22
+ time_formatter.string(at: Time.now - 180, relative_to: Time.now) # "3 minutes ago"
23
+ ```
24
+
25
+ Corresponding functions in Swift will have consistent output.
26
+
27
+ ```swift
28
+ import Foundation
29
+
30
+ ByteCountFormatter.string(fromByteCount: Int64(225935), countStyle: .file) // "226 KB"
31
+
32
+ let formatter = RelativeDateTimeFormatter(); formatter.unitsStyle = .full
33
+ formatter.localizedString(for: time, relativeTo: now) // "3 minutes ago"
34
+ ```
12
35
 
13
36
  ## Install
14
37
 
@@ -22,33 +45,28 @@ or in a `Gemfile`:
22
45
  gem "humane"
23
46
  ```
24
47
 
25
- ## Usage
48
+ ## Beyond Foundation's defaults
26
49
 
27
- ```ruby
28
- require "humane"
50
+ Two options on `Humane::TimeFormatter`, both off by default so it matches
51
+ `RelativeDateTimeFormatter` exactly out of the box:
29
52
 
30
- size_formatter = Humane::SizeFormatter.new
31
- size_formatter.string(from_byte_count: 225_935) # "226 KB" -- 1000-based math,
32
- # capitalized units, matching
33
- # Finder, not Rails'
34
- # number_to_human_size (1024-
35
- # based despite the same label)
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.
36
60
 
37
- time_formatter = Humane::TimeFormatter.new # collapse_minute: true
38
- time_formatter.string(at: scanned_at, relative_to: Time.now)
39
- # "3 minutes ago" / "in 3 minutes" / "less than a minute ago"
61
+ ```ruby
62
+ Humane::TimeFormatter.new(approximate: true).string(at: t - 15 * 3600, relative_to: t)
63
+ # => "about 15 hours ago"
40
64
  ```
41
65
 
42
- `Humane::TimeFormatter.new` uses `at:`, not `for:` -- Ruby's `for` is a
43
- reserved word, and while a `for:` keyword argument technically parses, reading
44
- it back out inside the method needs `binding.local_variable_get(:for)`. Not
45
- worth it just to match Swift's `localizedString(for:relativeTo:)` label
46
- literally.
47
-
48
66
  ## Scope
49
67
 
50
- Only what lambada and scandalous actually need today: Finder's `.file`
51
- byte-count style, and a numeric (non-calendar-aware) relative time style.
52
- `ByteCountFormatter`'s `allowed_units`/alternate `count_style`s and
53
- `RelativeDateTimeFormatter`'s `:named` style (`"yesterday"`, calendar-
54
- boundary-aware) aren't implemented -- contributions welcome.
68
+ Finder's `.file` byte-count style, and a numeric (non-calendar-aware)
69
+ relative time style -- that's the whole surface area today.
70
+ `allowed_units`/alternate `count_style`s and a `:named` style
71
+ (`"yesterday"`, calendar-boundary-aware) aren't implemented -- contributions
72
+ welcome.
@@ -1,11 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Humane
4
- # Formats one time relative to another the way Finder-adjacent tools do, symmetric "X ago"/"X from now".
4
+ # Formats one time relative to another the way Finder-adjacent tools do.
5
5
  class TimeFormatter
6
- # collapse_minute buckets anything under a minute as "less than a minute ago/from now". Defaults to true.
7
- def initialize(collapse_minute: true)
8
- @collapse_minute = collapse_minute
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
+ # 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)
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.
@@ -14,7 +16,7 @@ module Humane
14
16
  future = seconds.negative?
15
17
  seconds = seconds.abs
16
18
 
17
- if @collapse_minute && seconds < 60
19
+ if !@include_seconds && seconds < 60
18
20
  return future ? "in less than a minute" : "less than a minute ago"
19
21
  end
20
22
 
@@ -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.2.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.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Woodell