humane 0.1.0 → 0.3.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: fb176a1cd9cc6c573a593535d45a97d2e64677c54c55726b647374a0213e8f06
4
- data.tar.gz: 80474100589a2447566088e8fd5d0c4ea9eb232c7c2a1ebc7a054a7d802c539b
3
+ metadata.gz: 433a88d823e187b4481fb3c2632ba816b487b3b8047f272b5c4b17a930b6227b
4
+ data.tar.gz: 5c90d3a7ee778274382e8ca25d604d39e672333ddfb8985941b97e7ce345f335
5
5
  SHA512:
6
- metadata.gz: 4dd0a51d9b1571f64fdbab8e8708b9eaf5e018772fdf5d22bb0107eeaef8dcf778048046ab025f93e7d4c4dc7f6c51c344cb4efb2d9a35ad09a9adfec8d53acf
7
- data.tar.gz: 7e190db93ce2f15afb477f207fc11519587da08bdcb684d46bd363c7756934579cac460b89364b42c5cbe1dea967d7052eba2d78e662a9114abbca010a60505f
6
+ metadata.gz: 42ceb33a1bd570aa85b8cc5a9417204fcd9d6041e79e6e44c3b7a65469f9139c2897019afef22aab9269bd3956599c2e1ed430021a64f399a2fc6fac0e996c30
7
+ data.tar.gz: fc9eaa4b043bf6547444631fde9fa2bba69c493ecc07d5e43375ac65f6a20c9f607c73776c7f3e535080a4d2c682747cb8bff8e017e5ddfe67a0f79e3869a4ae
data/README.md CHANGED
@@ -5,9 +5,33 @@
5
5
  [![Release](https://img.shields.io/github/v/release/woodie/humane-ruby.svg)](https://github.com/woodie/humane-ruby/releases/latest)
6
6
  [![License](https://img.shields.io/github/license/woodie/humane-ruby.svg)](LICENSE)
7
7
 
8
- 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.
9
15
 
10
- 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, and `TimeFormatter`'s wording is a deliberate departure), but the same idea: 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
+ ```
11
35
 
12
36
  ## Install
13
37
 
@@ -21,33 +45,10 @@ or in a `Gemfile`:
21
45
  gem "humane"
22
46
  ```
23
47
 
24
- ## Usage
25
-
26
- ```ruby
27
- require "humane"
28
-
29
- size_formatter = Humane::SizeFormatter.new
30
- size_formatter.string(from_byte_count: 225_935) # "226 KB" -- 1000-based math,
31
- # capitalized units, matching
32
- # Finder, not Rails'
33
- # number_to_human_size (1024-
34
- # based despite the same label)
35
-
36
- time_formatter = Humane::TimeFormatter.new # collapse_minute: true
37
- time_formatter.string(at: scanned_at, relative_to: Time.now)
38
- # "3 minutes ago" / "3 minutes from now" / "less than a minute ago"
39
- ```
40
-
41
- `Humane::TimeFormatter.new` uses `at:`, not `for:` -- Ruby's `for` is a
42
- reserved word, and while a `for:` keyword argument technically parses, reading
43
- it back out inside the method needs `binding.local_variable_get(:for)`. Not
44
- worth it just to match Swift's `localizedString(for:relativeTo:)` label
45
- literally.
46
-
47
48
  ## Scope
48
49
 
49
- Only what lambada and scandalous actually need today: Finder's `.file`
50
- byte-count style, and a numeric (non-calendar-aware) relative time style.
51
- `ByteCountFormatter`'s `allowed_units`/alternate `count_style`s and
52
- `RelativeDateTimeFormatter`'s `:named` style (`"yesterday"`, calendar-
53
- boundary-aware) aren't implemented -- contributions welcome.
50
+ Finder's `.file` byte-count style, and a numeric (non-calendar-aware)
51
+ relative time style -- that's the whole surface area today.
52
+ `allowed_units`/alternate `count_style`s and a `:named` style
53
+ (`"yesterday"`, calendar-boundary-aware) aren't implemented -- contributions
54
+ welcome.
@@ -1,11 +1,11 @@
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
+ def initialize(include_seconds: false)
8
+ @include_seconds = include_seconds
9
9
  end
10
10
 
11
11
  # Returns the time at `at` relative to `relative_to` as a human-readable string.
@@ -14,8 +14,8 @@ module Humane
14
14
  future = seconds.negative?
15
15
  seconds = seconds.abs
16
16
 
17
- if @collapse_minute && seconds < 60
18
- return future ? "less than a minute from now" : "less than a minute ago"
17
+ if !@include_seconds && seconds < 60
18
+ return future ? "in less than a minute" : "less than a minute ago"
19
19
  end
20
20
 
21
21
  text =
@@ -29,7 +29,7 @@ module Humane
29
29
  pluralize((seconds / 86_400.0).round, "day")
30
30
  end
31
31
 
32
- future ? "#{text} from now" : "#{text} ago"
32
+ future ? "in #{text}" : "#{text} ago"
33
33
  end
34
34
 
35
35
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Humane
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: humane
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Woodell
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2026-07-08 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: Finder-accurate file sizes and relative dates for Ruby, modeled on Swift's
14
13
  ByteCountFormatter and RelativeDateTimeFormatter.
@@ -30,7 +29,6 @@ licenses:
30
29
  metadata:
31
30
  homepage_uri: https://github.com/woodie/humane-ruby
32
31
  source_code_uri: https://github.com/woodie/humane-ruby
33
- post_install_message:
34
32
  rdoc_options: []
35
33
  require_paths:
36
34
  - lib
@@ -45,8 +43,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
45
43
  - !ruby/object:Gem::Version
46
44
  version: '0'
47
45
  requirements: []
48
- rubygems_version: 3.3.5
49
- signing_key:
46
+ rubygems_version: 3.6.9
50
47
  specification_version: 4
51
48
  summary: Swift's file sizes and relative dates for Ruby
52
49
  test_files: []