refinements 14.1.0 → 14.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: 350ced29340ad39df23a202e41b3721db7293c3993364bc8796cca57bddc764c
4
- data.tar.gz: afff690e4107212f7bd72054637c9ef8da2bd1265de55899f23fe9769fd616af
3
+ metadata.gz: 20c9d5d74ead7b650ecdb5ff437ed6c361ca0c9ab687a594d6532319004a790c
4
+ data.tar.gz: 8900294ef41f48f20b806507bf767deee981d52d15564285045c8f2c09c76eae
5
5
  SHA512:
6
- metadata.gz: 350c7deafda58ba8f4770ffd993ab3bdd4519fe510e18bd9f52fb383a561d5790ce00598c06aae65d20b585fc848388218118bac0430995ab19350288160cecd
7
- data.tar.gz: aa43882784697baa26fdb2a522ef15ff359baa7ff91b72c6234e55b7c10d99d347bce79b33aa15051980f5ef6851eaa4536c7453f670c6bdcef51621990a5eff
6
+ metadata.gz: 92640b1bb83af678329d564731cd7022a1c48dc377e73a8fab982fbaab32268b8f1ca787966882e8a642c1be85f8e1507821c0324e940313c171399f91d71c48
7
+ data.tar.gz: 25581278e1d2ea886df83f4fc0fae01831e8fec601a55e4085bed15a1d07ba183bcb8503c678d0455e6be38c76db41a6ae21becaf71c95a59221c05a36a40fd7
checksums.yaml.gz.sig CHANGED
Binary file
data/README.adoc CHANGED
@@ -25,6 +25,7 @@ Enhances the following objects:
25
25
  * StringIO
26
26
  * Struct
27
27
  * Symbol
28
+ * Time
28
29
 
29
30
  == Requirements
30
31
 
@@ -91,6 +92,7 @@ require "refinements/string"
91
92
  require "refinements/string_io"
92
93
  require "refinements/struct"
93
94
  require "refinements/symbol"
95
+ require "refinements/time"
94
96
  ----
95
97
 
96
98
  === Using
@@ -114,6 +116,7 @@ class Example
114
116
  using Refinements::StringIO
115
117
  using Refinements::Struct
116
118
  using Refinements::Symbol
119
+ using Refinements::Time
117
120
  end
118
121
  ----
119
122
 
@@ -462,6 +465,8 @@ Any object that _is not_ the same type will have a `nil` value as shown in the l
462
465
 
463
466
  Answers new DateTime object for current UTC date/time.
464
467
 
468
+ ⚠️ This method (and refinement) is deprecated and will be removed in the next major release.
469
+
465
470
  [source,ruby]
466
471
  ----
467
472
  DateTime.utc # "#<DateTime: 2019-12-31T18:17:00+00:00 ((2458849j,65820s,181867000n),+0s,2299161j)>"
@@ -1446,21 +1451,20 @@ Answers a title string with proper capitalization of each word.
1446
1451
 
1447
1452
  ===== #trim_end
1448
1453
 
1449
- Answers the trimmed end of a string (non-mutated) for given length with optional delimiter and/or overflow.
1454
+ Answers the trimmed end of a string for maximum length with optional delimiter and/or overflow.
1450
1455
 
1451
- The delimiter is the second positional parameter (optional) and is `nil` by default. A custom string or regular expression can be used to customize behavior.
1456
+ The delimiter is the second positional parameter (optional) and is `nil` by default. A string or regular expression can be used to customize behavior.
1452
1457
 
1453
- The trailer is an optional keyword parameter that is an ellipsis (i.e. `"..."`) by default. The trailer can be a custom or empty string. The string length of the trailer is added to the length of the string being trimmed, so keep this in mind when setting length.
1458
+ The trailer is an optional keyword parameter that is an ellipsis by default. The trailer can be a custom or empty string. The string size of the trailer is added to the size of the string being trimmed, so keep this in mind when setting the maximum length.
1454
1459
 
1455
1460
  [source,ruby]
1456
1461
  ----
1457
1462
  demo = "It was the best of times"
1458
- length = demo.length
1463
+ size = demo.size
1459
1464
 
1460
1465
  demo.trim_end 9 # "It was..."
1461
1466
  demo.trim_end 12 # "It was th..."
1462
- demo.trim_end length # "It was the best of times"
1463
- demo.trim_end Float::INFINITY # "It was the best of times"
1467
+ demo.trim_end size # "It was the best of times"
1464
1468
  demo.trim_end 12, " " # "It was..."
1465
1469
  demo.trim_end 12, /\s/ # "It was..."
1466
1470
  demo.trim_end 6, trailer: "" # "It was"
@@ -1468,6 +1472,22 @@ demo.trim_end 16, trailer: "... (more)" # "It was... (more)"
1468
1472
  "demo".trim_end 3 # "..."
