refinements 9.0.0 → 9.2.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: 0746c18e226511e85f7d04c3f4cb6625ce9f5e693bf83a0cf305c2e21d4c4abe
4
- data.tar.gz: 1684d702a617bd9ab4b9303bc8fdf8b0076c4ecb8d65c124a9a7442246bd6451
3
+ metadata.gz: aac49ade733ea3bd5f36bd51d47c805c50dd6841ee7f8f24102336ff67bc8d08
4
+ data.tar.gz: 8d8f3d7edcc8289b90ba632f71970a64f22c41309c14eb68f555312d5516d320
5
5
  SHA512:
6
- metadata.gz: 19634c8df6dbc346faba14e88d13aabcdd257d0ecefb9ace0fc7e9a86f95be545d523a5e1c80caad5535310e4d035404e802e24aa9e3dc896430694540b90143
7
- data.tar.gz: bc750c9a7659337bfb7e2bf971a78bbd764bc9981eda9325f993ed5f06ffdccf93817b37732763a331b91f179a855c6a2212d448740a4d878c1a1ee7e38b6804
6
+ metadata.gz: 82dcc0b789e630281874f9d73e891927231066caa2c0255e3f3d63242edae1e32cc0eca709e76b6f82a85c13be92852e9e59e6a2b47b4802f6913235e38c6a0a
7
+ data.tar.gz: d17b6d4ee10c5568f52b6eee57e97538b1af910ff7241fc8faca30079dd82168da472dbe6c6d1d6f9f015f1887a53e85ced855173c293bcb6f9efb56cfc454f4
checksums.yaml.gz.sig CHANGED
Binary file
data/README.adoc CHANGED
@@ -11,12 +11,11 @@ image::https://img.shields.io/badge/code_style-alchemists-brightgreen.svg[Alchem
11
11
  [link=https://circleci.com/gh/bkuhlmann/refinements]
12
12
  image::https://circleci.com/gh/bkuhlmann/refinements.svg?style=svg[Circle CI Status]
13
13
 
14
- Refinements are a collection of enhancements to primitive Ruby objects without needing to resort to
15
- painful and hard to debug
16
- link:https://www.alchemists.io/articles/ruby_antipatterns/#_monkey_patches[monkey patches]. These
17
- refinements give you additional syntactic sugar to develop clean and concise implementations while
18
- using less code. By refining our code we can acquire the functionality we wish the core primitives
19
- had!
14
+ Refinements are a collection primitive Ruby objects enhancements without needing to resort to hard
15
+ to debug link:https://www.alchemists.io/articles/ruby_antipatterns/#_monkey_patches[monkey patches].
16
+ These refinements give you additional syntactic sugar to develop clean and concise implementations
17
+ while using less code. By refining our code we can acquire the functionality we wish the core
18
+ primitives had!
20
19
 
21
20
  toc::[]
22
21
 
@@ -148,7 +147,7 @@ Removes given array or elements without mutating itself.
148
147
 
149
148
  ===== #filter_find
150
149
 
151
- Answers the first truthy and filtered result from a collection.
150
+ Answers the first element which evaluates to true from a filtered collection.
152
151
 
153
152
  [source,ruby]
154
153
  ----
@@ -184,6 +183,19 @@ Inserts additional elements or array between all members of given array.
184
183
  [1, 2, 3].intersperse %i[a b c] # [1, :a, :b, :c, 2, :a, :b, :c, 3]
185
184
  ----
186
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
+
187
199
  ===== #maximum
188
200
 
189
201
  Answers the maximum extracted value from a collection of objects.
@@ -349,7 +361,7 @@ example # {a: "A", b: {one: 1, two: "Two"}}
349
361
 
350
362
  ===== #deep_stringify_keys
351
363
 
352
- Stringifies keys of nested hash without mutating itself. Does not handle nested arrays, though.
364
+ Answers string keys of a nested hash without mutating itself. Does not handle nested arrays, though.
353
365
 
354
366
  [source,ruby]
355
367
  ----
@@ -360,7 +372,7 @@ example # {a: {b: 2}}
360
372
 
361
373
  ===== #deep_stringify_keys!
362
374
 
363
- Stringifies keys of nested hash while mutating itself. Does not handle nested arrays, though.
375
+ Answers string keys of nested hash while mutating itself. Does not handle nested arrays, though.
364
376
 
365
377
  [source,ruby]
366
378
  ----
@@ -437,6 +449,19 @@ example.flatten_keys! # {a_b: 1}
437
449
  example # {a_b: 1}
438
450
  ----
439
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
+
440
465
  ===== #recurse
441
466
 
442
467
  Recursively iterates over the hash and any hash value by applying the given block to it. Does not
@@ -695,6 +720,28 @@ Pathname("/example.txt").touch.delete # Pathname("/example")
695
720
  Pathname("/example.txt").delete # Errno::ENOENT
696
721
  ----
697
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
+
698
745
  ===== #directories
699
746
 
700
747
  Answers all directories or filtered directories for current path.
@@ -827,14 +874,14 @@ parent_path = Pathname "/one"
827
874
  child_path = parent_path.join "two"
828
875
 
829
876
  child_path.make_path
830
- child_path.remove_tree # Pathname "/one/two"
877
+ parent_path.remove_tree # Pathname "/one"
831
878
  child_path.exist? # false
832
- paremt_path.exist? # true
879
+ parent_path.exist? # false
833
880
 
834
881
  child_path.make_path
835
- parent_path.remove_tree # Pathname "/one"
882
+ child_path.remove_tree # Pathname "/one/two"
836
883
  child_path.exist? # false
837
- parent_path.exist? # false
884
+ parent_path.exist? # true
838
885
  ----
839
886
 
840
887
  ===== #rewrite
@@ -886,7 +933,7 @@ Answers `true`/`false` based on whether string is blank, `<space>`, `\n`, `\t`,
886
933
 
887
934
  ===== #camelcase
888
935
 
889
- Answers a camelcased string.
936
+ Answers a camel cased string.
890
937
 
891
938
  [source,ruby]
892
939
  ----
@@ -895,7 +942,7 @@ Answers a camelcased string.
895
942
 
896
943
  ===== #down
897
944
 
898
- Answers string with only first letter downcased.
945
+ Answers string with only first letter down cased.
899
946
 
900
947
  [source,ruby]
901
948
  ----
@@ -977,7 +1024,7 @@ well in other languages.
977
1024
 
978
1025
  ===== #snakecase
979
1026
 
980
- Answers a snakecased string.
1027
+ Answers a snake cased string.
981
1028
 
982
1029
  [source,ruby]
983
1030
  ----
@@ -986,7 +1033,7 @@ Answers a snakecased string.
986
1033
 
987
1034
  ===== #titleize
988
1035
 
989
- Answers titleized string.
1036
+ Answers a title string with proper capitalization of each word.
990
1037
 
991
1038
  [source,ruby]
992
1039
  ----
@@ -1008,7 +1055,7 @@ Answers string as a boolean.
1008
1055
 
1009
1056
  ===== #up
1010
1057
 
1011
- Answers string with only first letter upcased.
1058
+ Answers string with only first letter capitalized.
1012
1059
 
1013
1060
  [source,ruby]
1014
1061
  ----
@@ -1038,6 +1085,8 @@ buffer # "This is a test."
1038
1085
 
1039
1086
  ===== .keyworded?
1040
1087
 
1088
+ ⚠️ Will be removed in the next major version. Use `.keyword_init?` instead.
1089
+
1041
1090
  Answers whether a struct was constructed with keyword or positional arguments.
1042
1091
 
1043
1092
  [source,ruby]
@@ -1054,14 +1103,14 @@ whether the struct was constructed with positional or keyword arguments.
1054
1103
  [source,ruby]
1055
1104
  ----
1056
1105
  Example = Struct.new :a, :b, :c
1057
- Example.with_keywords a: 1, b: 2, c: 3 # "#<struct a=1, b=2, c=3>"
1058
- Example.with_keywords a: 1 # "#<struct a=1, b=nil, c=nil>"
1059
- 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>
1060
1109
 
1061
1110
  Example = Struct.new :a, :b, :c, keyword_init: true
1062
- Example.with_keywords a: 1, b: 2, c: 3 # "#<struct a=1, b=2, c=3>"
1063
- Example.with_keywords a: 1 # "#<struct a=1, b=nil, c=nil>"
1064
- 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>
1065
1114
  ----
1066
1115
 
1067
1116
  ===== .with_positions
@@ -1072,106 +1121,125 @@ whether the struct was constructed with positional or keyword arguments.
1072
1121
  [source,ruby]
1073
1122
  ----
1074
1123
  Example = Struct.new :a, :b, :c
1075
- Example.with_positions 1, 2, 3 # "#<struct a=1, b=2, c=3>"
1076
- 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>
1077
1126
 
1078
1127
  Example = Struct.new :a, :b, :c, keyword_init: true
1079
- Example.with_positions 1, 2, 3 # "#<struct a=1, b=2, c=3>"
1080
- 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>
1081
1130
  ----
1082
1131
 
1083
1132
  ===== #merge
1084
1133
 
1085
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.
1086
1136
 
1087
1137
  [source,ruby]
1088
1138
  ----
1139
+ example = Struct.new("Example", :a, :b, :c).new 1, 2, 3
1089
1140
  other = Struct.new("Other", :a, :b, :c).new 7, 8, 9
1090
1141
 
1091
- example = Struct.new(:a, :b, :c).new 1, 2, 3
1092
- example.merge a: 10 # "#<struct a=10, b=2, c=3>"
1093
- example.merge a: 10, c: 30 # "#<struct a=10, b=2, c=30>"
1094
- example.merge a: 10, b: 20, c: 30 # "#<struct a=10, b=20, c=30>"
1095
- example.merge {a: 10, b: 20} # "#<struct a=10, b=20, c=3>"
1096
- example.merge other # "#<struct a=7, b=8, c=9>"
1097
- example # "#<struct a=1, b=2, c=3>"
1098
-
1099
- example = Struct.new(:a, :b, :c, keyword_init: true).new a: 1, b: 2, c: 3
1100
- example.merge a: 10 # "#<struct a=10, b=2, c=3>"
1101
- example.merge a: 10, c: 30 # "#<struct a=10, b=2, c=30>"
1102
- example.merge {a: 10, b: 20} # "#<struct a=10, b=20, c=3>"
1103
- example.merge other # "#<struct a=7, b=8, c=9>"
1104
- example.merge a: 10, b: 20, c: 30 # "#<struct a=10, b=20, c=30>"
1105
- 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>
1106
1147
  ----
1107
1148
 
1108
1149
  ===== #merge!
1109
1150
 
1110
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.
1111
1153
 
1112
1154
  [source,ruby]
1113
1155
  ----
1156
+ example = Struct.new("Example", :a, :b, :c).new 1, 2, 3
1114
1157
  other = Struct.new("Other", :a, :b, :c).new 7, 8, 9
1115
1158
 
1116
- example = Struct.new(:a, :b, :c).new 1, 2, 3
1117
- example.merge! a: 10 # "#<struct a=10, b=2, c=3>"
1118
- example.merge! a: 10, c: 30 # "#<struct a=10, b=2, c=30>"
1119
- example.merge! {a: 10, b: 20} # "#<struct a=10, b=20, c=3>"
1120
- example.merge! other # "#<struct a=7, b=8, c=9>"
1121
- example.merge! a: 10, b: 20, c: 30 # "#<struct a=10, b=20, c=30>"
1122
- example # "#<struct a=10, b=20, c=30>"
1123
-
1124
- example = Struct.new(:a, :b, :c, keyword_init: true).new a: 1, b: 2, c: 3
1125
- example.merge! a: 10 # "#<struct a=10, b=2, c=3>"
1126
- example.merge! a: 10, c: 30 # "#<struct a=10, b=2, c=30>"
1127
- example.merge! {a: 10, b: 20} # "#<struct a=10, b=20, c=3>"
1128
- example.merge! other # "#<struct a=7, b=8, c=9>"
1129
- example.merge! a: 10, b: 20, c: 30 # "#<struct a=10, b=20, c=30>"
1130
- 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>
1131
1164
  ----
1132
1165
 
1133
1166
  ===== #revalue
1134
1167
 
1135
- Transforms values without mutating itself. An optional hash can be supplied to pinpoint and
1136
- transform specific attributes. In the event that a block isn't supplied, the struct will answer
1137
- itself since there is nothing to operate on. Behavior is the same regardless of whether the struct
1138
- is constructed using positional or keyword arguments. A positional struct is used in the examples
1139
- 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.
1140
1173
 
1141
1174
  [source,ruby]
1142
1175
  ----
1143
1176
  example = Struct.new("Example", :a, :b, :c).new 1, 2, 3
1144
- example.revalue { |value| value * 2 } # "#<struct a=2, b=4, c=6>"
1145
- example.revalue(c: 2) { |previous, current| previous + current } # "#<struct a=1, b=2, c=5>"
1146
- example.revalue c: 2 # "#<struct a=1, b=2, c=3>"
1147
- example.revalue # "#<struct a=1, b=2, c=3>"
1148
- 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>
1149
1183
  ----
1150
1184
 
1151
1185
  ===== #revalue!
1152
1186
 
1153
- Transforms values while mutating itself. An optional hash can be supplied to pinpoint and transform
1154
- specific attributes. In the event that a block isn't supplied, the struct will answer itself since
1155
- there is nothing to operate on. Behavior is the same regardless of whether the struct is constructed
1156
- using positional or keyword arguments. A positional struct is used in the examples below but a
1157
- 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.
1158
1192
 
1159
1193
  [source,ruby]
1160
1194
  ----
1161
- 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
+ ----
1208
+
1209
+ ===== #transmute
1210
+
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
+ ----
1162
1226
 
1163
- example = Example[1, 2, 3]
1164
- example.revalue! { |value| value * 2 } # "#<struct a=2, b=4, c=6>"
1165
- example # "#<struct a=2, b=4, c=6>"
1227
+ ===== #transmute!
1166
1228
 
1167
- example = Example[1, 2, 3]
1168
- example.revalue!(c: 2) { |previous, current| previous + current } # "#<struct a=1, b=2, c=5>"
1169
- example # "#<struct a=1, b=2, c=5>"
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}
1170
1238
 
