refinements 9.0.2 → 9.1.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: ad550aa0de5b7885133c4a146ff83f8df49184e10fdafc36c57a32511d4ca4e3
4
- data.tar.gz: 52279fbf8e921a3500b5b341bd421b7827ad37f5f5cd0c0976042b33a2332885
3
+ metadata.gz: 4d3e2ba6964cfd39388a7f055fa830a5654ee264d04b2f519c1a735b922c08a7
4
+ data.tar.gz: f711604bba6cd3809398a5a2f6cb0ec867851538e58355c403a2b26aac4d75e3
5
5
  SHA512:
6
- metadata.gz: '095faf7714a553a86654fbb302ebaebe964ba8c291f5e6bf4dc010a2f96ae74ddf2b4bc2fd3a537eb4f76ae38d8bfdc1f06d9a70ac62b039d8437cdbe6fddd0d'
7
- data.tar.gz: d642f75675fa88cf809ea086e4965b43d091c90ff75f11b0d5878ba2f4e900ef427c849b18345b6f1804729a91e20d805ce67223c334fe4af44f1c104b34026e
6
+ metadata.gz: a72da7caf3a0698ae29bce0c51e3ce66829c41ac471e90a0f8777dc0f2e587c79304daf5ffb98983bf6ac50c79afb621d570216e557e550d0771208a039a52ed
7
+ data.tar.gz: ad031fb50b8d0c747d69b856d6e3a41eea74ba7de959bb8bfeda6b17c5b866ef2480fadce96ba50baa9b66c8e2fa6469d82e0a115cfa09a0d645668c2b3a0ef3
checksums.yaml.gz.sig CHANGED
Binary file
data/README.adoc CHANGED
@@ -183,6 +183,19 @@ Inserts additional elements or array between all members of given array.
183
183
  [1, 2, 3].intersperse %i[a b c] # [1, :a, :b, :c, 2, :a, :b, :c, 3]
184
184
  ----
185
185
 
186
+ ===== #many?
187
+
188
+ Answers true if an array has more than one element. Can take a block which evaluates as truthy or
189
+ falsey.
190
+
191
+ [source,ruby]
192
+ ----
193
+ [1, 2].many? # true
194
+ [1, 2, 3].many?(&:odd?) # true
195
+ [1].many? # false
196
+ [].many? # false
197
+ ----
198
+
186
199
  ===== #maximum
187
200
 
188
201
  Answers the maximum extracted value from a collection of objects.
@@ -436,6 +449,19 @@ example.flatten_keys! # {a_b: 1}
436
449
  example # {a_b: 1}
437
450
  ----
438
451
 
452
+ ===== #many?
453
+
454
+ Answers true if a hash has more than one element. Can take a block which evaluates as truthy or
455
+ falsey.
456
+
457
+ [source,ruby]
458
+ ----
459
+ {a: 1, b: 2}.many? # true
460
+ {a: 1, b: 2, c: 2}.many? { |_key, value| value == 2 } # true
461
+ {a: 1}.many? # false
462
+ {}.many? # false
463
+ ----
464
+
439
465
  ===== #recurse
440
466
 
441
467
  Recursively iterates over the hash and any hash value by applying the given block to it. Does not
@@ -694,6 +720,28 @@ Pathname("/example.txt").touch.delete # Pathname("/example")
694
720
  Pathname("/example.txt").delete # Errno::ENOENT
695
721
  ----
696
722
 
723
+ ===== #delete_prefix
724
+
725
+ Deletes a path prefix and answers new pathname.
726
+
727
+ [source,ruby]
728
+ ----
729
+ Pathname("a/path/example-test.rb").delete_prefix("example-") # Pathname("a/path/test.rb")
730
+ Pathname("example-test.rb").delete_prefix("example-") # Pathname("test.rb")
731
+ Pathname("example-test.rb").delete_prefix("miss") # Pathname("example-test.rb")
732
+ ----
733
+
734
+ ===== #delete_suffix
735
+
736
+ Deletes a path suffix and answers new pathname.
737
+
738
+ [source,ruby]
739
+ ----
740
+ Pathname("a/path/test-example.rb").delete_suffix("-example") # Pathname("a/path/test.rb")
741
+ Pathname("test-example.rb").delete_suffix("-example") # Pathname("test.rb")
742
+ Pathname("test-example.rb").delete_suffix("miss") # Pathname("test-example.rb")
743
+ ----
744
+
697
745
  ===== #directories
