refinements 7.16.0 → 8.1.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: 27c7f3818bd1f3d0d3fc39cb4c8721097d5003176b54710888deba6d012b4f77
4
- data.tar.gz: f607fea47fce94c11c3ab7dcc6603b169567133fbbe6ca76fb0af0c9fa0196e4
3
+ metadata.gz: 6446344f206e8c2b4175dedc5ccd4d14c4bcaadd6d7969fcf04ef3d185143b03
4
+ data.tar.gz: ccdd0a5a93d36e1c4f4e5ed23b2f29ee3ee1cd3d8654a919ade1ed17841228cf
5
5
  SHA512:
6
- metadata.gz: da5ff60dc1e6d4055adc850bbe7aa48d02c148f17fb42c9e50f939d577d506097603c2790cf5ce040cce1cb8389e28ebd363d3318d1224d06df4fc6d36e56b3f
7
- data.tar.gz: a5a96d51b0e95d1f603262ca8ce767969e3faca18645327f368d8ee03aed7d0fa27211d9e35a8af30bcc6ca86d6dad33d804a4c9f1b830789b39b06dc0791793
6
+ metadata.gz: 4b27775e5e325f98b308bdfd913129074266f75c4601caec7d48e9ca64e7608d24a2ebcbea4795a7548d61db36fecfe8ddbab3593c9aa9df8ca153b63ad41dac
7
+ data.tar.gz: afe11cd5ac6e81723f4afef741699a7615e96103c85fef24f51749e35ac02c79ffb0c9eb0fc459d63b7bba26f0b3469d251ae562b211fae4b8b29dbac71f02dc
@@ -1 +1,3 @@
1
- )q�w�֍~�B�i�������6su>Z<���Ϣ�7����&�wJ~x���ҩ�ϛ|)×+p}����ݮ�^3O?�tg^���8C�ޯ9yi�R.:k��l�����z�_-��8�i�C��=�Uw�sKxj�> ��+ t{EY"�W�a���J0��e���Qu���Wn�@�n�&aG��9���3����Rd0{�~�_v��Hsϼ���tl�jR]pzn���z.���n.��3 s����z;�z@xJ�
1
+ p������"����BcNDXR;A��VPȑsbaǣيL�z�?OT"
2
+ 8e���A�i��FeY�`b�S]���p�7�������� ^���H
3
+ ���.g�a=MA=蟌���fl��|$����'�:t3�A�bM3#%���\��<�&-%;��܍Y�iy ���b��Pt+��3%FVb8��yR����&�Y�� ����]j�/)�������"x�Xy��U�
data.tar.gz.sig CHANGED
Binary file
@@ -22,7 +22,6 @@ Enhances the following objects:
22
22
  * Array
23
23
  * BigDecimal
24
24
  * DateTime
25
- * File
26
25
  * Hash
27
26
  * IO
28
27
  * Pathname
@@ -70,7 +69,6 @@ gem "refinements", require: false
70
69
  require "refinements/arrays"
71
70
  require "refinements/big_decimals"
72
71
  require "refinements/date_times"
73
- require "refinements/files"
74
72
  require "refinements/hashes"
75
73
  require "refinements/ios"
76
74
  require "refinements/pathnames"
@@ -90,7 +88,6 @@ class Example
90
88
  using Refinements::Arrays
91
89
  using Refinements::BigDecimals
92
90
  using Refinements::DateTimes
93
- using Refinements::Files
94
91
  using Refinements::Hashes
95
92
  using Refinements::IOs
96
93
  using Refinements::Pathnames
@@ -171,6 +168,18 @@ Answers mean/average all elements within an array.
171
168
  [1.25, 1.5, 1.75].mean # => 1.5
172
169
  ----
173
170
 
171
+ ===== #pad
172
+
173
+ Answers new array padded with given value up to a maximum size. Useful in situations where an array
174
+ needs to be a specific size with padded values.
175
+
176
+ [source,ruby]
177
+ ----
178
+ [1].pad 0 # => [1]
179
+ [1].pad 0, max: 3 # => [1, 0, 0]
180
+ [1, 2].pad 3, max: 3 # => [1, 2, 3]
181
+ ----
182
+
174
183
  ===== #ring
175
184
 
176
185
  Answers a circular array which can enumerate before, current, after elements.
@@ -208,18 +217,6 @@ Answers new DateTime object for current UTC date/time.
208
217
  DateTime.utc # => #<DateTime: 2019-12-31T18:17:00+00:00 ((2458849j,65820s,181867000n),+0s,2299161j)>
209
218
  ----
210
219
 
211
- ==== File
212
-
213
- ===== .rewrite
214
-
215
- When given a file path and a block, it provides the contents of the recently read file for
216
- manipulation and immediate writing back to the same file.
217
-
218
- [source,ruby]
219
- ----
220
- File.rewrite("/test.txt") { |content| content.gsub "[placeholder]", "example" }
221
- ----
222
-
223
220
  ==== Hash
224
221
 
225
222
  ===== .infinite
@@ -312,28 +309,6 @@ example.deep_symbolize_keys! # => {a: {b: 1}}
312
309
  example # => {a: {b: 1}}
313
310
  ----
314
311
 
315
- ===== #except
316
-
317
- Answers new hash with given keys removed without mutating itself.
318
-
319
- [source,ruby]
320
- ----
321
- example = {a: 1, b: 2, c: 3}
322
- example.except :a, :b # => {c: 3}
323
- example # => {a: 1, b: 2, c: 3}
324
- ----
325
-
326
- ===== #except!
327
-
328
- Answers new hash with given keys removed while mutating itself.
329
-
330
- [source,ruby]
331
- ----
332
- example = {a: 1, b: 2, c: 3}
333
- example.except! :a, :b # => {c: 3}
334
- example # => {c: 3}
335
- ----
336
-
337
312
  ===== #flatten_keys
338
313
 
339
314
  Flattens nested keys as top-level keys without mutating itself. Does not handle nested arrays,
@@ -376,50 +351,6 @@ example.recurse(&:symbolize_keys) # => {a: {b: 1}}
376
351
  example.recurse(&:invert) # => {{"b" => 1} => "a"}
377
352
  ----
378
353
 
379
- ===== #rekey
380
-
381
- Transforms keys per mapping (size of mapping can vary) without mutating itself.
382
-
383
- [source,ruby]
384
- ----
385
- example = {a: 1, b: 2, c: 3}
386
- example.rekey a: :amber, b: :blue # => {amber: 1, blue: 2, c: 3}
387
- example # => {a: 1, b: 2, c: 3}
388
- ----
389
-
390
- ===== #rekey!
391
-
392
- Transforms keys per mapping (size of mapping can vary) while mutating itself.
393
-
394
- [source,ruby]
395
- ----
396
- example = {a: 1, b: 2, c: 3}
397
- example.rekey! a: :amber, b: :blue # => {amber: 1, blue: 2, c: 3}
398
- example # => {amber: 1, blue: 2, c: 3}
399
- ----
400
-
401
- ===== #reverse_merge
402
-
403
- Merges calling hash into passed in hash without mutating itself.
404
-
405
- [source,ruby]
406
- ----
407
- example = {a: 1, b: 2}
408
- example.reverse_merge a: 0, c: 3 # => {a: 1, b: 2, c: 3}
409
- example # => {a: 1, b: 2}
410
- ----
411
-
412
- ===== #reverse_merge!
413
-
414
- Merges calling hash into passed in hash while mutating itself.
415
-
416
- [source,ruby]
417
- ----
418
- example = {a: 1, b: 2}
419
- example.reverse_merge! a: 0, c: 3 # => {a: 1, b: 2, c: 3}
420
- example # => {a: 1, b: 2, c: 3}
421
- ----
422
-
423
354
  ===== #stringify_keys
424
355
 
425
356
  Converts keys to strings without mutating itself.
@@ -583,7 +514,7 @@ Pathname.make_temp_dir { |path| path.join "sub_dir" } # => Pathname:/var/
583
514
  Requires all files in given root path and corresponding nested tree structure. All files are sorted
584
515
  before being required to ensure consistent behavior. Example:
585
516
 
586
- [source,rby]
517
+ [source,ruby]
587
518
  ----
588
519
  # Before
589
520
  Dir[File.join(__dir__, "support/shared_contexts/**/*.rb")].sort.each { |path| require path }
@@ -785,8 +716,8 @@ Updates access and modification times for path. Defaults to current time.
785
716
 
786
717
  [source,ruby]
787
718
  ----
788
- Pathname("example.txt").touch # => Pathname("example.txt")
789
- Pathname("example.txt").touch at: Time.now - 1 # => Pathname("example.txt")
719
+ Pathname("example.txt").touch # => Pathname("example.txt")
720
+ Pathname("example.txt").touch Time.now - 1 # => Pathname("example.txt")
790
721
  ----
791
722
 
792
723
  ===== #write
@@ -924,6 +855,50 @@ buffer # => "This is a test."
924
855
 
925
856
  ==== Struct
926
857
 
858
+ ===== .keyworded?
859
+
860
+ Answers whether a struct was constructed with keyword or positional arguments.
861
+
862
+ [source,ruby]
863
+ ----
864
+ Struct.new(:a, keyword_init: true).keyworded? # => true
865
+ Struct.new(:a).keyworded? # => false
866
+ ----
867
+
868
+ ===== .with_keywords
869
+
870
+ Answers a struct instance with given keyword arguments regardless of
871
+ whether the struct was constructed with positional or keyword arguments.
872
+
873
+ [source,ruby]
874
+ ----
875
+ Example = Struct.new :a, :b, :c
876
+ Example.with_keywords a: 1, b: 2, c: 3 # => #<struct a=1, b=2, c=3>
877
+ Example.with_keywords a: 1 # => #<struct a=1, b=nil, c=nil>
878
+ Example.with_keywords c: 1 # => #<struct a=nil, b=nil, c=1>
879
+
880
+ Example = Struct.new :a, :b, :c, keyword_init: true
881
+ Example.with_keywords a: 1, b: 2, c: 3 # => #<struct a=1, b=2, c=3>
882
+ Example.with_keywords a: 1 # => #<struct a=1, b=nil, c=nil>
883
+ Example.with_keywords c: 1 # => #<struct a=nil, b=nil, c=1>
884
+ ----
885
+
886
+ ===== .with_positions
887
+
888
+ Answers a struct instance with given positional arguments regardless of
889
+ whether the struct was constructed with positional or keyword arguments.
890
+
891
+ [source,ruby]
892
+ ----
893
+ Example = Struct.new :a, :b, :c
894
+ Example.with_positions 1, 2, 3 # => #<struct a=1, b=2, c=3>
895
+ Example.with_positions 1 # => #<struct a=1, b=nil, c=nil>
896
+
897
+ Example = Struct.new :a, :b, :c, keyword_init: true
898
+ Example.with_positions 1, 2, 3 # => #<struct a=1, b=2, c=3>
899
+ Example.with_positions 1 # => #<struct a=1, b=nil, c=nil>
900
+ ----
901
+
927
902
  ===== #merge
928
903
 
929
904
  Merges multiple attributes without mutating itself.
@@ -966,6 +941,53 @@ example.merge! a: 10, b: 20, c: 30 # => #<struct a=10, b=20, c=30>
966
941
  example # => #<struct a=10, b=20, c=30>
967
942
  ----
968
943
 
944
+ ===== #revalue
945
+
946
+ Transforms values without mutating itself. An optional hash can be supplied to pinpoint and
947
+ transform specific attributes. In the event that a block isn't supplied, the struct will answer
948
+ itself since there is nothing to operate on. Behavior is the same regardless of whether the struct
949
+ is constructed using positional or keyword arguments. A positional struct is used in the examples
950
+ below but a keyword struct would work too.
951
+
952
+ [source,ruby]
953
+ ----
954
+ Example = Struct.new :a, :b, :c
955
+
956
+ example = Example[1, 2, 3]
957
+ example.revalue { |value| value * 2 } # => #<struct a=2, b=4, c=6>
958
+ example.revalue(c: 2) { |previous, current| previous + current } # => #<struct a=1, b=2, c=5>
959
+ example.revalue c: 2 # => #<struct a=1, b=2, c=3>
960
+ example.revalue # => #<struct a=1, b=2, c=3>
961
+ example # => #<struct a=1, b=2, c=3>
962
+
963
+ ----
964
+
965
+ ===== #revalue!
966
+
967
+ Transforms values while mutating itself. An optional hash can be supplied to pinpoint and transform
968
+ specific attributes. In the event that a block isn't supplied, the struct will answer itself since
969
+ there is nothing to operate on. Behavior is the same regardless of whether the struct is constructed
970
+ using positional or keyword arguments. A positional struct is used in the examples below but a
971
+ keyword struct would work too.
972
+
973
+ [source,ruby]
974
+ ----
975
+ Example = Struct.new :a, :b, :c
976
+
977
+ example = Example[1, 2, 3]
978
+ example.revalue! { |value| value * 2 } # => #<struct a=2, b=4, c=6>
979
+ example # => #<struct a=2, b=4, c=6>
980
+
981
+ example = Example[1, 2, 3]
982
+ example.revalue!(c: 2) { |previous, current| previous + current } # => #<struct a=1, b=2, c=5>
983
+ example # => #<struct a=1, b=2, c=5>
984
+
985
+ example = Example[1, 2, 3]
986
+ example.revalue! c: 2 # => #<struct a=1, b=2, c=3>
987
+ example.revalue! # => #<struct a=1, b=2, c=3>
988
+ example # => #<struct a=1, b=2, c=3>
989
+ ----
990
+
969
991
  == Development
970
992
 
971
993
  To contribute, run:
@@ -4,7 +4,6 @@ require "refinements/identity"
4
4
  require "refinements/arrays"
5
5
  require "refinements/big_decimals"
6
6
  require "refinements/date_times"
7
- require "refinements/files"
8
7
  require "refinements/hashes"
9
8
  require "refinements/ios"
10
9
  require "refinements/pathnames"
@@ -3,43 +3,21 @@
3
3
  module Refinements
4
4
  module Arrays
5
5
  refine Array do
6
- def compress
7
- compact.reject(&:empty?)
8
- end
9
-
10
- def compress!
11
- replace compress
12
- end
13
-
14
- def exclude *elements
15
- warn "[DEPRECATION]: #exclude is deprecated, use #excluding instead."
16
- excluding(*elements)
17
- end
18
-
19
- def excluding *elements
20
- self - elements.flatten
21
- end
22
-
23
- def include *elements
24
- warn "[DEPRECATION]: #include is deprecated, use #including instead."
25
- including(*elements)
26
- end
27
-
28
- def including *elements
29
- self + elements.flatten
30
- end
31
-
32
- def intersperse *elements
33
- product([elements]).tap(&:pop).flatten.push last
34
- end
35
-
36
- def mean
37
- size.zero? ? 0 : sum(0) / size
38
- end
39
-
40
- def ring &block
41
- [last, *self, first].each_cons 3, &block
42
- end
6
+ def compress = compact.reject(&:empty?)
7
+
8
+ def compress! = replace(compress)
9
+
10
+ def excluding(*elements) = self - elements.flatten
11
+
12
+ def including(*elements) = self + elements.flatten
13
+
14
+ def intersperse(*elements) = product([elements]).tap(&:pop).flatten.push(last)
15
+
16
+ def mean = size.zero? ? 0 : sum(0) / size
17
+
18
+ def pad(value, max: size) = dup.fill(value, size..(max - 1))
19
+
20
+ def ring(&block) = [last, *self, first].each_cons(3, &block)
43
21
  end
44
22
  end
45
23
  end
@@ -5,9 +5,7 @@ require "bigdecimal"
5
5
  module Refinements
6
6
  module BigDecimals
7
7
  refine BigDecimal do
8
- def inspect
9
- format "#<BigDecimal:%{id} %{string}>", id: object_id, string: to_s("F")
10
- end
8
+ def inspect = format("#<BigDecimal:%{id} %{string}>", id: object_id, string: to_s("F"))
11
9
  end
12
10
  end
13
11
  end
@@ -5,9 +5,7 @@ require "date"
5
5
  module Refinements
6
6
  module DateTimes
7
7
  refine DateTime.singleton_class do
8
- def utc
9
- now.new_offset 0
10
- end
8
+ def utc = now.new_offset(0)
11
9
  end
12
10
  end
13
11
  end
@@ -7,9 +7,7 @@ module Refinements
7
7
  new { |new_hash, missing_key| new_hash[missing_key] = new(&new_hash.default_proc) }
8
8
  end
9
9
 
10
- def with_default value
11
- new { |new_hash, missing_key| new_hash[missing_key] = value }
12
- end
10
+ def with_default(value) = new { |new_hash, missing_key| new_hash[missing_key] = value }
13
11
  end
14
12
 
15
13
  refine Hash do
@@ -25,33 +23,15 @@ module Refinements
25
23
  end
26
24
  end
27
25
 
28
- def deep_merge! other
29
- replace deep_merge(other)
30
- end
31
-
32
- def deep_stringify_keys
33
- recurse(&:stringify_keys)
34
- end
35
-
36
- def deep_stringify_keys!
37
- replace deep_stringify_keys
38
- end
26
+ def deep_merge!(other) = replace(deep_merge(other))
39
27
 
40
- def deep_symbolize_keys
41
- recurse(&:symbolize_keys)
42
- end
28
+ def deep_stringify_keys = recurse(&:stringify_keys)
43
29
 
44
- def deep_symbolize_keys!
45
- replace deep_symbolize_keys
46
- end
30
+ def deep_stringify_keys! = replace(deep_stringify_keys)
47
31
 
48
- def except *keys
49
- reject { |key, _value| keys.include? key }
50
- end
32
+ def deep_symbolize_keys = recurse(&:symbolize_keys)
51
33
 
52
- def except! *keys
53
- replace except(*keys)
54
- end
34
+ def deep_symbolize_keys! = replace(deep_symbolize_keys)
55
35
 
56
36
  # :reek:TooManyStatements
57
37
  def flatten_keys prefix: nil, delimiter: "_", cast: :to_sym
@@ -82,41 +62,13 @@ module Refinements
82
62
  end
83
63
  end
84
64
 
85
- def rekey mapping = {}
86
- return self if mapping.empty?
65
+ def stringify_keys = reduce({}) { |hash, (key, value)| hash.merge key.to_s => value }
87
66
 
88
- transform_keys { |key| mapping[key] || key }
89
- end
67
+ def stringify_keys! = replace(stringify_keys)
90
68
 
91
- def rekey! mapping = {}
92
- replace rekey(mapping)
93
- end
69
+ def symbolize_keys = reduce({}) { |hash, (key, value)| hash.merge key.to_sym => value }
94
70
 
95
- def reverse_merge other
96
- warn "[DEPRECATION]: #reverse_merge is deprecated, use #merge instead."
97
- merge(other) { |_key, old_value, _new_value| old_value }
98
- end
99
-
100
- def reverse_merge! other
101
- warn "[DEPRECATION]: #reverse_merge! is deprecated, use #merge! instead."
102
- replace reverse_merge(other)
103
- end
104
-
105
- def stringify_keys
106
- reduce({}) { |hash, (key, value)| hash.merge key.to_s => value }
107
- end
108
-
109
- def stringify_keys!
110
- replace stringify_keys
111
- end
112
-
113
- def symbolize_keys
114
- reduce({}) { |hash, (key, value)| hash.merge key.to_sym => value }
115
- end
116
-
117
- def symbolize_keys!
118
- replace symbolize_keys
119
- end
71
+ def symbolize_keys! = replace(symbolize_keys)
120
72
 
121
73
  def use &block
122
74
  return [] unless block
@@ -5,7 +5,7 @@ module Refinements
5
5
  module Identity
6
6
  NAME = "refinements"
7
7
  LABEL = "Refinements"
8
- VERSION = "7.16.0"
8
+ VERSION = "8.1.0"
9
9
  VERSION_LABEL = "#{LABEL} #{VERSION}"
10
10
  end
11
11
  end
@@ -23,13 +23,9 @@ module Refinements
23
23
  reopen backup
24
24
  end
25
25
 
26
- def reread length = nil, buffer: nil
27
- tap(&:rewind).read length, buffer
28
- end
26
+ def reread(length = nil, buffer: nil) = tap(&:rewind).read(length, buffer)
29
27
 
30
- def squelch &block
31
- self.class.void.then { |void| redirect(void, &block) }
32
- end
28
+ def squelch(&block) = self.class.void.then { |void| redirect(void, &block) }
33
29
  end
34
30
  end
35
31
  end
@@ -5,6 +5,7 @@ require "pathname"
5
5
  module Refinements
6
6
  module Pathnames
7
7
  refine Kernel do
8
+ # :reek:UncommunicativeMethodName
8
9
  def Pathname object
9
10
  return super(String(object)) unless object
10
11
 
@@ -13,9 +14,7 @@ module Refinements
13
14
  end
14
15
 
15
16
  refine Pathname.singleton_class do
16
- def home
17
- new ENV["HOME"]
18
- end
17
+ def home = new(ENV["HOME"])
19
18
 
20
19
  def make_temp_dir prefix: "temp-", suffix: nil, root: nil
21
20
  Dir.mktmpdir([prefix, suffix], root) { |path| block_given? ? yield(new path) : new(path) }
@@ -25,9 +24,7 @@ module Refinements
25
24
  new(root).files(pattern).each { |path| require path.to_s }
26
25
  end
27
26
 
28
- def root
29
- new "/"
30
- end
27
+ def root = new("/")
31
28
  end
32
29
 
33
30
  refine Pathname do
@@ -45,64 +42,38 @@ module Refinements
45
42
  glob(pattern, flag).select(&:directory?).sort
46
43
  end
47
44
 
48
- def extensions
49
- basename.to_s.split(/(?=\.)+/).tap(&:shift)
50
- end
45
+ def extensions = basename.to_s.split(/(?=\.)+/).tap(&:shift)
51
46
 
52
- def files pattern = "*", flag: File::FNM_SYSCASE
53
- glob(pattern, flag).select(&:file?).sort
54
- end
47
+ def files(pattern = "*", flag: File::FNM_SYSCASE) = glob(pattern, flag).select(&:file?).sort
55
48
 
56
- def gsub pattern, replacement
57
- self.class.new to_s.gsub(pattern, replacement)
58
- end
49
+ def gsub(pattern, replacement) = self.class.new(to_s.gsub(pattern, replacement))
59
50
 
60
51
  def make_ancestors
61
52
  dirname.mkpath
62
53
  self
63
54
  end
64
55
 
65
- def make_dir
66
- exist? ? self : mkdir and self
67
- end
56
+ def make_dir = exist? ? self : (mkdir and self)
68
57
 
69
58
  def make_path
70
59
  mkpath
71
60
  self
72
61
  end
73
62
 
74
- def mkdir
75
- exist? ? self : super and self
76
- end
77
-
78
- def name
79
- basename extname
80
- end
63
+ def name = basename(extname)
81
64
 
82
- def relative_parent root_dir
83
- relative_path_from(root_dir).parent
84
- end
65
+ def relative_parent(root_dir) = relative_path_from(root_dir).parent
85
66
 
86
- def relative_parent_from root_dir
87
- warn "[DEPRECATION]: Pathname#relative_parent_from is deprecated, " \
88
- "use Pathname#relative_parent instead."
89
- relative_parent root_dir
90
- end
91
-
92
- def remove_dir
93
- exist? ? (rmdir and self) : self
94
- end
67
+ def remove_dir = exist? ? (rmdir and self) : self
95
68
 
96
69
  def remove_tree
97
70
  rmtree if exist?
98
71
  self
99
72
  end
100
73
 
101
- def rewrite
102
- read.then { |content| write yield(content) if block_given? }
103
- end
74
+ def rewrite = read.then { |content| write yield(content) if block_given? }
104
75
 
105
- def touch at: Time.now
76
+ def touch at = Time.now
106
77
  exist? ? utime(at, at) : write("")
107
78
  self
108
79
  end
@@ -5,9 +5,7 @@ require "stringio"
5
5
  module Refinements
6
6
  module StringIOs
7
7
  refine StringIO do
8
- def reread length = nil, buffer: nil
9
- tap(&:rewind).read length, buffer
10
- end
8
+ def reread(length = nil, buffer: nil) = tap(&:rewind).read(length, buffer)
11
9
  end
12
10
  end
13
11
  end
@@ -2,19 +2,10 @@
2
2
 
3
3
  module Refinements
4
4
  module Strings
5
- DELIMITERS = %r([a-z][A-Z]|\s*-\s*|\s*/\s*|\s*:+\s*|\s*_\s*|\s+).freeze
6
-
7
- refine String.singleton_class do
8
- def delimiters
9
- warn "[DEPRECATION]: .delimiters is deprecated, use DELIMITERS instead."
10
- DELIMITERS
11
- end
12
- end
5
+ DELIMITERS = %r([a-z][A-Z]|\s*-\s*|\s*/\s*|\s*:+\s*|\s*_\s*|\s+)
13
6
 
14
7
  refine String do
15
- def blank?
16
- match?(/\A\s*\z/)
17
- end
8
+ def blank? = match?(/\A\s*\z/)
18
9
 
19
10
  def camelcase
20
11
  return up unless match? DELIMITERS
@@ -74,9 +65,7 @@ module Refinements
74
65
  .then { |parts| combine parts, :up, "/" }
75
66
  end
76
67
 
77
- def to_bool
78
- %w[true yes on t y 1].include? downcase.strip
79
- end
68
+ def to_bool = %w[true yes on t y 1].include?(downcase.strip)
80
69
 
81
70
  def up
82
71
  return self if empty?
@@ -2,13 +2,34 @@
2
2
 
3
3
  module Refinements
4
4
  module Structs
5
+ refine Struct.singleton_class do
6
+ def keyworded? = inspect.include?("keyword_init: true")
7
+
8
+ def with_keywords(**arguments) = keyworded? ? new(**arguments) : new.merge!(**arguments)
9
+
10
+ def with_positions(*values) = keyworded? ? new(**Hash[members.zip values]) : new(*values)
11
+ end
12
+
5
13
  refine Struct do
6
- def merge **attributes
7
- dup.merge! attributes
8
- end
14
+ def merge(**attributes) = dup.merge!(**attributes)
9
15
 
10
16
  def merge! **attributes
11
- to_h.merge(attributes).each { |key, value| self[key] = value }
17
+ to_h.merge(**attributes).each { |key, value| self[key] = value }
18
+ self
19
+ end
20
+
21
+ def revalue attributes = each_pair
22
+ return self unless block_given?
23
+
24
+ dup.tap do |copy|
25
+ attributes.each { |key, value| copy[key] = yield self[key], value }
26
+ end
27
+ end
28
+
29
+ def revalue! attributes = each_pair
30
+ return self unless block_given?
31
+
32
+ attributes.each { |key, value| self[key] = yield self[key], value }
12
33
  self
13
34
  end
14
35
  end
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: 7.16.0
4
+ version: 8.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brooke Kuhlmann
@@ -28,218 +28,8 @@ cert_chain:
28
28
  2XV8FRa7/JimI07sPLC13eLY3xd/aYTi85Z782KIA4j0G8XEEWAX0ouBhlXPocZv
29
29
  QWc=
30
30
  -----END CERTIFICATE-----
31
- date: 2020-11-28 00:00:00.000000000 Z
32
- dependencies:
33
- - !ruby/object:Gem::Dependency
34
- name: bundler-audit
35
- requirement: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - "~>"
38
- - !ruby/object:Gem::Version
39
- version: '0.7'
40
- type: :development
41
- prerelease: false
42
- version_requirements: !ruby/object:Gem::Requirement
43
- requirements:
44
- - - "~>"
45
- - !ruby/object:Gem::Version
46
- version: '0.7'
47
- - !ruby/object:Gem::Dependency
48
- name: bundler-leak
49
- requirement: !ruby/object:Gem::Requirement
50
- requirements:
51
- - - "~>"
52
- - !ruby/object:Gem::Version
53
- version: '0.2'
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- requirements:
58
- - - "~>"
59
- - !ruby/object:Gem::Version
60
- version: '0.2'
61
- - !ruby/object:Gem::Dependency
62
- name: gemsmith
63
- requirement: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - "~>"
66
- - !ruby/object:Gem::Version
67
- version: '14.8'
68
- type: :development
69
- prerelease: false
70
- version_requirements: !ruby/object:Gem::Requirement
71
- requirements:
72
- - - "~>"
73
- - !ruby/object:Gem::Version
74
- version: '14.8'
75
- - !ruby/object:Gem::Dependency
76
- name: git-lint
77
- requirement: !ruby/object:Gem::Requirement
78
- requirements:
79
- - - "~>"
80
- - !ruby/object:Gem::Version
81
- version: '1.3'
82
- type: :development
83
- prerelease: false
84
- version_requirements: !ruby/object:Gem::Requirement
85
- requirements:
86
- - - "~>"
87
- - !ruby/object:Gem::Version
88
- version: '1.3'
89
- - !ruby/object:Gem::Dependency
90
- name: guard-rspec
91
- requirement: !ruby/object:Gem::Requirement
92
- requirements:
93
- - - "~>"
94
- - !ruby/object:Gem::Version
95
- version: '4.7'
96
- type: :development
97
- prerelease: false
98
- version_requirements: !ruby/object:Gem::Requirement
99
- requirements:
100
- - - "~>"
101
- - !ruby/object:Gem::Version
102
- version: '4.7'
103
- - !ruby/object:Gem::Dependency
104
- name: pry
105
- requirement: !ruby/object:Gem::Requirement
106
- requirements:
107
- - - "~>"
108
- - !ruby/object:Gem::Version
109
- version: '0.13'
110
- type: :development
111
- prerelease: false
112
- version_requirements: !ruby/object:Gem::Requirement
113
- requirements:
114
- - - "~>"
115
- - !ruby/object:Gem::Version
116
- version: '0.13'
117
- - !ruby/object:Gem::Dependency
118
- name: pry-byebug
119
- requirement: !ruby/object:Gem::Requirement
120
- requirements:
121
- - - "~>"
122
- - !ruby/object:Gem::Version
123
- version: '3.9'
124
- type: :development
125
- prerelease: false
126
- version_requirements: !ruby/object:Gem::Requirement
127
- requirements:
128
- - - "~>"
129
- - !ruby/object:Gem::Version
130
- version: '3.9'
131
- - !ruby/object:Gem::Dependency
132
- name: rake
133
- requirement: !ruby/object:Gem::Requirement
134
- requirements:
135
- - - "~>"
136
- - !ruby/object:Gem::Version
137
- version: '13.0'
138
- type: :development
139
- prerelease: false
140
- version_requirements: !ruby/object:Gem::Requirement
141
- requirements:
142
- - - "~>"
143
- - !ruby/object:Gem::Version
144
- version: '13.0'
145
- - !ruby/object:Gem::Dependency
146
- name: reek
147
- requirement: !ruby/object:Gem::Requirement
148
- requirements:
149
- - - "~>"
150
- - !ruby/object:Gem::Version
151
- version: '6.0'
152
- type: :development
153
- prerelease: false
154
- version_requirements: !ruby/object:Gem::Requirement
155
- requirements:
156
- - - "~>"
157
- - !ruby/object:Gem::Version
158
- version: '6.0'
159
- - !ruby/object:Gem::Dependency
160
- name: rspec
161
- requirement: !ruby/object:Gem::Requirement
162
- requirements:
163
- - - "~>"
164
- - !ruby/object:Gem::Version
165
- version: '3.10'
166
- type: :development
167
- prerelease: false
168
- version_requirements: !ruby/object:Gem::Requirement
169
- requirements:
170
- - - "~>"
171
- - !ruby/object:Gem::Version
172
- version: '3.10'
173
- - !ruby/object:Gem::Dependency
174
- name: rubocop
175
- requirement: !ruby/object:Gem::Requirement
176
- requirements:
177
- - - "~>"
178
- - !ruby/object:Gem::Version
179
- version: '1.3'
180
- type: :development
181
- prerelease: false
182
- version_requirements: !ruby/object:Gem::Requirement
183
- requirements:
184
- - - "~>"
185
- - !ruby/object:Gem::Version
186
- version: '1.3'
187
- - !ruby/object:Gem::Dependency
188
- name: rubocop-performance
189
- requirement: !ruby/object:Gem::Requirement
190
- requirements:
191
- - - "~>"
192
- - !ruby/object:Gem::Version
193
- version: '1.8'
194
- type: :development
195
- prerelease: false
196
- version_requirements: !ruby/object:Gem::Requirement
197
- requirements:
198
- - - "~>"
199
- - !ruby/object:Gem::Version
200
- version: '1.8'
201
- - !ruby/object:Gem::Dependency
202
- name: rubocop-rake
203
- requirement: !ruby/object:Gem::Requirement
204
- requirements:
205
- - - "~>"
206
- - !ruby/object:Gem::Version
207
- version: '0.5'
208
- type: :development
209
- prerelease: false
210
- version_requirements: !ruby/object:Gem::Requirement
211
- requirements:
212
- - - "~>"
213
- - !ruby/object:Gem::Version
214
- version: '0.5'
215
- - !ruby/object:Gem::Dependency
216
- name: rubocop-rspec
217
- requirement: !ruby/object:Gem::Requirement
218
- requirements:
219
- - - "~>"
220
- - !ruby/object:Gem::Version
221
- version: '2.0'
222
- type: :development
223
- prerelease: false
224
- version_requirements: !ruby/object:Gem::Requirement
225
- requirements:
226
- - - "~>"
227
- - !ruby/object:Gem::Version
228
- version: '2.0'
229
- - !ruby/object:Gem::Dependency
230
- name: simplecov
231
- requirement: !ruby/object:Gem::Requirement
232
- requirements:
233
- - - "~>"
234
- - !ruby/object:Gem::Version
235
- version: '0.19'
236
- type: :development
237
- prerelease: false
238
- version_requirements: !ruby/object:Gem::Requirement
239
- requirements:
240
- - - "~>"
241
- - !ruby/object:Gem::Version
242
- version: '0.19'
31
+ date: 2021-01-16 00:00:00.000000000 Z
32
+ dependencies: []
243
33
  description:
244
34
  email:
245
35
  - brooke@alchemists.io
@@ -255,7 +45,6 @@ files:
255
45
  - lib/refinements/arrays.rb
256
46
  - lib/refinements/big_decimals.rb
257
47
  - lib/refinements/date_times.rb
258
- - lib/refinements/files.rb
259
48
  - lib/refinements/hashes.rb
260
49
  - lib/refinements/identity.rb
261
50
  - lib/refinements/ios.rb
@@ -279,14 +68,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
279
68
  requirements:
280
69
  - - "~>"
281
70
  - !ruby/object:Gem::Version
282
- version: '2.7'
71
+ version: '3.0'
283
72
  required_rubygems_version: !ruby/object:Gem::Requirement
284
73
  requirements:
285
74
  - - ">="
286
75
  - !ruby/object:Gem::Version
287
76
  version: '0'
288
77
  requirements: []
289
- rubygems_version: 3.1.4
78
+ rubygems_version: 3.2.5
290
79
  signing_key:
291
80
  specification_version: 4
292
81
  summary: A collection of refinements to core Ruby objects.
metadata.gz.sig CHANGED
Binary file
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Refinements
4
- module Files
5
- refine File.singleton_class do
6
- def rewrite path
7
- warn "[DEPRECATION]: File.rewrite is deprecated, use Pathname#rewrite instead."
8
- read(path).then { |content| write path, yield(content) }
9
- end
10
- end
11
- end
12
- end