refinements 8.2.0 → 8.4.1

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: 5706b329caaea4cbf42c9dd34d8da325d5e4b5628385a760de2ca4b4c5efaf06
4
- data.tar.gz: c20a4aad691df3a683d3ea29d58ae0dbcef4475d297460a945874c2298972864
3
+ metadata.gz: 0b633f00ddb573cba22fcf647e293a7f4544fecc82ad3869d9c3528d0bd8e1aa
4
+ data.tar.gz: 00ef7df42687d801237b8b27563aaa028e19b75a4764c32a3c2220f58186dbe0
5
5
  SHA512:
6
- metadata.gz: d14f810b454fe4e3116dc35b04a3701485b9ddad99889db58da96cec8e8556271ec53292447861748909add9d293a21a7c3c7a49fc16720fd61f7654dc2251a4
7
- data.tar.gz: 61defc07d39c9de58db72ae0f937319694d156c6a0ed2896f6c2a9b645bfbb5fd829e01e96c242764cb4115dc030dbb5ffcb8010446cec34316b7666b62f8061
6
+ metadata.gz: edb16cbce2ab22b6b95303751e87f09037dd570b38ffb7e6620d91a3e8e7bc9293fdcf4a4378e091847b12ccb80bd5d57c5bab22b0d930507327e54c29c9c855
7
+ data.tar.gz: 5767133f63df1a255b0ac9d6f9ff1ab6c8dfc73d237968a3cea225e3d2be4964fd8aa2298c7047cb81a8594ba6a9f4a676738d485519b696efeee20ddfe4b51d
checksums.yaml.gz.sig CHANGED
Binary file
data/README.adoc CHANGED
@@ -11,7 +11,9 @@ 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
- A collection of refinements (enhancements) to primitive Ruby objects.
14
+ Refinements is a collection of enhancements to primitive Ruby objects without needing to resort
15
+ painful and hard to debug monkey patches. These refinements give you additional syntactic sugar to
16
+ build clean and concise implementations all while using less code.
15
17
 
16
18
  toc::[]
17
19
 
@@ -105,13 +107,14 @@ The following sections demonstrate how each refinement enriches your objects wit
105
107
 
106
108
  ===== #compress
107
109
 
108
- Removes `nil` and empty values without mutating itself.
110
+ Removes `nil` and empty objects without mutating itself.
109
111
 
110
112
  [source,ruby]
111
113
  ----
112
- example = ["An", nil, "", "Example"]
113
- example.compress # => ["An", "Example"]
114
- example # => ["An", nil, "", "Example"]
114
+ object = Object.new
115
+ example = [1, "blueberry", nil, "", [], {}, object]
116
+ example.compress # => [1, "blueberry", object]
117
+ example # => [1, "blueberry", nil, "", [], {}, object]
115
118
  ----
116
119
 
117
120
  ===== #compress!
@@ -120,9 +123,10 @@ Removes `nil` and empty values while mutating itself.
120
123
 
121
124
  [source,ruby]
122
125
  ----
123
- example = ["An", nil, "", "Example"]
124
- example.compress! # => ["An", "Example"]
125
- example # => ["An", "Example"]
126
+ object = Object.new
127
+ example = [1, "blueberry", nil, "", [], {}, object]
128
+ example.compress # => [1, "blueberry", object]
129
+ example # => [1, "blueberry", object]
126
130
  ----
127
131
 
128
132
  ===== #excluding
@@ -286,6 +290,30 @@ example = Hash.with_default []
286
290
  example[:b] # => []
287
291
  ----
288
292
 
293
+ ===== #compress
294
+
295
+ Removes `nil` and empty objects without mutating itself.
296
+
297
+ [source,ruby]
298
+ ----
299
+ object = Object.new
300
+ example = {a: 1, b: "blueberry", c: nil, d: "", e: [], f: {}, g: object}
301
+ example.compress # => {a: 1, b: "blueberry", g: object}
302
+ example # => {a: 1, b: "blueberry", c: nil, d: "", e: [], f: {}, g: object}
303
+ ----
304
+
305
+ ===== #compress!
306
+
307
+ Removes `nil` and empty objects while mutating itself.
308
+
309
+ [source,ruby]
310
+ ----
311
+ object = Object.new
312
+ example = {a: 1, b: "blueberry", c: nil, d: "", e: [], f: {}, g: object}
313
+ example.compress! # => {a: 1, b: "blueberry", g: object}
314
+ example # => {a: 1, b: "blueberry", g: object}
315
+ ----
316
+
289
317
  ===== #deep_merge