698
746
 
699
747
  Answers all directories or filtered directories for current path.
@@ -1055,14 +1103,14 @@ whether the struct was constructed with positional or keyword arguments.
1055
1103
  [source,ruby]
1056
1104
  ----
1057
1105
  Example = Struct.new :a, :b, :c
1058
- Example.with_keywords a: 1, b: 2, c: 3 # "#<struct a=1, b=2, c=3>"
1059
- Example.with_keywords a: 1 # "#<struct a=1, b=nil, c=nil>"
1060
- Example.with_keywords c: 1 # "#<struct a=nil, b=nil, c=1>"
1106
+ Example.with_keywords a: 1, b: 2, c: 3 # #<struct a=1, b=2, c=3>
1107
+ Example.with_keywords a: 1 # #<struct a=1, b=nil, c=nil>
1108
+ Example.with_keywords c: 1 # #<struct a=nil, b=nil, c=1>
1061
1109
 
1062
1110
  Example = Struct.new :a, :b, :c, keyword_init: true
1063
- Example.with_keywords a: 1, b: 2, c: 3 # "#<struct a=1, b=2, c=3>"
1064
- Example.with_keywords a: 1 # "#<struct a=1, b=nil, c=nil>"
1065
- Example.with_keywords c: 1 # "#<struct a=nil, b=nil, c=1>"
1111
+ Example.with_keywords a: 1, b: 2, c: 3 # #<struct a=1, b=2, c=3>
1112
+ Example.with_keywords a: 1 # #<struct a=1, b=nil, c=nil>
1113
+ Example.with_keywords c: 1 # #<struct a=nil, b=nil, c=1>
1066
1114
  ----
1067
1115
 
1068
1116
  ===== .with_positions
@@ -1073,106 +1121,125 @@ whether the struct was constructed with positional or keyword arguments.
1073
1121
  [source,ruby]
1074
1122
  ----
1075
1123
  Example = Struct.new :a, :b, :c
1076
- Example.with_positions 1, 2, 3 # "#<struct a=1, b=2, c=3>"
1077
- Example.with_positions 1 # "#<struct a=1, b=nil, c=nil>"
1124
+ Example.with_positions 1, 2, 3 # #<struct a=1, b=2, c=3>
1125
+ Example.with_positions 1 # #<struct a=1, b=nil, c=nil>
1078
1126
 
1079
1127
  Example = Struct.new :a, :b, :c, keyword_init: true
1080
- Example.with_positions 1, 2, 3 # "#<struct a=1, b=2, c=3>"
1081
- Example.with_positions 1 # "#<struct a=1, b=nil, c=nil>"
1128
+ Example.with_positions 1, 2, 3 # #<struct a=1, b=2, c=3>
1129
+ Example.with_positions 1 # #<struct a=1, b=nil, c=nil>
1082
1130
  ----
1083
1131
 
1084
1132
  ===== #merge
1085
1133
 
1086
1134
  Merges multiple attributes without mutating itself and supports any object that responds to `#to_h`.
1135
+ Works regardless of whether the struct is constructed with positional or keyword arguments.
1087
1136
 
1088
1137
  [source,ruby]
1089
1138
  ----
1139
+ example = Struct.new("Example", :a, :b, :c).new 1, 2, 3
1090
1140
  other = Struct.new("Other", :a, :b, :c).new 7, 8, 9
1091
1141
 
