refinements 12.8.0 → 12.10.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 +4 -4
- checksums.yaml.gz.sig +0 -0
- data/README.adoc +87 -9
- data/lib/refinements/array.rb +6 -1
- data/lib/refinements/hash.rb +8 -2
- data/lib/refinements/module.rb +12 -0
- data/lib/refinements/pathname.rb +2 -2
- data/lib/refinements/struct.rb +6 -1
- data/lib/refinements.rb +1 -0
- data/refinements.gemspec +1 -1
- data.tar.gz.sig +0 -0
- metadata +4 -3
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 56d9d8ea056f8d22d211d5d9ad964b74e5fc86eb4be0b3476a1cd15c0ef74856
|
4
|
+
data.tar.gz: bd3d5e64e0dfd3ae296d81a6b96212390b223f79b91d14fd3af14eab2c2dd867
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 63f889fedadba076370027afce7f0e176bd7e840d6b470d99043033a39f79277a4aebe7b0573e8d6c367432c59e98cb77b26ee62b5148f420121b632d86f8dfb
|
7
|
+
data.tar.gz: 47712bdf7aff52e05671f7fed297bd06f54dfa1e554e529723c3ab932b2204d30630a25906b757769f314c213e004014fc887bcdfd5b6ac36edf1949bd16335d
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/README.adoc
CHANGED
@@ -20,6 +20,7 @@ Enhances the following objects:
|
|
20
20
|
* IO
|
21
21
|
* LogDevice
|
22
22
|
* Logger
|
23
|
+
* Module
|
23
24
|
* Object
|
24
25
|
* Pathname
|
25
26
|
* String
|
@@ -87,6 +88,7 @@ require "refinements/hash"
|
|
87
88
|
require "refinements/io"
|
88
89
|
require "refinements/log_device"
|
89
90
|
require "refinements/logger"
|
91
|
+
require "refinements/module"
|
90
92
|
require "refinements/object"
|
91
93
|
require "refinements/pathname"
|
92
94
|
require "refinements/string"
|
@@ -111,6 +113,7 @@ class Example
|
|
111
113
|
using Refinements::IO
|
112
114
|
using Refinements::LogDevice
|
113
115
|
using Refinements::Logger
|
116
|
+
using Refinements::Module
|
114
117
|
using Refinements::Object
|
115
118
|
using Refinements::Pathname
|
116
119
|
using Refinements::String
|
@@ -186,6 +189,8 @@ Removes given array or elements without mutating itself.
|
|
186
189
|
|
187
190
|
===== #filter_find
|
188
191
|
|
192
|
+
⚠️ _This is deprecated and will be removed in Version 13.0.0._
|
193
|
+
|
189
194
|
Answers the first element which evaluates to true from a filtered collection.
|
190
195
|
|
191
196
|
[source,ruby]
|
@@ -635,7 +640,7 @@ Any object that _is not_ the same type will have a `nil` value as shown in the l
|
|
635
640
|
|
636
641
|
Fetches value for exiting or missing key. Behavior is identical to `#fetch` except when the value of
|
637
642
|
the key is `nil` you'll get the default value instead. This eliminates the need for using an _or_
|
638
|
-
expression `example.fetch(:desired_key) || "default_value"`.
|
643
|
+
expression: `example.fetch(:desired_key) || "default_value"`.
|
639
644
|
|
640
645
|
[source,ruby]
|
641
646
|
----
|
@@ -650,8 +655,7 @@ expression `example.fetch(:desired_key) || "default_value"`.
|
|
650
655
|
|
651
656
|
===== #flatten_keys
|
652
657
|
|
653
|
-
Flattens nested keys as top-level keys without mutating itself. Does not handle nested arrays
|
654
|
-
though.
|
658
|
+
Flattens nested keys as top-level keys without mutating itself. Keys are converted to symbols. Does not handle nested arrays.
|
655
659
|
|
656
660
|
[source,ruby]
|
657
661
|
----
|
@@ -665,8 +669,7 @@ example # {a: {b: 1}}
|
|
665
669
|
|
666
670
|
===== #flatten_keys!
|
667
671
|
|
668
|
-
Flattens nested keys as top-level keys while mutating itself. Does not handle nested arrays
|
669
|
-
though.
|
672
|
+
Flattens nested keys as top-level keys while mutating itself. Keys are converted to symbols. Does not handle nested arrays.
|
670
673
|
|
671
674
|
[source,ruby]
|
672
675
|
----
|
@@ -747,6 +750,52 @@ example.symbolize_keys! # {a: 1, b: 2}
|
|
747
750
|
example # {a: 1, b: 2}
|
748
751
|
----
|
749
752
|
|
753
|
+
===== #transform_value
|
754
|
+
|
755
|
+
Transforms a value for the specified key _only_ if the key exists and a block is given. Otherwise, the original hash is answered. Does not mutate itself.
|
756
|
+
|
757
|
+
[source,ruby]
|
758
|
+
----
|
759
|
+
example = {a: 1, b: 2}
|
760
|
+
|
761
|
+
example.transform_value :b # {a: 1, b: 2}
|
762
|
+
example.transform_value(:b) { 20 } # {a: 1, b: 20}
|
763
|
+
example.transform_value(:b) { |value| value * 10 } # {a: 1, b: 20}
|
764
|
+
example.transform_value :c # {a: 1, b: 2}
|
765
|
+
example.transform_value(:c) { :bogus } # {a: 1, b: 2}
|
766
|
+
----
|
767
|
+
|
768
|
+
The original object _is not_ mutated:
|
769
|
+
|
770
|
+
[source,ruby]
|
771
|
+
----
|
772
|
+
example.transform_value(:b) { 20 } # {a: 1, b: 20}
|
773
|
+
example # {a: 1, b: 2}
|
774
|
+
----
|
775
|
+
|
776
|
+
===== #transform_value!
|
777
|
+
|
778
|
+
Transforms a value for the specified key _only_ if the key exists and a block is given. Otherwise, the original hash is answered. Mutates itself.
|
779
|
+
|
780
|
+
[source,ruby]
|
781
|
+
----
|
782
|
+
example = {a: 1, b: 2}
|
783
|
+
|
784
|
+
example.transform_value! :b # {a: 1, b: 2}
|
785
|
+
example.transform_value!(:b) { 20 } # {a: 1, b: 20}
|
786
|
+
example.transform_value!(:b) { |value| value * 10 } # {a: 1, b: 20}
|
787
|
+
example.transform_value! :c # {a: 1, b: 2}
|
788
|
+
example.transform_value!(:c) { :bogus } # {a: 1, b: 2}
|
789
|
+
----
|
790
|
+
|
791
|
+
The original object _is_ mutated:
|
792
|
+
|
793
|
+
[source,ruby]
|
794
|
+
----
|
795
|
+
example.transform_value!(:b) { 20 } # {a: 1, b: 20}
|
796
|
+
example # {a: 1, b: 20}
|
797
|
+
----
|
798
|
+
|
750
799
|
===== #transform_with
|
751
800
|
|
752
801
|
Transforms values of keys using specific operations (i.e. any object that responds to `#call`). Does not mutate itself and you can transform multiple values at once:
|
@@ -768,7 +817,7 @@ example.transform_with bogus: -> value { value.tr "<>", "" }
|
|
768
817
|
# {email: "<jd@example.com>"}
|
769
818
|
----
|
770
819
|
|
771
|
-
The original object
|
820
|
+
The original object _is not_ mutated:
|
772
821
|
|
773
822
|
[source,ruby]
|
774
823
|
----
|
@@ -796,7 +845,7 @@ example.transform_with! bogus: -> value { value.tr "<>", "" }
|
|
796
845
|
# {email: "<jd@example.com>"}
|
797
846
|
----
|
798
847
|
|
799
|
-
The original object
|
848
|
+
The original object _is_ mutated:
|
800
849
|
|
801
850
|
[source,ruby]
|
802
851
|
----
|
@@ -925,7 +974,8 @@ logger.reread # ""
|
|
925
974
|
|
926
975
|
Allows you to log _any_ message which is identical in behavior and functionality to the `Logger#unknown` method only this requires less typing and better matches the terminology used by the `#unknown` method.
|
927
976
|
|
928
|
-
|
977
|
+
[source,ruby]
|
978
|
+
----
|
929
979
|
logger = Logger.new STDOUT
|
930
980
|
|
931
981
|
logger.any "Test."
|
@@ -933,7 +983,33 @@ logger.any "Test."
|
|
933
983
|
|
934
984
|
logger.any { "Test." }
|
935
985
|
A, [2000-01-10T09:00:00.330719 #44925] ANY -- : Test.
|
936
|
-
|
986
|
+
----
|
987
|
+
|
988
|
+
==== Module
|
989
|
+
|
990
|
+
===== #pseudonym
|
991
|
+
|
992
|
+
Allows you to set a temporary name for your anonymous `Module` (or `Class` since `Class` inherits from `Module`) with a better default than what `+#set_temporary_name+` provides.
|
993
|
+
|
994
|
+
[source,ruby]
|
995
|
+
----
|
996
|
+
Module.new.pseudonym "demo" # demo-44600
|
997
|
+
Module.new.pseudonym "demo", delimiter: "_" # demo_60900
|
998
|
+
Module.new.pseudonym "demo", nil # demo-
|
999
|
+
Module.new.pseudonym "demo", nil, delimiter: nil # demo
|
1000
|
+
----
|
1001
|
+
|
1002
|
+
The same applies for anonymous classes since classes inherit from modules:
|
1003
|
+
|
1004
|
+
[source,ruby]
|
1005
|
+
----
|
1006
|
+
Class.new.pseudonym "demo" # demo-44600 < Object
|
1007
|
+
Class.new.pseudonym "demo", delimiter: "_" # demo_60900 < Object
|
1008
|
+
Class.new.pseudonym "demo", nil # demo- < Object
|
1009
|
+
Class.new.pseudonym "demo", nil, delimiter: nil # demo < Object
|
1010
|
+
----
|
1011
|
+
|
1012
|
+
💡 While convenient, if you find yourself nullifying the suffix and/or delimiter, you're better off using `+#set_temporary_name+`.
|
937
1013
|
|
938
1014
|
==== Object
|
939
1015
|
|
@@ -1554,6 +1630,8 @@ io.to_str # "One, Two."
|
|
1554
1630
|
|
1555
1631
|
===== .with_positions
|
1556
1632
|
|
1633
|
+
⚠️ _This is deprecated and will be removed in Version 13.0.0._
|
1634
|
+
|
1557
1635
|
Answers a struct instance with given positional arguments regardless of
|
1558
1636
|
whether the struct was constructed with positional or keyword arguments.
|
1559
1637
|
|
data/lib/refinements/array.rb
CHANGED
@@ -19,7 +19,12 @@ module Refinements
|
|
19
19
|
|
20
20
|
def excluding(*elements) = self - elements.flatten
|
21
21
|
|
22
|
-
def filter_find(&
|
22
|
+
def filter_find(&)
|
23
|
+
warn "`#{self.class}##{__method__}` is deprecated and will be removed in Version 13.0.0.",
|
24
|
+
category: :deprecated
|
25
|
+
|
26
|
+
block_given? ? lazy.map(&).find(&:itself) : lazy
|
27
|
+
end
|
23
28
|
|
24
29
|
def including(*elements) = self + elements.flatten
|
25
30
|
|
data/lib/refinements/hash.rb
CHANGED
@@ -80,9 +80,15 @@ module Refinements
|
|
80
80
|
|
81
81
|
def symbolize_keys! = transform_keys!(&:to_sym)
|
82
82
|
|
83
|
-
def
|
83
|
+
def transform_value(key, &) = dup.transform_value!(key, &)
|
84
84
|
|
85
|
-
def
|
85
|
+
def transform_value! key
|
86
|
+
block_given? && key?(key) ? merge!(key => yield(self[key])) : self
|
87
|
+
end
|
88
|
+
|
89
|
+
def transform_with(**) = dup.transform_with!(**)
|
90
|
+
|
91
|
+
def transform_with!(**operations)
|
86
92
|
operations.each { |key, function| self[key] = function.call self[key] if key? key }
|
87
93
|
self
|
88
94
|
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Refinements
|
4
|
+
# Provides additional enhancements to the Symbol primitive.
|
5
|
+
module Module
|
6
|
+
refine ::Module do
|
7
|
+
def pseudonym prefix, suffix = object_id, delimiter: "-"
|
8
|
+
set_temporary_name "#{prefix}#{delimiter}#{suffix}"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/refinements/pathname.rb
CHANGED
@@ -53,14 +53,14 @@ module Refinements
|
|
53
53
|
def delete_suffix(pattern) = parent.join %(#{name.sub(/#{pattern}\z/, "")}#{extname})
|
54
54
|
|
55
55
|
def directories pattern = "*", flag: File::FNM_SYSCASE
|
56
|
-
glob(pattern, flag).select(&:directory?)
|
56
|
+
glob(pattern, flag).select(&:directory?)
|
57
57
|
end
|
58
58
|
|
59
59
|
def empty = file? ? (truncate(0) and self) : remove_tree.make_dir
|
60
60
|
|
61
61
|
def extensions = basename.to_s.split(/(?=\.)+/).tap(&:shift)
|
62
62
|
|
63
|
-
def files(pattern = "*", flag: File::FNM_SYSCASE) = glob(pattern, flag).select(&:file?)
|
63
|
+
def files(pattern = "*", flag: File::FNM_SYSCASE) = glob(pattern, flag).select(&:file?)
|
64
64
|
|
65
65
|
def gsub(pattern, replacement) = self.class.new(to_s.gsub(pattern, replacement))
|
66
66
|
|
data/lib/refinements/struct.rb
CHANGED
@@ -6,7 +6,12 @@ module Refinements
|
|
6
6
|
# Provides additional enhancements to the Struct primitive.
|
7
7
|
module Struct
|
8
8
|
refine ::Struct.singleton_class do
|
9
|
-
def with_positions(*values)
|
9
|
+
def with_positions(*values)
|
10
|
+
warn "`#{self.class}##{__method__}` is deprecated and will be removed in Version 13.0.0.",
|
11
|
+
category: :deprecated
|
12
|
+
|
13
|
+
keyword_init? ? new(**members.zip(values).to_h) : new(*values)
|
14
|
+
end
|
10
15
|
end
|
11
16
|
|
12
17
|
refine ::Struct do
|
data/lib/refinements.rb
CHANGED
data/refinements.gemspec
CHANGED
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: 12.
|
4
|
+
version: 12.10.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: 2024-
|
38
|
+
date: 2024-10-22 00:00:00.000000000 Z
|
39
39
|
dependencies: []
|
40
40
|
description:
|
41
41
|
email:
|
@@ -57,6 +57,7 @@ files:
|
|
57
57
|
- lib/refinements/io.rb
|
58
58
|
- lib/refinements/log_device.rb
|
59
59
|
- lib/refinements/logger.rb
|
60
|
+
- lib/refinements/module.rb
|
60
61
|
- lib/refinements/object.rb
|
61
62
|
- lib/refinements/pathname.rb
|
62
63
|
- lib/refinements/shared/diff.rb
|
@@ -96,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
97
|
- !ruby/object:Gem::Version
|
97
98
|
version: '0'
|
98
99
|
requirements: []
|
99
|
-
rubygems_version: 3.5.
|
100
|
+
rubygems_version: 3.5.22
|
100
101
|
signing_key:
|
101
102
|
specification_version: 4
|
102
103
|
summary: A collection of core object refinements.
|
metadata.gz.sig
CHANGED
Binary file
|