1469
1473
  ----
1470
1474
 
1475
+ ===== #trim_middle
1476
+
1477
+ Answers the trimmed middle of a string for maximum length with optional gap (default: ellipsis). The minimum length is always the size of the gap plus one character on each side of the gap.
1478
+
1479
+ [source,ruby]
1480
+ ----
1481
+ demo = "It was the best of times"
1482
+ size = demo.size
1483
+
1484
+ demo.trim_middle 14 # "It w...times"
1485
+ demo.trim_middle size # "It was the...t of times"
1486
+ "space".trim_middle 5 # "s...e"
1487
+ demo.trim_middle 14, gap: "+" # "It was+ times"
1488
+ demo.trim_middle 3 # "It was the best of times"
1489
+ ----
1490
+
1471
1491
  ===== #truthy?
1472
1492
 
1473
1493
  Answers if string is truthy.
@@ -1633,7 +1653,6 @@ Enhances symbol-to-proc functionality by allowing you to send positional, keywor
1633
1653
 
1634
1654
  [source,ruby]
1635
1655
  ----
1636
-
1637
1656
  %w[clue crow cow].map(&:tr.call("c", "b")) # ["blue", "brow", "bow"]
1638
1657
  [1.3, 1.5, 1.9].map(&:round.call(half: :up)) # [1, 2, 2]
1639
1658
  [%w[a b c], %w[c a b]].map(&:index.call { |element| element == "b" }) # [1, 2]
@@ -1643,6 +1662,18 @@ Enhances symbol-to-proc functionality by allowing you to send positional, keywor
1643
1662
 
1644
1663
  ⚠️ Use of `#call` without any arguments should be avoided in order to not incur extra processing costs since the original symbol-to-proc call can be used instead.
1645
1664
 
1665
+ ==== Time
1666
+
1667
+ ===== #rfc_3339
1668
+
1669
+ Answers time, as a string, in RFC 3339 format.
1670
+
1671
+ [source,ruby]
1672
+ ----
1673
+ Time.now.rfc_3339 # "2026-01-24T08:56:55-07:00"
1674
+ Time.now.utc.rfc_3339 # "2026-01-24T15:57:13+00:00"
1675
+ ----
1676
+
1646
1677
  == Development
1647
1678
 
1648
1679
  To contribute, run:
@@ -6,7 +6,10 @@ module Refinements
6
6
  # Provides additional enhancements to the DateTime primitive.
7
7
  module DateTime
8
8
  refine ::DateTime.singleton_class do
9
- def utc = now.new_offset(0)
9
+ def utc
10
+ warn "`DateTime.#{__method__}` is deprecated, use `Time` instead.", category: :deprecated
11
+ now.new_offset 0
12
+ end
10
13
  end
11
14
  end
12
15
  end
@@ -71,7 +71,7 @@ module Refinements
71
71
 
72
72
  def rewrite = read.then { |content| write yield(content) if block_given? }
73
73
 
74
- def touch at = Time.now
74
+ def touch at = ::Time.now
75
75
  exist? ? utime(at, at) : write("")
76
76
  self
77
77
  end
@@ -34,7 +34,7 @@ module Refinements
34
34
 
35
35
  def last minimum = 0
36
36
  return self if empty?
37
- return self[size - 1] if minimum.zero?
37
+ return self[-1] if minimum.zero?
38
38
  return "" if minimum.negative?
39
39
 
40
40
  self[(minimum + 1)..]
@@ -52,7 +52,15 @@ module Refinements
52
52
  .then { |parts| combine parts, :down, "_" }
53
53
  end
54
54
 
55
- def squish = gsub(/[[:space:]]+/, " ").tap(&:strip!)
55
+ def squish
56
+ duplicate = dup
57
+
58
+ # Find two or more spaces/tabs/newlines or a single space that isn't " ".
59
+ duplicate.gsub!(/([[:space:]]{2,}|[[[:space:]]&&[^ ]])/, " ")
60
+
61
+ duplicate.strip!
62
+ duplicate
63
+ end
56
64
 
57
65
  def titleize
58
66
  return capitalize unless match? DELIMITERS
@@ -62,12 +70,26 @@ module Refinements
62
70
  .then { |parts| combine parts, :up, "/" }
63
71
  end
64
72
 
65
- def trim_end to, delimiter = nil, trailer: "..."
66
- return dup if length <= to
73
+ def trim_end maximum, delimiter = nil, trailer: "..."
74
+ return dup if size <= maximum
75
+
76
+ offset = maximum - trailer.size
77
+ stop = delimiter ? rindex(delimiter, offset) || offset : offset
78
+ "#{self[...stop]}#{trailer}"
79
+ end
80
+
81
+ def trim_middle maximum, gap: "..."
82
+ gap_size = gap.size
83
+ minimum = gap_size + 2
84
+ limit = maximum - gap_size
85
+
86
+ return dup if maximum < minimum || maximum > size
87
+ return "#{first}#{gap}#{last}" if minimum == maximum
88
+
89
+ half = limit / 2
90
+ stop = half.odd? ? half - 1 : half
67
91
 