1092
- example = Struct.new(:a, :b, :c).new 1, 2, 3
1093
- example.merge a: 10 # "#<struct a=10, b=2, c=3>"
1094
- example.merge a: 10, c: 30 # "#<struct a=10, b=2, c=30>"
1095
- example.merge a: 10, b: 20, c: 30 # "#<struct a=10, b=20, c=30>"
1096
- example.merge {a: 10, b: 20} # "#<struct a=10, b=20, c=3>"
1097
- example.merge other # "#<struct a=7, b=8, c=9>"
1098
- example # "#<struct a=1, b=2, c=3>"
1099
-
1100
- example = Struct.new(:a, :b, :c, keyword_init: true).new a: 1, b: 2, c: 3
1101
- example.merge a: 10 # "#<struct a=10, b=2, c=3>"
1102
- example.merge a: 10, c: 30 # "#<struct a=10, b=2, c=30>"
1103
- example.merge {a: 10, b: 20} # "#<struct a=10, b=20, c=3>"
1104
- example.merge other # "#<struct a=7, b=8, c=9>"
1105
- example.merge a: 10, b: 20, c: 30 # "#<struct a=10, b=20, c=30>"
1106
- example # "#<struct a=1, b=2, c=3>"
1142
+ example.merge a: 10 # #<struct Struct::Example a=10, b=2, c=3>
1143
+ example.merge a: 10, c: 30 # #<struct Struct::Example a=10, b=2, c=30>
1144
+ example.merge a: 10, b: 20, c: 30 # #<struct Struct::Example a=10, b=20, c=30>
1145
+ example.merge other # #<struct Struct::Example a=7, b=8, c=9>
1146
+ example # #<struct Struct::Example a=1, b=2, c=3>
1107
1147
  ----
1108
1148
 
1109
1149
  ===== #merge!
1110
1150
 
1111
1151
  Merges multiple attributes while mutating itself and supports any object that responds to `#to_h`.
1152
+ Works regardless of whether the struct is constructed with positional or keyword arguments.
1112
1153
 
1113
1154
  [source,ruby]
1114
1155
  ----
1156
+ example = Struct.new("Example", :a, :b, :c).new 1, 2, 3
1115
1157
  other = Struct.new("Other", :a, :b, :c).new 7, 8, 9
1116
1158
 
1117
- example = Struct.new(:a, :b, :c).new 1, 2, 3
1118
- example.merge! a: 10 # "#<struct a=10, b=2, c=3>"
1119
- example.merge! a: 10, c: 30 # "#<struct a=10, b=2, c=30>"
1120
- example.merge! {a: 10, b: 20} # "#<struct a=10, b=20, c=3>"
1121
- example.merge! other # "#<struct a=7, b=8, c=9>"
1122
- example.merge! a: 10, b: 20, c: 30 # "#<struct a=10, b=20, c=30>"
1123
- example # "#<struct a=10, b=20, c=30>"
1124
-
1125
- example = Struct.new(:a, :b, :c, keyword_init: true).new a: 1, b: 2, c: 3
1126
- example.merge! a: 10 # "#<struct a=10, b=2, c=3>"
1127
- example.merge! a: 10, c: 30 # "#<struct a=10, b=2, c=30>"
1128
- example.merge! {a: 10, b: 20} # "#<struct a=10, b=20, c=3>"
1129
- example.merge! other # "#<struct a=7, b=8, c=9>"
1130
- example.merge! a: 10, b: 20, c: 30 # "#<struct a=10, b=20, c=30>"
1131
- example # "#<struct a=10, b=20, c=30>"
1159
+ example.merge! a: 10 # #<struct Struct::Example a=10, b=2, c=3>
1160
+ example.merge! a: 10, c: 30 # #<struct Struct::Example a=10, b=2, c=30>
1161
+ example.merge! other # #<struct Struct::Example a=7, b=8, c=9>
1162
+ example.merge! a: 10, b: 20, c: 30 # #<struct Struct::Example a=10, b=20, c=30>
1163
+ example # #<struct Struct::Example a=10, b=20, c=30>
1132
1164
  ----
