refinements 14.1.0 → 14.2.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: d020697c0869daf2e3bcc99d87db35971ed15bd9a232b930f5420fdd1e463822
4
+ data.tar.gz: 1a90b8c69ca796cb3445939e9661d6f41f89eb54f52142f98d868783d067402c
5
5
  SHA512:
6
- metadata.gz: 350c7deafda58ba8f4770ffd993ab3bdd4519fe510e18bd9f52fb383a561d5790ce00598c06aae65d20b585fc848388218118bac0430995ab19350288160cecd
7
- data.tar.gz: aa43882784697baa26fdb2a522ef15ff359baa7ff91b72c6234e55b7c10d99d347bce79b33aa15051980f5ef6851eaa4536c7453f670c6bdcef51621990a5eff
6
+ metadata.gz: b855ed89c0deadec703182707a3a77f025ac02dd9893e81bacc1bb63f85dcf9e7c3ca63cdabd448417af4e4be1b92920271129ed3e575afc9210acc73cc8f01c
7
+ data.tar.gz: 7f3933aef0ab201659aa807360f84c9dacc7844e2b86800e49cebf20f62dcb3fab20e1357468df90b4b2c5e931c064f0373743097206fd180f62cdaf2214cfdd
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
@@ -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.2.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
@@ -1,5 +1,2 @@
1
- ?��ݍ}
2
- ^y VP6$.�_��@�)z
3
- w�p _4��̳��βg_�F8��~
4
- l�R�>C�wI،7 韌��柀x������<2�X��f:��~~d� ���D���~m8���#������WG��r?8�,7�! ��cO :��b&4���������)S����
5
- �8����yFLY!z��Zy9��{��6-�b}���������H�3��V��1cnr7~@S��Sy!
1
+ A7g�"��^0��:~3Ef��� 7%��8�ڲ^�V��mB6�`K{#*{��5"��tG���\�*��g��%���@w��$�!<~
2
+ �W(��`�q�֊@�W��3l�;2��0��\���V�hW_wOˡ-����p�����C�b,f��;$�ߏ"$w�[���Ц.k�Fg��'�R/ߤ�����a�>�8H�/������=��l<��'�E���U�N�W=�1_P�
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.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brooke Kuhlmann
@@ -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:
metadata.gz.sig CHANGED
Binary file