1171
- example = Example[1, 2, 3]
1172
- example.revalue! c: 2 # "#<struct a=1, b=2, c=3>"
1173
- example.revalue! # "#<struct a=1, b=2, c=3>"
1174
- 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>
1175
1243
  ----
1176
1244
 
1177
1245
  ==== Symbol
@@ -1219,35 +1287,17 @@ To test, run:
1219
1287
  bundle exec rake
1220
1288
  ----
1221
1289
 
1222
- == Versioning
1223
-
1224
- Read link:https://semver.org[Semantic Versioning] for details. Briefly, it means:
1225
-
1226
- * Major (X.y.z) - Incremented for any backwards incompatible public API changes.
1227
- * Minor (x.Y.z) - Incremented for new, backwards compatible, public API enhancements/fixes.
1228
- * Patch (x.y.Z) - Incremented for small, backwards compatible, bug fixes.
1229
-
1230
- == Code of Conduct
1231
-
1232
- Please note that this project is released with a link:CODE_OF_CONDUCT.adoc[CODE OF CONDUCT]. By
1233
- participating in this project you agree to abide by its terms.
1234
-
1235
- == Contributions
1236
-
1237
- Read link:CONTRIBUTING.adoc[CONTRIBUTING] for details.
1238
-
1239
- == Community
1290
+ == link:https://www.alchemists.io/policies/license[License]
1240
1291
 