290
318
 
291
319
  Merges deeply nested hashes together without mutating itself.
@@ -535,7 +563,7 @@ Wraps `Dir.mktmpdir` with the following behavior (see
535
563
  link:https://rubyapi.org/o/Dir.mktmpdir#method-c-mktmpdir[Dir.mktmpdir] for details):
536
564
 
537
565
  * *Without Block* - Answers a newly created Pathname instance which is not automatically cleaned up.
538
- * *With Block* Yields a Pathname instance, answers result of given block, and automatidally cleans
566
+ * *With Block* Yields a Pathname instance, answers result of given block, and automatically cleans
539
567
  up temporary directory after block exits.
540
568
 
541
569
  The following examples use truncated temporary directories for illustration purposes only. In
@@ -614,6 +642,19 @@ Copies file from current location to new location while answering itself so it c
614
642
  Pathname("input.txt").copy Pathname("output.txt") # => Pathname("input.txt")
615
643
  ----
616
644
 
645
+ ===== #delete
646
+
647
+ Deletes file or directory and answers itself so it can be chained.
648
+
649
+ [source,ruby]
650
+ ----
651
+ # When path exists.
652
+ Pathname("/example.txt").touch.delete # => Pathname("/example")
653
+
654
+ # When path doesn't exist.
655
+ Pathname("/example.txt").delete # => Errno::ENOENT
656
+ ----
657
+
617
658
  ===== #directories
618
659
 
619
660
  Answers all directories or filtered directories for current path.
@@ -1,11 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Refinements
4
+ # Provides additional enhancements to the Array primitive.
4
5
  module Arrays
5
6
  refine Array do
6
- def compress = compact.reject(&:empty?)
7
+ def compress = dup.compress!
7
8
 
8
- def compress! = replace(compress)
9
+ def compress!
10
+ compact!
11
+ delete_if { |element| element.respond_to?(:empty?) && element.empty? }
12
+ end
9
13
 
10
14
  def excluding(*elements) = self - elements.flatten
11
15
 
@@ -17,7 +21,7 @@ module Refinements
17
21
 
18
22
  def maximum(key) = map(&key).max
19
23
 
20
- def mean = size.zero? ? 0 : sum(0) / size
24
+ def mean = size.zero? ? 0 : sum(0.0) / size
21
25
 
22
26
  def minimum(key) = map(&key).min
23
27
 
@@ -3,6 +3,7 @@
3
3
  require "bigdecimal"
4
4
 
5
5
  module Refinements
6
+ # Provides additional enhancements to the BigDecimal primitive.
6
7
  module BigDecimals
7
8
  refine BigDecimal do
8
9
  def inspect = format("#<BigDecimal:%{id} %{string}>", id: object_id, string: to_s("F"))
@@ -3,6 +3,7 @@
3
3
  require "date"
4
4
 
5
5
  module Refinements
6
+ # Provides additional enhancements to the DateTime primitive.
6
7
  module DateTimes
7
8
  refine DateTime.singleton_class do
8
9
  def utc = now.new_offset(0)
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Refinements
4
+ # Provides additional enhancements to the Hash primitive.
4
5
  module Hashes
5
6
  refine Hash.singleton_class do
6
7
  def infinite
@@ -11,6 +12,14 @@ module Refinements
11
12
  end
12
13
 
13
14
  refine Hash do
15
+ def compress = dup.compress!
16
+
17
+ def compress!
18
+ return self if empty?
19
+
20
+ compact!.delete_if { |_key, value| value.respond_to?(:empty?) && value.empty? }
21
+ end
22
+
14
23
  def deep_merge other
15
24
  clazz = self.class
16
25
 
@@ -62,13 +71,13 @@ module Refinements
62
71
  end
63
72
  end
64
73
 
65
- def stringify_keys = reduce({}) { |hash, (key, value)| hash.merge key.to_s => value }
74
+ def stringify_keys = transform_keys(&:to_s)
66
75
 
67
- def stringify_keys! = replace(stringify_keys)
76
+ def stringify_keys! = transform_keys!(&:to_s)
68
77
 
69
- def symbolize_keys = reduce({}) { |hash, (key, value)| hash.merge key.to_sym => value }
78
+ def symbolize_keys = transform_keys(&:to_sym)
70
79
 
71
- def symbolize_keys! = replace(symbolize_keys)
80
+ def symbolize_keys! = transform_keys!(&:to_sym)
72
81
 
73
82
  def use &block
74
83
  return [] unless block
@@ -5,7 +5,7 @@ module Refinements
5
5
  module Identity
6
6
  NAME = "refinements"
7
7
  LABEL = "Refinements"
8
- VERSION = "8.2.0"
9
- VERSION_LABEL = "#{LABEL} #{VERSION}"
8
+ VERSION = "8.4.1"
9
+ VERSION_LABEL = "#{LABEL} #{VERSION}".freeze
10
10
  end
11
11
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Refinements
4
+ # Provides additional enhancements to the IO primitive.
4
5
  module IOs
5
6
  refine IO.singleton_class do
6
7
  def void
@@ -3,9 +3,9 @@
3
3
  require "pathname"
4
4
 
5
5
  module Refinements
6
+ # Provides additional enhancements to the Pathname primitive.
6
7
  module Pathnames
7
8
  refine Kernel do
8
- # :reek:UncommunicativeMethodName
9
9
  def Pathname object
10
10
  return super(String(object)) unless object
11
11
 
@@ -24,7 +24,7 @@ module Refinements
24
24
  new(root).files(pattern).each { |path| require path.to_s }
25
25
  end
26
26
 
27
- def root = new("/")
27
+ def root = new(File::SEPARATOR)
28
28
  end
29
29
 
30
30
  refine Pathname do
@@ -38,6 +38,8 @@ module Refinements
38
38
  self
39
39
  end
40
40
 
41
+ def delete = super && self
42
+
41
43
  def directories pattern = "*", flag: File::FNM_SYSCASE
42
44
  glob(pattern, flag).select(&:directory?).sort
43
45
  end
@@ -3,6 +3,7 @@
3
3
  require "stringio"
4
4
 
5
5
  module Refinements
6
+ # Provides additional enhancements to the StringIO primitive.
6
7
  module StringIOs
7
8
  refine StringIO do
8
9
  def reread(length = nil, buffer: nil) = tap(&:rewind).read(length, buffer)
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Refinements
4
+ # Provides additional enhancements to the String primitive.
4
5
  module Strings
5
6
  DELIMITERS = %r([a-z][A-Z]|\s*-\s*|\s*/\s*|\s*:+\s*|\s*_\s*|\s+)
6
7
 
7
8
  refine String do
8
- def blank? = match?(/\A\s*\z/)
9
+ def blank? = empty? || match?(/\A[[:space:]]*\z/)
9
10
 
10
11
  def camelcase
11
12
  return up unless match? DELIMITERS
@@ -35,7 +36,7 @@ module Refinements
35
36
  def indent multiplier = 1, padding: " "
36
37
  return self if multiplier.negative?
37
38
 
38
- padding * multiplier + self
39
+ (padding * multiplier) + self
39
40
  end
40
41
 
41
42
  def last number = 0
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Refinements
4
+ # Provides additional enhancements to the Struct primitive.
4
5
  module Structs
5
6
  refine Struct.singleton_class do
6
7
  def keyworded? = inspect.include?("keyword_init: true")
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: 8.2.0
4
+ version: 8.4.1
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-05-25 00:00:00.000000000 Z
31
+ date: 2021-10-03 00:00:00.000000000 Z
32
32
  dependencies: []
33
33
  description:
34
34
  email:
@@ -75,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
75
  - !ruby/object:Gem::Version
76
76
  version: '0'
77
77
  requirements: []
78
- rubygems_version: 3.2.17
78
+ rubygems_version: 3.2.28
79
79
  signing_key:
80
80
  specification_version: 4
81
81
  summary: A collection of refinements to core Ruby objects.
metadata.gz.sig CHANGED
Binary file