refinements 8.2.2 → 8.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
 
@@ -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"))
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Refinements
4
+ # Provides additional enhancements to Class objects.
5
+ module Classes
6
+ refine Class do
7
+ def descendants
8
+ ObjectSpace.each_object(singleton_class)
9
+ .reject { |klass| klass.singleton_class? || klass == self }
10
+ end
11
+ end
12
+ end
13
+ end
@@ -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
 
@@ -33,6 +42,10 @@ module Refinements
33
42
 
34
43
  def deep_symbolize_keys! = replace(deep_symbolize_keys)
35
44
 
45
+ def fetch_value key, *default_value, &block
46
+ fetch(key, *default_value, &block) || default_value.first
47
+ end
48
+
36
49
  # :reek:TooManyStatements
37
50
  def flatten_keys prefix: nil, delimiter: "_", cast: :to_sym
38
51
  fail StandardError, "Unknown cast: #{cast}." unless %i[to_sym to_s].include? cast
@@ -62,13 +75,13 @@ module Refinements
62
75
  end
63
76
  end
64
77
 
65
- def stringify_keys = reduce({}) { |hash, (key, value)| hash.merge key.to_s => value }
78
+ def stringify_keys = transform_keys(&:to_s)
66
79
 
67
- def stringify_keys! = replace(stringify_keys)
80
+ def stringify_keys! = transform_keys!(&:to_s)
68
81
 
69
- def symbolize_keys = reduce({}) { |hash, (key, value)| hash.merge key.to_sym => value }
82
+ def symbolize_keys = transform_keys(&:to_sym)
70
83
 
71
- def symbolize_keys! = replace(symbolize_keys)
84
+ def symbolize_keys! = transform_keys!(&:to_sym)
72
85
 
73
86
  def use &block
74
87
  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.2"
8
+ VERSION = "8.5.1"
9
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,12 +24,16 @@ 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
31
- def change_dir &block
32
- block ? Dir.chdir(self, &block) : (Dir.chdir self and self)
31
+ def change_dir
32
+ if block_given?
33
+ Dir.chdir(self) { |path| yield Pathname(path) }
34
+ else
35
+ Dir.chdir self and self
36
+ end
33
37
  end
34
38
 
35
39
  def copy to
@@ -38,10 +42,16 @@ module Refinements
38
42
  self
39
43
  end
40
44
 
45
+ def deep_touch(...) = make_ancestors.touch(...)
46
+
47
+ def delete = super && self
48
+
41
49
  def directories pattern = "*", flag: File::FNM_SYSCASE
42
50
  glob(pattern, flag).select(&:directory?).sort
43
51
  end
44
52
 
53
+ def empty = file? ? (truncate(0) and self) : remove_tree.make_dir
54
+
45
55
  def extensions = basename.to_s.split(/(?=\.)+/).tap(&:shift)
46
56
 
47
57
  def files(pattern = "*", flag: File::FNM_SYSCASE) = glob(pattern, flag).select(&:file?).sort
@@ -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
@@ -15,39 +16,31 @@ module Refinements
15
16
  .then { |parts| combine parts, :up }
16
17
  end
17
18
 
18
- def down
19
- return self if empty?
20
-
21
- first.downcase + self[1, size]
22
- end
19
+ def down = empty? ? self : first.downcase + self[1, size]
23
20
 
24
- def first number = 0
21
+ def first maximum = 0
25
22
  return self if empty?
23
+ return self[0] if maximum.zero?
24
+ return "" if maximum.negative?
26
25
 
27
- max = Integer number
28
-
29
- return self[0] if max.zero?
30
- return "" if max.negative?
31
-
32
- self[..(max - 1)]
26
+ self[..(maximum - 1)]
33
27
  end
34
28
 
35
29
  def indent multiplier = 1, padding: " "
36
- return self if multiplier.negative?
37
-
38
- padding * multiplier + self
30
+ multiplier.negative? ? self : (padding * multiplier) + self
39
31
  end
40
32
 
41
- def last number = 0
33
+ def last minimum = 0
42
34
  return self if empty?
35
+ return self[size - 1] if minimum.zero?
36
+ return "" if minimum.negative?
43
37
 
44
- min = Integer number
38
+ self[(minimum + 1)..]
39
+ end
45
40
 
46
- return self[size - 1] if min.zero?
47
- return "" if min.negative?
41
+ def pluralize(suffix, replace: /$/, count: 0) = count.abs == 1 ? self : sub(replace, suffix)
48
42
 
49
- self[(min + 1)..]
50
- end
43
+ def singularize(suffix, replace: "", count: 1) = count.abs == 1 ? sub(suffix, replace) : self
51
44
 
52
45
  def snakecase
53
46
  return downcase unless match? DELIMITERS
@@ -67,11 +60,7 @@ module Refinements
67
60
 
68
61
  def to_bool = %w[true yes on t y 1].include?(downcase.strip)
69
62
 
70
- def up
71
- return self if empty?
72
-
73
- first.upcase + self[1, size]
74
- end
63
+ def up = empty? ? self : first.upcase + self[1, size]
75
64
 
76
65
  private
77
66
 
@@ -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")
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Refinements
4
+ # Provides additional enhancements to the Symbol primitive.
5
+ module Symbols
6
+ refine Symbol do
7
+ def call *arguments, &block
8
+ proc { |receiver| receiver.public_send self, *arguments, &block }
9
+ end
10
+ end
11
+ end
12
+ end
data/lib/refinements.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "refinements/identity"
4
4
  require "refinements/arrays"
5
+ require "refinements/classes"
5
6
  require "refinements/big_decimals"
6
7
  require "refinements/date_times"
7
8
  require "refinements/hashes"
@@ -10,3 +11,4 @@ require "refinements/pathnames"
10
11
  require "refinements/strings"
11
12
  require "refinements/string_ios"
12
13
  require "refinements/structs"
14
+ require "refinements/symbols"
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.2
4
+ version: 8.5.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-09-06 00:00:00.000000000 Z
31
+ date: 2021-10-21 00:00:00.000000000 Z
32
32
  dependencies: []
33
33
  description:
34
34
  email:
@@ -44,6 +44,7 @@ files:
44
44
  - lib/refinements.rb
45
45
  - lib/refinements/arrays.rb
46
46
  - lib/refinements/big_decimals.rb
47
+ - lib/refinements/classes.rb
47
48
  - lib/refinements/date_times.rb
48
49
  - lib/refinements/hashes.rb
49
50
  - lib/refinements/identity.rb
@@ -52,6 +53,7 @@ files:
52
53
  - lib/refinements/string_ios.rb
53
54
  - lib/refinements/strings.rb
54
55
  - lib/refinements/structs.rb
56
+ - lib/refinements/symbols.rb
55
57
  homepage: https://www.alchemists.io/projects/refinements
56
58
  licenses:
57
59
  - Apache-2.0
@@ -75,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
77
  - !ruby/object:Gem::Version
76
78
  version: '0'
77
79
  requirements: []
78
- rubygems_version: 3.2.27
80
+ rubygems_version: 3.2.29
79
81
  signing_key:
80
82
  specification_version: 4
81
83
  summary: A collection of refinements to core Ruby objects.
metadata.gz.sig CHANGED
Binary file