refinements 11.1.3 → 12.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3abadfec48a3ef48adc2130ab1e9a451fd27cacbb7e5c18c8332757e5db91937
4
- data.tar.gz: fa754c6b2c1a19e214edce661a362e93f521381e1ab1fb8f1ebd99fbbca0b13f
3
+ metadata.gz: a497496f7842bab471ed5d7703054c050aad5d62dcdc317678d4d40d871dbefe
4
+ data.tar.gz: 117975da807b741ff3b9dff8d585dbba8d00243af81f7c8daf964cda0de6b7b9
5
5
  SHA512:
6
- metadata.gz: 67c3fb92363ce57b335c74ad29f492eeb634504fb8ba3a97b1374c3fe4cf4a7fb9ed1b274273640e5dac6f9d146753c9df79b4cc90156c089b2e3fba3d005a16
7
- data.tar.gz: 4a1d3d5533fe0c76efddd34120066a559835394cf471577b539ef9450a5557243de47647968be6dfb78f6cb19e44fd5cc05a4059be51881c7da6d6932575d9dc
6
+ metadata.gz: bf13a20ac2e6f6849ab0ae65929f0574c6569cf5840e8aada271f63749326c319531c1e6a572b9bad2526cfb5280df48d9f1ee638a4cff87105e2429edad2d23
7
+ data.tar.gz: 6b07855b0b39fe4d45509e9cbc11c8e7392a686cd485b73cab3c6134ad6d709f3355cb0b3f18bff81becbfb87058c8f3e0e872ad7db69a657a1c25e8d1390533
checksums.yaml.gz.sig CHANGED
Binary file
data/README.adoc CHANGED
@@ -13,7 +13,6 @@ toc::[]
13
13
  Enhances the following objects:
14
14
 
15
15
  * Array
16
- * BigDecimal
17
16
  * Data
18
17
  * DateTime
19
18
  * Hash
@@ -23,7 +22,7 @@ Enhances the following objects:
23
22
  * Pathname
24
23
  * String
25
24
  * StringIO
26
- * Structs
25
+ * Struct
27
26
 
28
27
  == Requirements
29
28
 
@@ -77,19 +76,18 @@ gem "refinements", require: false
77
76
 
78
77
  [source,ruby]
79
78
  ----
80
- require "refinements/arrays"
81
- require "refinements/big_decimals"
79
+ require "refinements/array"
82
80
  require "refinements/data"
83
- require "refinements/date_times"
84
- require "refinements/hashes"
85
- require "refinements/ios"
86
- require "refinements/pathnames"
87
- require "refinements/strings"
88
- require "refinements/string_ios"
89
- require "refinements/structs"
90
- require "refinements/symbols"
91
- require "refinements/log_devices"
92
- require "refinements/loggers"
81
+ require "refinements/date_time"
82
+ require "refinements/hash"
83
+ require "refinements/io"
84
+ require "refinements/log_device"
85
+ require "refinements/logger"
86
+ require "refinements/pathname"
87
+ require "refinements/string"
88
+ require "refinements/string_io"
89
+ require "refinements/struct"
90
+ require "refinements/symbol"
93
91
  ----
94
92
 
95
93
  === Using
@@ -100,19 +98,18 @@ refinement(s):
100
98
  [source,ruby]
101
99
  ----
102
100
  class Example
103
- using Refinements::Arrays
104
- using Refinements::BigDecimals
101
+ using Refinements::Array
105
102
  using Refinements::Data
106
- using Refinements::DateTimes
107
- using Refinements::Hashes
108
- using Refinements::IOs
109
- using Refinements::Pathnames
110
- using Refinements::Strings
111
- using Refinements::StringIOs
112
- using Refinements::Structs
113
- using Refinements::Symbols
114
- using Refinements::LogDevices
115
- using Refinements::Loggers
103
+ using Refinements::DateTime
104
+ using Refinements::Hash
105
+ using Refinements::IO
106
+ using Refinements::LogDevice
107
+ using Refinements::Logger
108
+ using Refinements::Pathname
109
+ using Refinements::String
110
+ using Refinements::StringIO
111
+ using Refinements::Struct
112
+ using Refinements::Symbol
116
113
  end