1241
- Feel free to link:https://www.alchemists.io/community[join the commmunity] for discussions related
1242
- to this project and much more.
1292
+ == link:https://www.alchemists.io/policies/security[Security]
1243
1293
 
1244
- == License
1294
+ == link:https://www.alchemists.io/policies/code_of_conduct[Code of Conduct]
1245
1295
 
1246
- Read link:LICENSE.adoc[LICENSE] for details.
1296
+ == link:https://www.alchemists.io/policies/contributions[Contributions]
1247
1297
 
1248
- == Changes
1298
+ == link:https://www.alchemists.io/projects/refinements/versions[Versions]
1249
1299
 
1250
- Read link:CHANGES.adoc[CHANGES] for details.
1300
+ == link:https://www.alchemists.io/community[Community]
1251
1301
 
1252
1302
  == Credits
1253
1303
 
@@ -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!
@@ -42,11 +46,9 @@ module Refinements
42
46
 
43
47
  def deep_symbolize_keys! = replace(deep_symbolize_keys)
44
48
 
45
- # rubocop:disable Style/MethodDefParentheses
46
49
  def fetch_value(key, *default_value, &)
47
50
  fetch(key, *default_value, &) || default_value.first
48
51
  end
49
- # rubocop:enable Style/MethodDefParentheses
50
52
 
