refinements 12.6.0 → 12.7.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: e32b598b948abb097125834b408d41c1fa4313a671615b89475c101089fbb870
4
- data.tar.gz: 9fb3a2380ac0effbe2ac0ca5045e736b9730b2c57162c927f3f0577d04e4da81
3
+ metadata.gz: d95a713a69dbbeaec7c6c2f76c634f576d763e86d32b1f46bad9817ac6eacd70
4
+ data.tar.gz: 5d972fa9193dab8743c5ed1aa7980a9b11be3b34e951f885377d25742d791eb1
5
5
  SHA512:
6
- metadata.gz: dd4fcab10575c3b0933edbf5290b29fbd2498225f40ff038a626d72dda70462f5b24f1ae76f885d7be5134953d050336024de70bdd3d8545010a3fd4d173e4b3
7
- data.tar.gz: 1c2a09c4a735be01b6d471548a1621f09d75c394c0a6afcea599cce7c812fb9d2ee4e0510ff8e24cd9d0c6110233d1d764bbc0767b81c287b849ab80241eb826
6
+ metadata.gz: cec359f8d82a7e98889f2913e8330857d15f9d7a99ae0024e3895b6b4ec61c7a6c6a17897fb19b3ae1f10224d02be2affca6c81093d7965c6acfdb58d0b5c535
7
+ data.tar.gz: b967aed2a285c8ca49b5ef6a54f830721a19ccdc7521ac7857bedcec849b781e7b6a357b770c4f99cd47fe4197f2f0b2cbecd149e89096cd4193c841a64dc623
checksums.yaml.gz.sig CHANGED
Binary file
data/README.adoc CHANGED
@@ -13,16 +13,19 @@ toc::[]
13
13
  Enhances the following objects:
14
14
 
15
15
  * Array
16
+ * Binding
16
17
  * Data
17
18
  * DateTime
18
19
  * Hash
19
20
  * IO
20
21
  * LogDevice
21
22
  * Logger
23
+ * Object
22
24
  * Pathname
23
25
  * String
24
26
  * StringIO
25
27
  * Struct
28
+ * Symbol
26
29
 
27
30
  == Requirements
28
31
 
@@ -77,12 +80,14 @@ gem "refinements", require: false
77
80
  [source,ruby]
78
81
  ----
79
82
  require "refinements/array"
83
+ require "refinements/binding"
80
84
  require "refinements/data"
81
85
  require "refinements/date_time"
82
86
  require "refinements/hash"
83
87
  require "refinements/io"
84
88
  require "refinements/log_device"
85
89
  require "refinements/logger"
90
+ require "refinements/object"
86
91
  require "refinements/pathname"
87
92
  require "refinements/string"
88
93
  require "refinements/string_io"
@@ -99,12 +104,14 @@ refinement(s):
99
104
  ----
100
105
  class Example
101
106
  using Refinements::Array
107
+ using Refinements::Binding
102
108
  using Refinements::Data
103
109
  using Refinements::DateTime
104
110
  using Refinements::Hash
105
111
  using Refinements::IO
106
112
  using Refinements::LogDevice
107
113
  using Refinements::Logger
114
+ using Refinements::Object
108
115
  using Refinements::Pathname
109
116
  using Refinements::String
110
117
  using Refinements::StringIO
@@ -394,6 +401,59 @@ Further enhances `#to_sentence` by answering a sentence where all elements are i
394
401
 
395
402
  💡 You can use a string or a symbol for the conjunction (i.e. `"and"` or `:and`).
396
403
 
404
+ ==== Binding
405
+
406
+ ===== #[]
407
+
408
+ Allows you to obtain a local variable. This is an alias to `#local_variable_get`.
409
+
410
+ [source,ruby]
411
+ ----
412
+ a = 1
413
+ binding[:a] # 1
414
+ binding[:bogus] # `bogus' is not defined (NameError)
415
+ ----
416
+
417
+ ===== #[]=
418
+
419
+ Allows you to set a local variable. This is an alias to `#local_variable_set`.
420
+
421
+ [source,ruby]
422
+ ----
423
+ a = 1
424
+ binding[:a] = 5
425
+ binding[:bogus] = "bad"
426
+
427
+ binding[:a] # 5
428
+ binding[:bogus] # # `bogus' is not defined (NameError)
429
+ ----
430
+
431
+ ===== #local?
432
+
433
+ Allows you to check if local variable is defined. This is an alias to `#local_variable_defined?`.
434
+
435
+ [source,ruby]
436
+ ----
437
+ a = 1
438
+
439
+ binding.local? :a # true
440
+ binding.local? :b # false
441
+ ----
442
+
443
+ ===== #locals
444
+
445
+ Allows you to acquire all locally defined variables. This is an alias to `#local_variables`.
446
+
447
+ [source,ruby]
448
+ ----
449
+ binding.locals # []
450
+
451
+ a = 1
452
+ b = 2
453
+
454
+ binding.locals # [:a, :b]
455
+ ----
456
+
397
457
  ==== Data