117
114
  ----
118
115
 
@@ -280,9 +277,9 @@ needs to be a specific size with padded values.
280
277
 
281
278
  [source,ruby]
282
279
  ----
283
- [1].pad 0 # [1]
284
- [1].pad 0, max: 3 # [1, 0, 0]
285
- [1, 2].pad 3, max: 3 # [1, 2, 3]
280
+ ["a"].pad 0 # ["a"]
281
+ ["a"].pad "-", 3 # ["a", "-", "-"]
282
+ %w[a b].pad "-", 3 # ["a", "b", "-"]
286
283
  ----
287
284
 
288
285
  ===== #pick
@@ -393,17 +390,6 @@ Builds upon and enhances `#to_sentence` further by answering a sentence which al
393
390
  %w[eins zwei drei].to_usage "und", delimiter: " " # "\"eins\" \"zwei\" und \"drei\""
394
391
  ----
395
392
 
396
- ==== Big Decimal
397
-
398
- ===== #inspect
399
-
400
- Allows one to inspect a big decimal with numeric representation.
401
-
402
- [source,ruby]
403
- ----
404
- BigDecimal.new("5.0E-10").inspect # "#<BigDecimal:3fd3d458fe84 0.0000000005>"
405
- ----
406
-
407
393
  ==== Data
408
394
 
409
395
  ===== #diff
@@ -1288,11 +1274,11 @@ well in other languages.
1288
1274
  [source,ruby]
1289
1275
  ----
1290
1276
  "apple".pluralize "s" # apples
1291
- "apple".pluralize "s", count: 0 # apples
1292
- "apple".pluralize "s", count: 1 # apple
1293
- "apple".pluralize "s", count: -1 # apple
1294
- "apple".pluralize "s", count: 2 # apples
1295
- "apple".pluralize "s", count: -2 # apples
1277
+ "apple".pluralize "s", 0 # apples
1278
+ "apple".pluralize "s", 1 # apple
1279
+ "apple".pluralize "s", -1 # apple
1280
+ "apple".pluralize "s", 2 # apples
1281
+ "apple".pluralize "s", -2 # apples
1296
1282
  "cactus".pluralize "i", replace: "us" # cacti
1297
1283
  "cul-de-sac".pluralize "ls", replace: "l" # culs-de-sac
1298
1284
  ----
@@ -1308,11 +1294,11 @@ well in other languages.
1308
1294
  [source,ruby]
1309
1295
  ----
1310
1296
  "apples".singularize "s" # apple
1311
- "apples".singularize "s", count: 0 # apples
1312
- "apples".singularize "s", count: 1 # apple
1313
- "apples".singularize "s", count: -1 # apple
1314
- "apples".singularize "s", count: 2 # apples
1315
- "apples".singularize "s", count: -2 # apples
1297
+ "apples".singularize "s", 0 # apples
1298
+ "apples".singularize "s", 1 # apple
1299
+ "apples".singularize "s", -1 # apple
1300
+ "apples".singularize "s", 2 # apples
1301
+ "apples".singularize "s", -2 # apples
1316
1302
  "cacti".singularize "i", replace: "us" # cactus
1317
1303
  "culs-de-sac".singularize "ls", replace: "l" # cul-de-sac
1318
1304
  ----
@@ -1345,6 +1331,30 @@ Answers a title string with proper capitalization of each word.
1345
1331
  "ThisIsAnExample".titleize # "This Is An Example"
1346
1332
  ----
1347
1333
 