51
53
  # :reek:TooManyStatements
52
54
  def flatten_keys prefix: nil, delimiter: "_", cast: :to_sym
@@ -17,7 +17,11 @@ module Refinements
17
17
  def home = new(ENV["HOME"])
18
18
 
19
19
  def make_temp_dir prefix: "temp-", suffix: nil, root: nil
20
- Dir.mktmpdir([prefix, suffix], root) { |path| block_given? ? yield(new path) : new(path) }
20
+ if block_given?
21
+ Dir.mktmpdir([prefix, suffix], root) { |path| yield new(path) }
22
+ else
23
+ new Dir.mktmpdir([prefix, suffix], root)
24
+ end
21
25
  end
22
26
 
23
27
  def require_tree root, pattern = "**/*.rb"
@@ -46,6 +50,10 @@ module Refinements
46
50
 
47
51
  def delete = super && self
48
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
+
49
57
  def directories pattern = "*", flag: File::FNM_SYSCASE
50
58
  glob(pattern, flag).select(&:directory?).sort
51
59
  end
@@ -70,7 +78,7 @@ module Refinements
70
78
  self
71
79
  end
72
80
 
73
- def name = basename(extname)
81
+ def name = basename extname
74
82
 
75
83
  def relative_parent(root_dir) = relative_path_from(root_dir).parent
