refinements 7.17.0 → 7.18.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: a24d8f2e7c34e994169a0c99ee15bfd30a8ed20d3aed61b567190d76c82cb23c
4
- data.tar.gz: cbf64daa1b7e41296ccb4ed1dd3e288fa958fc7a1df64f5661464c5d350cea3f
3
+ metadata.gz: 5dd9bd6cbc9800c0c66d8c1ae4ed93124143f196a4173723438bb7d9fc12957e
4
+ data.tar.gz: 9d6651c258900528aa68db2a8b26e1bd77aef9be70fe3fbeadf63e4c0580ab45
5
5
  SHA512:
6
- metadata.gz: 73e908dc60bb232b21f8a5a670a42d803d8a69cf74034be74a65885761868f139db5e61362c4de33d08f0541d2716880e3b20f6772ddb6445c1ff545459fffec
7
- data.tar.gz: 3843e65221606fc7671e2bf8696086de50be0561b2cde91f7e3d6e5b5ebdf604f018f2cb6bf132723ef68fa7079cfcdbf7dbae070574fb6e5f3144629f0a967d
6
+ metadata.gz: bad8de30ca4c07607013a08d8346d1390b3f6223b943588ac94ce1c7e44fb5f59ebb634c9adb4d0f1773929ede430ffc7025fa9e6fcdca6f055d3bfa4c5cbeb8
7
+ data.tar.gz: d81030c3b1a8db17fff6cafea12496041d2cb95e87452cf932c7cf03c60dea4971f1bbce59c30fde5b5a8d80f713b5a00fca1cbcbba6bbb1962f77e21984f9b0
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -171,6 +171,18 @@ Answers mean/average all elements within an array.
171
171
  [1.25, 1.5, 1.75].mean # => 1.5
172
172
  ----
173
173
 
174
+ ===== #pad
175
+
176
+ Answers new array padded with given value up to a maximum size. Useful in situations where an array
177
+ needs to be a specific size with padded values.
178
+
179
+ [source,ruby]
180
+ ----
181
+ [1].pad 0 # => [1]
182
+ [1].pad 0, max: 3 # => [1, 0, 0]
183
+ [1, 2].pad 3, max: 3 # => [1, 2, 3]
184
+ ----
185
+
174
186
  ===== #ring
175
187
 
176
188
  Answers a circular array which can enumerate before, current, after elements.
@@ -924,6 +936,50 @@ buffer # => "This is a test."
924
936
 
925
937
  ==== Struct
926
938
 
939
+ ===== .keyworded?
940
+
941
+ Answers whether a struct was constructed with keyword or positional arguments.
942
+
943
+ [source,ruby]
944
+ ----
945
+ Struct.new(:a, keyword_init: true).keyworded? # => true
946
+ Struct.new(:a).keyworded? # => false
947
+ ----
948
+
949
+ ===== .with_keywords
950
+
951
+ Answers a struct instance with given keyword arguments regardless of
952
+ whether the struct was constructed with positional or keyword arguments.
953
+
954
+ [source,ruby]
955
+ ----
956
+ Example = Struct.new :a, :b, :c
957
+ Example.with_keywords a: 1, b: 2, c: 3 # => #<struct a=1, b=2, c=3>
958
+ Example.with_keywords a: 1 # => #<struct a=1, b=nil, c=nil>
959
+ Example.with_keywords c: 1 # => #<struct a=nil, b=nil, c=1>
960
+
961
+ Example = Struct.new :a, :b, :c, keyword_init: true
962
+ Example.with_keywords a: 1, b: 2, c: 3 # => #<struct a=1, b=2, c=3>
963
+ Example.with_keywords a: 1 # => #<struct a=1, b=nil, c=nil>
964
+ Example.with_keywords c: 1 # => #<struct a=nil, b=nil, c=1>
965
+ ----
966
+
967
+ ===== .with_positions
968
+
969
+ Answers a struct instance with given positional arguments regardless of
970
+ whether the struct was constructed with positional or keyword arguments.
971
+
972
+ [source,ruby]
973
+ ----
974
+ Example = Struct.new :a, :b, :c
975
+ Example.with_positions 1, 2, 3 # => #<struct a=1, b=2, c=3>
976
+ Example.with_positions 1 # => #<struct a=1, b=nil, c=nil>
977
+
978
+ Example = Struct.new :a, :b, :c, keyword_init: true
979
+ Example.with_positions 1, 2, 3 # => #<struct a=1, b=2, c=3>
980
+ Example.with_positions 1 # => #<struct a=1, b=nil, c=nil>
981
+ ----
982
+
927
983
  ===== #merge
928
984
 
929
985
  Merges multiple attributes without mutating itself.
@@ -966,6 +1022,53 @@ example.merge! a: 10, b: 20, c: 30 # => #<struct a=10, b=20, c=30>
966
1022
  example # => #<struct a=10, b=20, c=30>
967
1023
  ----
968
1024
 