398
458
 
399
459
  ===== #diff
@@ -875,6 +935,55 @@ logger.any { "Test." }
875
935
  A, [2000-01-10T09:00:00.330719 #44925] ANY -- : Test.
876
936
  ```
877
937
 
938
+ ==== Object
939
+
940
+ ===== #in?
941
+
942
+ Allows you to know if `self` is included in, or an element of, the target object.
943
+
944
+ [source,ruby]
945
+ ----
946
+ 1.in? [1, 2, 3] # true
947
+ 9.in? [1, 2, 3] # false
948
+
949
+ "a".in? %w[a b c] # true
950
+ "z".in? %w[a b c] # false
951
+
952
+ :a.in? %i[a b c] # true
953
+ :z.in? %i[a b c] # false
954
+
955
+ :a.in? %i[a b c].to_enum # true
956
+ :z.in? %i[a b c].to_enum # false
957
+
958
+ :a.in?({a: 1, b: 2, c: 3}) # true
959
+ :z.in?({a: 1, b: 2, c: 3}) # false
960
+
961
+ 1.in? 1..5 # true
962
+ 9.in? 1..5 # false
963
+
964
+ 1.in? Set[1, 2, 3] # true
965
+ 9.in? Set[1, 2, 3] # false
966
+
967
+ "a".in? "abcde" # true
968
+ "z".in? "abcde" # false
969
+
970
+ "z".in? Object.new # `String#include?` must be implemented. (NoMethodError)
971
+ ----
972
+
973
+ ===== #to_proc
974
+
975
+ Allows you to cast any object to a proc.
976
+
977
+ [source,ruby]
978
+ ----
979
+ one = Class.new { def call = :test }
980
+ .new
981
+ two = Object.new
982
+
983
+ one.to_proc # #<Proc:0x0000000124019580 (lambda)>
984
+ two.to_proc # `Object#call` must be implemented. (NoMethodError)
985
+ ----
986
+
878
987
  ==== Pathname
879
988
 
880
989
  ===== Pathname
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "refinements/shared/enumerables/many"
3
+ require "refinements/shared/many"
4
4
 
5
5
  module Refinements
6
6
  # Provides additional enhancements to the Array primitive.
7
7
  module Array
8
8
  refine ::Array do
9
- import_methods Shared::Enumerables::Many
9
+ import_methods Shared::Many
10
10
 
11
11
  def combinatorial?(other) = !other.empty? && size == union(other).size
12
12
 
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Refinements
4
+ # Provides additional enhancements to the Binding class.
5
+ module Binding
6
+ refine ::Binding do
7
+ alias_method :[], :local_variable_get
8
+ alias_method :[]=, :local_variable_set
9
+ alias_method :local?, :local_variable_defined?
10
+ alias_method :locals, :local_variables
11
+ end
12
+ end
13
+ end
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "refinements/shared/values/diff"
3
+ require "refinements/shared/diff"
4
4
 
5
5
  module Refinements
6
6
  # Provides additional enhancements to the Struct primitive.
7
7
  module Data
8
8
  refine ::Data do
9
- import_methods Shared::Values::Diff
9
+ import_methods Shared::Diff
10
10
  end
11
11
  end
12
12
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "refinements/shared/enumerables/many"
3
+ require "refinements/shared/many"
4
4
 
5
5
  module Refinements
6
6
  # Provides additional enhancements to the Hash primitive.
@@ -12,7 +12,7 @@ module Refinements
12
12
  end
13
13
 
14
14
  refine ::Hash do
15
- import_methods Shared::Enumerables::Many
15
+ import_methods Shared::Many
16
16
 
17
17
  def compress = compact.delete_if { |_key, value| value.respond_to?(:empty?) && value.empty? }
18
18
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "refinements/shared/ios/reread"
3
+ require "refinements/shared/reread"
4
4
 
5
5
  module Refinements
6
6
  # Provides additional enhancements to the IO primitive.
@@ -17,7 +17,7 @@ module Refinements
17
17
  end
18
18
 
19
19
  refine ::IO do
20
- import_methods Shared::IOs::Reread
20
+ import_methods Shared::Reread
21
21
 
22
22
  def redirect other
23
23
  return self unless block_given?
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Refinements
4
+ # Provides additional enhancements to the Object class.
5
+ module Object
6
+ refine ::Object do
7
+ def in? other
8
+ case other
9
+ when Range then other.cover? self
10
+ when ::Array, Enumerable, ::Hash, Set, ::String then other.include? self
11
+ else fail NoMethodError, "`#{self.class}#include?` must be implemented."
12
+ end
13
+ end
14
+
15
+ def to_proc
16
+ return method(:call).to_proc if respond_to? :call
17
+
18
+ fail NoMethodError, "`#{self.class}#call` must be implemented."
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Refinements
4
+ module Shared
5
+ # Provides functionality for knowing the difference between two whole value objects.
6
+ module Diff
7
+ def diff other
8
+ if other.is_a? self.class
9
+ to_h.merge(other.to_h) { |_, one, two| [one, two].uniq }
10
+ .select { |_, diff| diff.size == 2 }
11
+ else
12
+ to_h.each.with_object({}) { |(key, value), diff| diff[key] = [value, nil] }
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Refinements
4
+ module Shared
5
+ # Provides functionality for knowing whether an enumerable has many elements or not.
6
+ module Many
7
+ def many?
8
+ return size > 1 unless block_given?
9
+
10
+ total = reduce(0) { |count, item| yield(item) ? count + 1 : count }
11
+ total > 1
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Refinements
4
+ module Shared
5
+ # Provides functionality for I/O object rewinding.
6
+ module Reread
7
+ def reread(length = nil, buffer: nil) = tap(&:rewind).read(length, buffer)
8
+ end
9
+ end
10
+ end
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "refinements/shared/ios/reread"
3
+ require "refinements/shared/reread"
4
4
  require "stringio"
5
5
 
6
6
  module Refinements
7
7
  # Provides additional enhancements to the StringIO primitive.
8
8
  module StringIO
9
9
  refine ::StringIO do
10
- import_methods Shared::IOs::Reread
10
+ import_methods Shared::Reread
11
11
  end
12
12
  end
13
13
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "refinements/shared/values/diff"
3
+ require "refinements/shared/diff"
4
4
 
5
5
  module Refinements
6
6
  # Provides additional enhancements to the Struct primitive.
@@ -10,7 +10,7 @@ module Refinements
10
10
  end
11
11
 
12
12
  refine ::Struct do
13
- import_methods Shared::Values::Diff
13
+ import_methods Shared::Diff
14
14
 
15
15
  def merge(...) = dup.merge!(...)
16
16
 
data/lib/refinements.rb CHANGED
@@ -1,12 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "refinements/array"
4
+ require "refinements/binding"
4
5
  require "refinements/data"
5
6
  require "refinements/date_time"
6
7
  require "refinements/hash"
7
8
  require "refinements/io"
8
9
  require "refinements/log_device"
9
10
  require "refinements/logger"
11
+ require "refinements/object"
10
12
  require "refinements/pathname"
11
13
  require "refinements/string"
12
14
  require "refinements/string_io"
data/refinements.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "refinements"
5
- spec.version = "12.6.0"
5
+ spec.version = "12.7.0"
6
6
  spec.authors = ["Brooke Kuhlmann"]
7
7
  spec.email = ["brooke@alchemists.io"]
8
8
  spec.homepage = "https://alchemists.io/projects/refinements"
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.6.0
4
+ version: 12.7.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-07-13 00:00:00.000000000 Z
38
+ date: 2024-07-15 00:00:00.000000000 Z
39
39
  dependencies: []
40
40
  description:
41
41
  email:
@@ -50,16 +50,18 @@ files:
50
50
  - README.adoc
51
51
  - lib/refinements.rb
52
52
  - lib/refinements/array.rb
53
+ - lib/refinements/binding.rb
53
54
  - lib/refinements/data.rb
54
55
  - lib/refinements/date_time.rb
55
56
  - lib/refinements/hash.rb
56
57
  - lib/refinements/io.rb
57
58
  - lib/refinements/log_device.rb
58
59
  - lib/refinements/logger.rb
60
+ - lib/refinements/object.rb
59
61
  - lib/refinements/pathname.rb
60
- - lib/refinements/shared/enumerables/many.rb
61
- - lib/refinements/shared/ios/reread.rb
62
- - lib/refinements/shared/values/diff.rb
62
+ - lib/refinements/shared/diff.rb
63
+ - lib/refinements/shared/many.rb
64
+ - lib/refinements/shared/reread.rb
63
65
  - lib/refinements/string.rb
64
66
  - lib/refinements/string_io.rb
65
67
  - lib/refinements/struct.rb
metadata.gz.sig CHANGED
Binary file
@@ -1,17 +0,0 @@
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
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Refinements
4
- module Shared
5
- module IOs
6
- # Provides shared functionality for I/O object rewinding.
7
- module Reread
8
- def reread(length = nil, buffer: nil) = tap(&:rewind).read(length, buffer)
9
- end
10
- end
11
- end
12
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Refinements
4
- module Shared
5
- module Values
6
- # Provides shared whole value functionality for knowing the difference between two objects.
7
- module Diff
8
- def diff other
9
- if other.is_a? self.class
10
- to_h.merge(other.to_h) { |_, one, two| [one, two].uniq }
11
- .select { |_, diff| diff.size == 2 }
12
- else
13
- to_h.each.with_object({}) { |(key, value), diff| diff[key] = [value, nil] }
14
- end
15
- end
16
- end
17
- end
18
- end
19
- end