1334
+ ===== #truncate
1335
+
1336
+ Answers a truncated, non-mutated, string for given length with optional delimiter and/or overflow.
1337
+
1338
+ The delimiter is the second positional parameter (optional) and is `nil` by default. A custom string or regular expression can be used to customize truncation behavior.
1339
+
1340
+ 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 truncated, so keep this in mind when setting truncation length.
1341
+
1342
+ [source,ruby]
1343
+ ----
1344
+ demo = "It was the best of times"
1345
+ length = demo.length
1346
+
1347
+ demo.truncate 9 # "It was..."
1348
+ demo.truncate 12 # "It was th..."
1349
+ demo.truncate length # "It was the best of times"
1350
+ demo.truncate Float::INFINITY # "It was the best of times"
1351
+ demo.truncate 12, " " # "It was..."
1352
+ demo.truncate 12, /\s/ # "It was..."
1353
+ demo.truncate 6, trailer: "" # "It was"
1354
+ demo.truncate 16, trailer: "... (more)" # "It was... (more)"
1355
+ "demo".truncate 3 # "..."
1356
+ ----
1357
+
1348
1358
  ===== #to_bool
1349
1359
 
1350
1360
  Answers string as a boolean.
@@ -1551,7 +1561,7 @@ with public methods in order to not break encapsulation.
1551
1561
  ----
1552
1562
 
1553
1563
  ⚠️ Use of `#call` without any arguments or block should be avoided in order to not incur extra
1554
- processing costs since the original symbol-to-proc call can used instead.
1564
+ processing costs since the original symbol-to-proc call can be used instead.
1555
1565
 
1556
1566
  == Development
1557
1567
 
@@ -4,8 +4,8 @@ require "refinements/shared/enumerables/many"
4
4
 
5
5
  module Refinements
6
6
  # Provides additional enhancements to the Array primitive.
7
- module Arrays
8
- refine Array do
7
+ module Array
8
+ refine ::Array do
9
9
  import_methods Shared::Enumerables::Many
10
10
 
11
11
  def combinatorial?(other) = !other.empty? && size == union(other).size
@@ -31,7 +31,7 @@ module Refinements
31
31
 
32
32
  def minimum(key) = map(&key).min
33
33
 
34
- def pad(value, max: size) = dup.fill(value, size..(max - 1))
34
+ def pad(value, max = size) = dup.fill(value, size..(max - 1))
35
35
 
36
36
  def pick(*keys)
37
37
  return if empty?
@@ -4,8 +4,8 @@ require "date"
4
4
 
5
5
  module Refinements
6
6
  # Provides additional enhancements to the DateTime primitive.
7
- module DateTimes
8
- refine DateTime.singleton_class do
7
+ module DateTime
8
+ refine ::DateTime.singleton_class do
9
9
  def utc = now.new_offset(0)
10
10
  end
11
11
  end
@@ -4,8 +4,8 @@ require "refinements/shared/enumerables/many"
4
4
 
5
5
  module Refinements
6
6
  # Provides additional enhancements to the Hash primitive.
7
- module Hashes
8
- refine Hash.singleton_class do
7
+ module Hash
8
+ refine ::Hash.singleton_class do
9
9
  def infinite
10
10
  new { |new_hash, missing_key| new_hash[missing_key] = new(&new_hash.default_proc) }
11
11
  end
@@ -13,7 +13,7 @@ module Refinements
13
13
  def with_default(value) = new { |new_hash, missing_key| new_hash[missing_key] = value }
14
14
  end
15
15
 
16
- refine Hash do
16
+ refine ::Hash do
17
17
  import_methods Shared::Enumerables::Many
18
18
 
19
19
  def compress = compact.delete_if { |_key, value| value.respond_to?(:empty?) && value.empty? }
@@ -57,7 +57,7 @@ module Refinements
57
57
  reduce({}) do |accumulator, (key, value)|
58
58
  flat_key = prefix ? :"#{prefix}#{delimiter}#{key}" : key
59
59
 