1133
1165
 
1134
1166
  ===== #revalue
1135
1167
 
1136
- Transforms values without mutating itself. An optional hash can be supplied to pinpoint and
1137
- transform specific attributes. In the event that a block isn't supplied, the struct will answer
1138
- itself since there is nothing to operate on. Behavior is the same regardless of whether the struct
1139
- is constructed using positional or keyword arguments. A positional struct is used in the examples
1140
- below but a keyword struct would work too.
1168
+ Transforms values without mutating itself. An optional hash can be supplied to target specific
1169
+ attributes. In the event that a block isn't supplied, the struct will answer itself since there is
1170
+ nothing to operate on. Behavior is the same regardless of whether the struct is constructed using
1171
+ positional or keyword arguments. Works regardless of whether the struct is constructed with
1172
+ positional or keyword arguments.
1141
1173
 
1142
1174
  [source,ruby]
1143
1175
  ----
1144
1176
  example = Struct.new("Example", :a, :b, :c).new 1, 2, 3
1145
- example.revalue { |value| value * 2 } # "#<struct a=2, b=4, c=6>"
1146
- example.revalue(c: 2) { |previous, current| previous + current } # "#<struct a=1, b=2, c=5>"
1147
- example.revalue c: 2 # "#<struct a=1, b=2, c=3>"
1148
- example.revalue # "#<struct a=1, b=2, c=3>"
1149
- example # "#<struct a=1, b=2, c=3>"
1177
+
1178
+ example.revalue { |value| value * 2 } # #<struct Struct::Example a=2, b=4, c=6>
1179
+ example.revalue(c: 2) { |previous, current| previous + current } # #<struct Struct::Example a=1, b=2, c=5>
1180
+ example.revalue c: 2 # #<struct Struct::Example a=1, b=2, c=3>
1181
+ example.revalue # #<struct Struct::Example a=1, b=2, c=3>
1182
+ example # #<struct Struct::Example a=1, b=2, c=3>
1150
1183
  ----
1151
1184
 
1152
1185
  ===== #revalue!
1153
1186
 
1154
- Transforms values while mutating itself. An optional hash can be supplied to pinpoint and transform
1155
- specific attributes. In the event that a block isn't supplied, the struct will answer itself since
1156
- there is nothing to operate on. Behavior is the same regardless of whether the struct is constructed
1157
- using positional or keyword arguments. A positional struct is used in the examples below but a
1158
- keyword struct would work too.
1187
+ Transforms values while mutating itself. An optional hash can be supplied to target specific
1188
+ attributes. In the event that a block isn't supplied, the struct will answer itself since there is
1189
+ nothing to operate on. Behavior is the same regardless of whether the struct is constructed using
1190
+ positional or keyword arguments. Works regardless of whether the struct is constructed with
1191
+ positional or keyword arguments.
1159
1192
 
1160
1193
  [source,ruby]
1161
1194
  ----
1162
- Example = Struct.new :a, :b, :c
1195
+ one = Struct.new("One", :a, :b, :c).new 1, 2, 3
1196
+ one.revalue! { |value| value * 2 } # #<struct Struct::One a=2, b=4, c=6>
1197
+ one # #<struct Struct::One a=2, b=4, c=6>
1198
+
1199
+ two = Struct.new("Two", :a, :b, :c).new 1, 2, 3
1200
+ two.revalue!(c: 2) { |previous, current| previous + current } # #<struct Struct::Two a=1, b=2, c=5>
1201
+ two # #<struct Struct::Two a=1, b=2, c=5>
1202
+
1203
+ three = Struct.new("Three", :a, :b, :c).new 1, 2, 3
1204
+ three.revalue! c: 2 # #<struct Struct::Three a=1, b=2, c=3>
1205
+ three.revalue! # #<struct Struct::Three a=1, b=2, c=3>
1206
+ three # #<struct Struct::Three a=1, b=2, c=3>
1207
+ ----
1163
1208
 