68
- offset = to - trailer.length
69
- maximum = delimiter ? rindex(delimiter, offset) || offset : offset
70
- "#{self[...maximum]}#{trailer}"
92
+ "#{self[...stop]}#{gap}#{self[-half...]}"
71
93
  end
72
94
 
73
95
  def truthy? = %w[true yes on t y 1].include? downcase.strip
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Refinements
4
+ # Provides additional enhancements to the Symbol primitive.
5
+ module Time
6
+ refine ::Time do
7
+ def rfc_3339 = strftime "%FT%T%:z"
8
+ end
9
+ end
10
+ end
data/lib/refinements.rb CHANGED
@@ -13,3 +13,4 @@ require "refinements/string"
13
13
  require "refinements/string_io"
14
14
  require "refinements/struct"
15
15
  require "refinements/symbol"
16
+ require "refinements/time"
data/refinements.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "refinements"
5
- spec.version = "14.1.0"
5
+ spec.version = "14.3.0"
6
6
  spec.authors = ["Brooke Kuhlmann"]
7
7
  spec.email = ["brooke@alchemists.io"]
8
8
  spec.homepage = "https://alchemists.io/projects/refinements"
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: refinements
3
3
  version: !ruby/object:Gem::Version
4
- version: 14.1.0
4
+ version: 14.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brooke Kuhlmann
@@ -9,9 +9,9 @@ bindir: bin
9
9
  cert_chain:
10
10
  - |
11
11
  -----BEGIN CERTIFICATE-----
12
- MIIENjCCAp6gAwIBAgIBAjANBgkqhkiG9w0BAQsFADBBMQ8wDQYDVQQDDAZicm9v
12
+ MIIENjCCAp6gAwIBAgIBAzANBgkqhkiG9w0BAQsFADBBMQ8wDQYDVQQDDAZicm9v
13
13
  a2UxGjAYBgoJkiaJk/IsZAEZFgphbGNoZW1pc3RzMRIwEAYKCZImiZPyLGQBGRYC
14
- aW8wHhcNMjUwMzIyMTQ1NDE3WhcNMjYwMzIyMTQ1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BeC1onx4qWp+uRX7eVsYS
34
- GiijocTc3enZVrXERetaXj8/9XWs3fB3HWY=
26
+ F5wykJEVMA0GCSqGSIb3DQEBCwUAA4IBgQAG+ykjp+DIXSybGEtX+/ve974mYfN6
27
+ 8U7qcVfRM+qDSOZ+97iu30qUTbVAKIHlHCDKRn3SgOffDUB5VU2MsJBh/3TPKWBZ
28
+ anB/uzMcwOfru+qyA3b7ZFqZzRLWmR5FtPObFxc0gYMT3YvLNHk2Nb9Vjq/PoiGG
29
+ e75PXweDOokwDA5m1gMOz1rdp/dlGMXkSFQg94PPVyUKXgO4VzWTgePSDxOIL+v6
30
+ +OWV6AaEH9BaqxnmdA5ubi0L7bhl0gbN92FxpNO3kpTjww8kme856a+wCK3qyM5w
31
+ 7ZLbUexynDN0Au8eSpT2Bf6ztGmB1S9ffzDJsGX1/lkpMIB51e48Xe2+gzzOgemk
32
+ CdZaGupj6WkarnT8kh/cPtyA5ax4rGX6GOS8meGxzkv8Uy0JSEOYAp6wLfIisYZp
33
+ IJBIXIOkwKKJ0eB5YHrUSJxzpP4LlcIg/eTftaXmJdYjy+2VRrCZYDjfguyLmMjR
34
+ KR9w4/Fjvqy87kCHmxMWa6IL2Vzt1Clm2cA=
35
35
  -----END CERTIFICATE-----
36
36
  date: 1980-01-02 00:00:00.000000000 Z
37
37
  dependencies: []
@@ -62,6 +62,7 @@ files:
62
62
  - lib/refinements/string_io.rb
63
63
  - lib/refinements/struct.rb
64
64
  - lib/refinements/symbol.rb
65
+ - lib/refinements/time.rb
65
66
  - refinements.gemspec
66
67
  homepage: https://alchemists.io/projects/refinements
67
68
  licenses:
@@ -88,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
89
  - !ruby/object:Gem::Version
89
90
  version: '0'
90
91
  requirements: []
91
- rubygems_version: 4.0.4
92
+ rubygems_version: 4.0.10
92
93
  specification_version: 4
93
94
  summary: A collection of core object refinements.
94
95
  test_files: []
metadata.gz.sig CHANGED
Binary file