60
- next accumulator.merge flat_key => value unless value in Hash
60
+ next accumulator.merge flat_key => value unless value.is_a? ::Hash
61
61
 
62
62
  accumulator.merge(recurse { value.flatten_keys prefix: flat_key, delimiter: })
63
63
  end
@@ -69,7 +69,7 @@ module Refinements
69
69
  return self unless block
70
70
 
71
71
  transform = yield self
72
- transform.each { |key, value| transform[key] = value.recurse(&block) if value in Hash }
72
+ transform.each { |key, value| transform[key] = value.recurse(&block) if value.is_a? ::Hash }
73
73
  end
74
74
 
75
75
  def stringify_keys = transform_keys(&:to_s)
@@ -4,8 +4,8 @@ require "refinements/shared/ios/reread"
4
4
 
5
5
  module Refinements
6
6
  # Provides additional enhancements to the IO primitive.
7
- module IOs
8
- refine IO.singleton_class do
7
+ module IO
8
+ refine ::IO.singleton_class do
9
9
  def void
10
10
  new(sysopen("/dev/null", "w+")).then do |io|
11
11
  return io unless block_given?
@@ -16,7 +16,7 @@ module Refinements
16
16
  end
17
17
  end
18
18
 
19
- refine IO do
19
+ refine ::IO do
20
20
  import_methods Shared::IOs::Reread
21
21
 
22
22
  def redirect other
@@ -1,18 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "logger"
4
- require "refinements/string_ios"
4
+ require "refinements/string_io"
5
5
 
6
6
  module Refinements
7
7
  # Provides additional enhancements to a log device.
8
- module LogDevices
9
- using StringIOs
8
+ module LogDevice
9
+ using StringIO
10
10
 
11
- refine Logger::LogDevice do
11
+ refine ::Logger::LogDevice do
12
12
  def reread
13
13
  case dev
14
- when File then dev.class.new(dev).read
15
- when StringIO then dev.reread
14
+ when ::File then dev.class.new(dev).read
15
+ when ::StringIO then dev.reread
16
16
  else ""
17
17
  end
18
18
  end
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "refinements/log_devices"
3
+ require "refinements/log_device"
4
4
 
5
5
  module Refinements
6
6
  # Provides additional enhancements to a logger.
7
- module Loggers
8
- using LogDevices
7
+ module Logger
8
+ using LogDevice
9
9
 
10
- refine Logger do
10
+ refine ::Logger do
11
11
  def reread = @logdev.reread
12
12
 
13
13
  alias_method :any, :unknown
@@ -4,7 +4,7 @@ require "pathname"
4
4
 
5
5
  module Refinements
6
6
  # Provides additional enhancements to the Pathname primitive.
7
- module Pathnames
7
+ module Pathname
8
8
  refine Kernel do
9
9
  def Pathname object
10
10
  return super(String(object)) unless object
@@ -13,7 +13,7 @@ module Refinements
13
13
  end
14
14
  end
15
15
 
16
- refine Pathname.singleton_class do
16
+ refine ::Pathname.singleton_class do
17
17
  def home = new ENV.fetch("HOME", "")
18
18
 
19
19
  def make_temp_dir prefix: "temp-", suffix: nil, root: nil
@@ -29,7 +29,7 @@ module Refinements
29
29
  def root = new(File::SEPARATOR)
30
30
  end
31
31
 
32
- refine Pathname do
32
+ refine ::Pathname do
33
33
  def change_dir
34
34
  if block_given?
35
35
  Dir.chdir(self) { |path| yield Pathname(path) }
@@ -2,10 +2,10 @@
2
2
 
3
3
  module Refinements
4
4
  # Provides additional enhancements to the String primitive.
5
- module Strings
5
+ module String
6
6
  DELIMITERS = %r([a-z][A-Z]|\s*-\s*|\s*/\s*|\s*:+\s*|\s*_\s*|\s+)
7
7
 