1164
- example = Example[1, 2, 3]
1165
- example.revalue! { |value| value * 2 } # "#<struct a=2, b=4, c=6>"
1166
- example # "#<struct a=2, b=4, c=6>"
1209
+ ===== #transmute
1167
1210
 
1168
- example = Example[1, 2, 3]
1169
- example.revalue!(c: 2) { |previous, current| previous + current } # "#<struct a=1, b=2, c=5>"
1170
- example # "#<struct a=1, b=2, c=5>"
1211
+ Transmutes given enumerable by using the foreign key map and merging those key values into the
1212
+ current struct while not mutating itself. Works regardless of whether the struct is constructed with
1213
+ positional or keyword arguments.
1214
+
1215
+ [source,ruby]
1216
+ ----
1217
+ a = Struct.new("A", :a, :b, :c).new 1, 2, 3
1218
+ b = Struct.new("B", :x, :y, :z).new 7, 8, 9
1219
+ c = {r: 10, s: 20, t: 30}
1220
+
1221
+ a.transmute b, a: :x, b: :y, c: :z # #<struct Struct::A a=7, b=8, c=9>
1222
+ a.transmute b, b: :y # #<struct Struct::A a=1, b=8, c=3>
1223
+ a.transmute c, c: :t # #<struct Struct::A a=1, b=2, c=30>
1224
+ a # #<struct Struct::A a=1, b=2, c=3>
1225
+ ----
1226
+
1227
+ ===== #transmute!
1228
+
1229
+ Transmutes given enumerable by using the foreign key map and merging those key values into the
1230
+ current struct while mutating itself. Works regardless of whether the struct is constructed with
1231
+ positional or keyword arguments.
1232
+
1233
+ [source,ruby]
1234
+ ----
1235
+ a = Struct.new("A", :a, :b, :c).new 1, 2, 3
1236
+ b = Struct.new("B", :x, :y, :z).new 7, 8, 9
1237
+ c = {r: 10, s: 20, t: 30}
1171
1238
 
1172
- example = Example[1, 2, 3]
1173
- example.revalue! c: 2 # "#<struct a=1, b=2, c=3>"
1174
- example.revalue! # "#<struct a=1, b=2, c=3>"
1175
- example # "#<struct a=1, b=2, c=3>"
1239
+ a.transmute! b, a: :x, b: :y, c: :z # #<struct Struct::A a=7, b=8, c=9>
1240
+ a.transmute! b, b: :y # #<struct Struct::A a=1, b=8, c=3>
1241
+ a.transmute! c, c: :t # #<struct Struct::A a=1, b=2, c=30>
1242
+ a # #<struct Struct::A a=7, b=8, c=30>
1176
1243
  ----
1177
1244
 
1178
1245
  ==== Symbol
@@ -1,9 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "refinements/shared/enumerables/many"
4
+
3
5
  module Refinements
4
6
  # Provides additional enhancements to the Array primitive.
5
7
  module Arrays
6
8
  refine Array do
9
+ import_methods Shared::Enumerables::Many
10
+
7
11
  def compress = dup.compress!
8
12
 
9
13
  def compress!
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "refinements/shared/enumerables/many"
4
+
3
5
  module Refinements
4
6
  # Provides additional enhancements to the Hash primitive.
5
7
  module Hashes
@@ -12,6 +14,8 @@ module Refinements
12
14
  end
13
15
 
14
16
  refine Hash do
17
+ import_methods Shared::Enumerables::Many
18
+
15
19
  def compress = dup.compress!
16
20
 
17
21
  def compress!
@@ -5,7 +5,7 @@ module Refinements
5
5
  module Identity
6
6
  NAME = "refinements"
7
7
  LABEL = "Refinements"
8
- VERSION = "9.0.2"
8
+ VERSION = "9.1.0"
9
9
  VERSION_LABEL = "#{LABEL} #{VERSION}".freeze
10
10
  end
11
11
  end
@@ -50,6 +50,10 @@ module Refinements
50
50
 