76
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
@@ -4,18 +4,21 @@ module Refinements
4
4
  # Provides additional enhancements to the Struct primitive.
5
5
  module Structs
6
6
  refine Struct.singleton_class do
7
- def keyworded? = inspect.include?("keyword_init: true")
7
+ def keyworded?
8
+ warn "[DEPRECATION]: .keyworded? is deprecated, use .keyword_init? instead."
9
+ inspect.include? "keyword_init: true"
10
+ end
8
11
 
9
- def with_keywords(**arguments) = keyworded? ? new(**arguments) : new.merge!(**arguments)
12
+ def with_keywords(**arguments) = keyword_init? ? new(**arguments) : new.merge!(**arguments)
10
13
 
11
- def with_positions(*values) = keyworded? ? new(**members.zip(values).to_h) : new(*values)
14
+ def with_positions(*values) = keyword_init? ? new(**members.zip(values).to_h) : new(*values)
12
15
  end
13
16
 
14
17
  refine Struct do
15
- def merge(object = nil) = dup.merge!(object)
18
+ def merge(...) = dup.merge!(...)
16
19
 
17
20
  def merge! object = nil
18
- 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 }
19
22
  self
20
23
  end
21
24
 
@@ -31,6 +34,13 @@ module Refinements
31
34
  attributes.each { |key, value| self[key] = yield self[key], value }
32
35
  self
33
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
34
44
  end
35
45
  end
36
46
  end
@@ -4,11 +4,9 @@ module Refinements
4
4
  # Provides additional enhancements to the Symbol primitive.
5
5
  module Symbols
6
6
  refine Symbol do