8
- refine String do
8
+ refine ::String do
9
9
  def blank? = empty? || match?(/\A[[:space:]]*\z/)
10
10
 
11
11
  def camelcase
@@ -38,9 +38,9 @@ module Refinements
38
38
  self[(minimum + 1)..]
39
39
  end
40
40
 
41
- def pluralize(suffix, replace: /$/, count: 0) = count.abs == 1 ? self : sub(replace, suffix)
41
+ def pluralize(suffix, count = 0, replace: /$/) = count.abs == 1 ? self : sub(replace, suffix)
42
42
 
43
- def singularize(suffix, replace: "", count: 1) = count.abs == 1 ? sub(suffix, replace) : self
43
+ def singularize(suffix, count = 1, replace: "") = count.abs == 1 ? sub(suffix, replace) : self
44
44
 
45
45
  def snakecase
46
46
  return downcase unless match? DELIMITERS
@@ -60,7 +60,15 @@ module Refinements
60
60
  .then { |parts| combine parts, :up, "/" }
61
61
  end
62
62
 
63
- def to_bool = %w[true yes on t y 1].include?(downcase.strip)
63
+ def truncate to, delimiter = nil, trailer: "..."
64
+ return dup if length <= to
65
+
66
+ offset = to - trailer.length
67
+ maximum = delimiter ? rindex(delimiter, offset) || offset : offset
68
+ "#{self[...maximum]}#{trailer}"
69
+ end
70
+
71
+ def to_bool = %w[true yes on t y 1].include? downcase.strip
64
72
 
65
73
  def up = empty? ? self : first.upcase + self[1, size]
66
74
 
@@ -5,8 +5,8 @@ require "stringio"
5
5
 
6
6
  module Refinements
7
7
  # Provides additional enhancements to the StringIO primitive.
8
- module StringIOs
9
- refine StringIO do
8
+ module StringIO
9
+ refine ::StringIO do
10
10
  import_methods Shared::IOs::Reread
11
11
  end
12
12
  end
@@ -4,12 +4,12 @@ require "refinements/shared/values/diff"
4
4
 
5
5
  module Refinements
6
6
  # Provides additional enhancements to the Struct primitive.
7
- module Structs
8
- refine Struct.singleton_class do
7
+ module Struct
8
+ refine ::Struct.singleton_class do
9
9
  def with_positions(*values) = keyword_init? ? new(**members.zip(values).to_h) : new(*values)
10
10
  end
11
11
 
12
- refine Struct do
12
+ refine ::Struct do
13
13
  import_methods Shared::Values::Diff
14
14
 
15
15
  def merge(...) = dup.merge!(...)
@@ -2,8 +2,8 @@
2
2
 
3
3
  module Refinements
4
4
  # Provides additional enhancements to the Symbol primitive.
5
- module Symbols
6
- refine Symbol do
5
+ module Symbol
6
+ refine ::Symbol do
7
7
  # rubocop:todo Naming/BlockForwarding
8
8
  def call(*arguments, &block)
9
9
  proc { |receiver| receiver.public_send self, *arguments, &block }
data/lib/refinements.rb CHANGED
@@ -1,15 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "refinements/arrays"
4
- require "refinements/big_decimals"
3
+ require "refinements/array"
5
4
  require "refinements/data"
6
- require "refinements/date_times"
7
- require "refinements/hashes"
8
- require "refinements/ios"
9
- require "refinements/log_devices"
10
- require "refinements/loggers"
11
- require "refinements/pathnames"
12
- require "refinements/string_ios"
13
- require "refinements/strings"
14
- require "refinements/structs"
15
- require "refinements/symbols"
5
+ require "refinements/date_time"
6
+ require "refinements/hash"
7
+ require "refinements/io"
8
+ require "refinements/log_device"
9
+ require "refinements/logger"
10
+ require "refinements/pathname"
11
+ require "refinements/string"
12
+ require "refinements/string_io"
13
+ require "refinements/struct"
14
+ require "refinements/symbol"
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 = "11.1.3"
5
+ spec.version = "12.0.0"
6
6
  spec.authors = ["Brooke Kuhlmann"]