51
51
  def delete = super && self
52
52
 
53
+ def delete_prefix(pattern) = parent.join %(#{name.sub(/\A#{pattern}/, "")}#{extname})
54
+
55
+ def delete_suffix(pattern) = parent.join %(#{name.sub(/#{pattern}\z/, "")}#{extname})
56
+
53
57
  def directories pattern = "*", flag: File::FNM_SYSCASE
54
58
  glob(pattern, flag).select(&:directory?).sort
55
59
  end
@@ -74,7 +78,7 @@ module Refinements
74
78
  self
75
79
  end
76
80
 
77
- def name = basename(extname)
81
+ def name = basename extname
78
82
 
79
83
  def relative_parent(root_dir) = relative_path_from(root_dir).parent
80
84
 
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Refinements
4
+ module Shared
5
+ module Enumerables
6
+ # Provides shared functionality for knowing whether an enumerable has many elements or not.
7
+ module Many
8
+ def many?
9
+ return size > 1 unless block_given?
10
+
11
+ total = reduce(0) { |count, item| yield(item) ? count + 1 : count }
12
+ total > 1
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -64,11 +64,10 @@ module Refinements
64
64
 
65
65
  private
66
66
 
67
- # :reek:DuplicateMethodCall
68
67
  # :reek:UtilityFunction
69
68
  def combine parts, method, delimiter = ""
70
69
  parts.reduce "" do |result, part|
71
- next part.__send__ method if result.empty?
70
+ next part.public_send method if result.empty?
72
71
 
73
72
  "#{result}#{delimiter}#{part.__send__ method}"
74
73
  end
@@ -15,10 +15,10 @@ module Refinements
15
15
  end
16
16
 
17
17
  refine Struct do
18
- def merge(object = nil) = dup.merge!(object)
18
+ def merge(...) = dup.merge!(...)
19
19
 
20
20
  def merge! object = nil
21
- to_h.merge(**object.to_h).each { |key, value| self[key] = value }
21
+ to_h.merge!(**object.to_h).each { |key, value| self[key] = value }
22
22
  self
23
23
  end
24
24
 
@@ -34,6 +34,13 @@ module Refinements
34
34
  attributes.each { |key, value| self[key] = yield self[key], value }
35
35
  self
36
36
  end
37
+
38
+ def transmute(...) = dup.transmute!(...)
39
+
40
+ def transmute! object, **key_map
41
+ mapping = key_map.invert
42
+ merge! object.to_h.slice(*mapping.keys).transform_keys!(mapping)
43
+ end
37
44
  end
38
45
  end
39
46
  end
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: 9.0.2
4
+ version: 9.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brooke Kuhlmann
@@ -28,7 +28,7 @@ cert_chain:
28
28
  lkHilIrX69jq8wMPpBhlaw2mRmeSL50Wv5u6xVBvOHhXFSP1crXM95vfLhLyRYod
29
29
  W2A=
30
30
  -----END CERTIFICATE-----
31
- date: 2022-01-11 00:00:00.000000000 Z
31
+ date: 2022-01-17 00:00:00.000000000 Z
32
32
  dependencies: []
33
33
  description:
34
34
  email:
@@ -49,6 +49,7 @@ files:
49
49
  - lib/refinements/identity.rb
50
50
  - lib/refinements/ios.rb
51
51
  - lib/refinements/pathnames.rb
52
+ - lib/refinements/shared/enumerables/many.rb
52
53
  - lib/refinements/string_ios.rb
53
54
  - lib/refinements/strings.rb
54
55
  - lib/refinements/structs.rb
@@ -77,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
78
  - !ruby/object:Gem::Version
78
79
  version: '0'
79
80
  requirements: []
80
- rubygems_version: 3.3.4
81
+ rubygems_version: 3.3.5
81
82
  signing_key:
82
83
  specification_version: 4
83
84
  summary: A collection of refinements to core Ruby objects.
metadata.gz.sig CHANGED
Binary file