7
- # rubocop:disable Style/MethodDefParentheses
8
7
  def call(*arguments, &)
9
8
  proc { |receiver| receiver.public_send self, *arguments, & }
10
9
  end
11
- # rubocop:enable Style/MethodDefParentheses
12
10
  end
13
11
  end
14
12
  end
data/lib/refinements.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "refinements/identity"
4
3
  require "refinements/arrays"
5
4
  require "refinements/big_decimals"
6
5
  require "refinements/date_times"
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "refinements"
5
+ spec.version = "9.2.0"
6
+ spec.platform = Gem::Platform::RUBY
7
+ spec.authors = ["Brooke Kuhlmann"]
8
+ spec.email = ["brooke@alchemists.io"]
9
+ spec.homepage = "https://www.alchemists.io/projects/refinements"
10
+ spec.summary = "A collection of refinements to core Ruby objects."
11
+ spec.license = "Hippocratic-3.0"
12
+
13
+ spec.metadata = {
14
+ "bug_tracker_uri" => "https://github.com/bkuhlmann/refinements/issues",
15
+ "changelog_uri" => "https://www.alchemists.io/projects/refinements/versions",
16
+ "documentation_uri" => "https://www.alchemists.io/projects/refinements",
17
+ "label" => "Refinements",
18
+ "rubygems_mfa_required" => "true",
19
+ "source_code_uri" => "https://github.com/bkuhlmann/refinements"
20
+ }
21
+
22
+ spec.signing_key = Gem.default_key_path
23
+ spec.cert_chain = [Gem.default_cert_path]
24
+
25
+ spec.required_ruby_version = "~> 3.1"
26
+
27
+ spec.files = Dir["*.gemspec", "lib/**/*"]
28
+ spec.extra_rdoc_files = Dir["README*", "LICENSE*"]
29
+ spec.require_paths = ["lib"]
30
+ 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.0
4
+ version: 9.2.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: 2021-12-26 00:00:00.000000000 Z
31
+ date: 2022-01-23 00:00:00.000000000 Z
32
32
  dependencies: []
33
33
  description:
34
34
  email:
@@ -46,20 +46,22 @@ files:
46
46
  - lib/refinements/big_decimals.rb
47
47
  - lib/refinements/date_times.rb
48
48
  - lib/refinements/hashes.rb
49
- - lib/refinements/identity.rb
50
49
  - lib/refinements/ios.rb
51
50
  - lib/refinements/pathnames.rb
51
+ - lib/refinements/shared/enumerables/many.rb
52
52
  - lib/refinements/string_ios.rb
53
53
  - lib/refinements/strings.rb
54
54
  - lib/refinements/structs.rb
55
55
  - lib/refinements/symbols.rb
56
+ - refinements.gemspec
56
57
  homepage: https://www.alchemists.io/projects/refinements
57
58
  licenses:
58
59
  - Hippocratic-3.0
59
60
  metadata:
60
61
  bug_tracker_uri: https://github.com/bkuhlmann/refinements/issues
61
- changelog_uri: https://www.alchemists.io/projects/refinements/changes.html
62
+ changelog_uri: https://www.alchemists.io/projects/refinements/versions
62
63
  documentation_uri: https://www.alchemists.io/projects/refinements
64
+ label: Refinements
63
65
  rubygems_mfa_required: 'true'
64
66
  source_code_uri: https://github.com/bkuhlmann/refinements
65
67
  post_install_message:
@@ -77,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
79
  - !ruby/object:Gem::Version
78
80
  version: '0'
79
81
  requirements: []
80
- rubygems_version: 3.3.3
82
+ rubygems_version: 3.3.5
81
83
  signing_key:
82
84
  specification_version: 4
83
85
  summary: A collection of refinements to core Ruby objects.
metadata.gz.sig CHANGED
Binary file
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Refinements
4
- # Gem identity information.
5
- module Identity
6
- NAME = "refinements"
7
- LABEL = "Refinements"
8
- VERSION = "9.0.0"
9
- VERSION_LABEL = "#{LABEL} #{VERSION}".freeze
10
- end
11
- end