7
7
  spec.email = ["brooke@alchemists.io"]
8
8
  spec.homepage = "https://alchemists.io/projects/refinements"
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.signing_key = Gem.default_key_path
23
23
  spec.cert_chain = [Gem.default_cert_path]
24
24
 
25
- spec.required_ruby_version = [">= 3.2", "<= 3.3"]
25
+ spec.required_ruby_version = "~> 3.3"
26
26
 
27
27
  spec.files = Dir["*.gemspec", "lib/**/*"]
28
28
  spec.extra_rdoc_files = Dir["README*", "LICENSE*"]
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: 11.1.3
4
+ version: 12.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brooke Kuhlmann
@@ -35,7 +35,7 @@ cert_chain:
35
35
  3n5C8/6Zh9DYTkpcwPSuIfAga6wf4nXc9m6JAw8AuMLaiWN/r/2s4zJsUHYERJEu
36
36
  gZGm4JqtuSg8pYjPeIJxS960owq+SfuC+jxqmRA54BisFCv/0VOJi7tiJVY=
37
37
  -----END CERTIFICATE-----
38
- date: 2023-12-25 00:00:00.000000000 Z
38
+ date: 2024-01-01 00:00:00.000000000 Z
39
39
  dependencies: []
40
40
  description:
41
41
  email:
@@ -49,22 +49,21 @@ files:
49
49
  - LICENSE.adoc
50
50
  - README.adoc
51
51
  - lib/refinements.rb
52
- - lib/refinements/arrays.rb
53
- - lib/refinements/big_decimals.rb
52
+ - lib/refinements/array.rb
54
53
  - lib/refinements/data.rb
55
- - lib/refinements/date_times.rb
56
- - lib/refinements/hashes.rb
57
- - lib/refinements/ios.rb
58
- - lib/refinements/log_devices.rb
59
- - lib/refinements/loggers.rb
60
- - lib/refinements/pathnames.rb
54
+ - lib/refinements/date_time.rb
55
+ - lib/refinements/hash.rb
56
+ - lib/refinements/io.rb
57
+ - lib/refinements/log_device.rb
58
+ - lib/refinements/logger.rb
59
+ - lib/refinements/pathname.rb
61
60
  - lib/refinements/shared/enumerables/many.rb
62
61
  - lib/refinements/shared/ios/reread.rb
63
62
  - lib/refinements/shared/values/diff.rb
64
- - lib/refinements/string_ios.rb
65
- - lib/refinements/strings.rb
66
- - lib/refinements/structs.rb
67
- - lib/refinements/symbols.rb
63
+ - lib/refinements/string.rb
64
+ - lib/refinements/string_io.rb
65
+ - lib/refinements/struct.rb
66
+ - lib/refinements/symbol.rb
68
67
  - refinements.gemspec
69
68
  homepage: https://alchemists.io/projects/refinements
70
69
  licenses:
@@ -83,10 +82,7 @@ require_paths:
83
82
  - lib
84
83
  required_ruby_version: !ruby/object:Gem::Requirement
85
84
  requirements:
86
- - - ">="
87
- - !ruby/object:Gem::Version
88
- version: '3.2'
89
- - - "<="
85
+ - - "~>"
90
86
  - !ruby/object:Gem::Version
91
87
  version: '3.3'
92
88
  required_rubygems_version: !ruby/object:Gem::Requirement
metadata.gz.sig CHANGED
Binary file
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "bigdecimal"
4
-
5
- module Refinements
6
- # Provides additional enhancements to the BigDecimal primitive.
7
- module BigDecimals
8
- refine BigDecimal do
9
- def inspect = format("#<BigDecimal:%<id>s %<string>s>", id: object_id, string: to_s("F"))
10
- end
11
- end
12
- end