1025
+ ===== #revalue
1026
+
1027
+ Transforms values without mutating itself. An optional hash can be supplied to pinpoint and
1028
+ transform specific attributes. In the event that a block isn't supplied, the struct will answer
1029
+ itself since there is nothing to operate on. Behavior is the same regardless of whether the struct
1030
+ is constructed using positional or keyword arguments. A positional struct is used in the examples
1031
+ below but a keyword struct would work too.
1032
+
1033
+ [source,ruby]
1034
+ ----
1035
+ Example = Struct.new :a, :b, :c
1036
+
1037
+ example = Example[1, 2, 3]
1038
+ example.revalue { |value| value * 2 } # => #<struct a=2, b=4, c=6>
1039
+ example.revalue(c: 2) { |previous, current| previous + current } # => #<struct a=1, b=2, c=5>
1040
+ example.revalue c: 2 # => #<struct a=1, b=2, c=3>
1041
+ example.revalue # => #<struct a=1, b=2, c=3>
1042
+ example # => #<struct a=1, b=2, c=3>
1043
+
1044
+ ----
1045
+
1046
+ ===== #revalue!
1047
+
1048
+ Transforms values while mutating itself. An optional hash can be supplied to pinpoint and transform
1049
+ specific attributes. In the event that a block isn't supplied, the struct will answer itself since
1050
+ there is nothing to operate on. Behavior is the same regardless of whether the struct is constructed
1051
+ using positional or keyword arguments. A positional struct is used in the examples below but a
1052
+ keyword struct would work too.
1053
+
1054
+ [source,ruby]
1055
+ ----
1056
+ Example = Struct.new :a, :b, :c
1057
+
1058
+ example = Example[1, 2, 3]
1059
+ example.revalue! { |value| value * 2 } # => #<struct a=2, b=4, c=6>
1060
+ example # => #<struct a=2, b=4, c=6>
1061
+
1062
+ example = Example[1, 2, 3]
1063
+ example.revalue!(c: 2) { |previous, current| previous + current } # => #<struct a=1, b=2, c=5>
1064
+ example # => #<struct a=1, b=2, c=5>
1065
+
1066
+ example = Example[1, 2, 3]
1067
+ example.revalue! c: 2 # => #<struct a=1, b=2, c=3>
1068
+ example.revalue! # => #<struct a=1, b=2, c=3>
1069
+ example # => #<struct a=1, b=2, c=3>
1070
+ ----
1071
+
969
1072
  == Development
970
1073
 
971
1074
  To contribute, run:
@@ -37,6 +37,10 @@ module Refinements
37
37
  size.zero? ? 0 : sum(0) / size
38
38
  end
39
39
 
40
+ def pad value, max: size
41
+ dup.fill value, size..(max - 1)
42
+ end
43
+
40
44
  def ring &block
41
45
  [last, *self, first].each_cons 3, &block
42
46
  end
@@ -5,7 +5,7 @@ module Refinements
5
5
  module Identity
6
6
  NAME = "refinements"
7
7
  LABEL = "Refinements"
8
- VERSION = "7.17.0"
8
+ VERSION = "7.18.0"
9
9
  VERSION_LABEL = "#{LABEL} #{VERSION}"
10
10
  end
11
11
  end
@@ -2,6 +2,20 @@
2
2
 
3
3
  module Refinements
4
4
  module Structs
5
+ refine Struct.singleton_class do
6
+ def keyworded?
7
+ inspect.include? "keyword_init: true"
8
+ end
9
+
10
+ def with_keywords arguments
11
+ keyworded? ? new(arguments) : new.merge!(arguments)
12
+ end
13
+
14
+ def with_positions *values
15
+ keyworded? ? new(Hash[members.zip values]) : new(*values)
16
+ end
17
+ end
18
+
5
19
  refine Struct do
6
20
  def merge **attributes
7
21
  dup.merge! attributes
@@ -11,6 +25,21 @@ module Refinements
11
25
  to_h.merge(attributes).each { |key, value| self[key] = value }
12
26
  self
13
27
  end
28
+
29
+ def revalue attributes = each_pair
30
+ return self unless block_given?
31
+
32
+ dup.tap do |copy|
33
+ attributes.each { |key, value| copy[key] = yield self[key], value }
34
+ end
35
+ end
36
+
37
+ def revalue! attributes = each_pair
38
+ return self unless block_given?
39
+
40
+ attributes.each { |key, value| self[key] = yield self[key], value }
41
+ self
42
+ end
14
43
  end
15
44
  end
16
45
  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.17.0
4
+ version: 7.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brooke Kuhlmann
@@ -28,7 +28,7 @@ cert_chain:
28
28
  2XV8FRa7/JimI07sPLC13eLY3xd/aYTi85Z782KIA4j0G8XEEWAX0ouBhlXPocZv
29
29
  QWc=
30
30
  -----END CERTIFICATE-----
31
- date: 2020-12-13 00:00:00.000000000 Z
31
+ date: 2020-12-21 00:00:00.000000000 Z
32
32
  dependencies: []
33
33
  description:
34
34
  email:
@@ -76,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
78
  requirements: []
79
- rubygems_version: 3.2.0
79
+ rubygems_version: 3.2.2
80
80
  signing_key:
81
81
  specification_version: 4
82
82
  summary: A collection of refinements to core Ruby objects.
metadata.gz.sig CHANGED
Binary file