refinements 8.4.1 → 9.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -27,7 +27,7 @@ module Refinements
27
27
 
28
28
  def pad(value, max: size) = dup.fill(value, size..(max - 1))
29
29
 
30
- def ring(&block) = [last, *self, first].each_cons(3, &block)
30
+ def ring(&) = [last, *self, first].each_cons(3, &)
31
31
  end
32
32
  end
33
33
  end
@@ -42,6 +42,12 @@ module Refinements
42
42
 
43
43
  def deep_symbolize_keys! = replace(deep_symbolize_keys)
44
44
 
45
+ # rubocop:disable Style/MethodDefParentheses
46
+ def fetch_value(key, *default_value, &)
47
+ fetch(key, *default_value, &) || default_value.first
48
+ end
49
+ # rubocop:enable Style/MethodDefParentheses
50
+
45
51
  # :reek:TooManyStatements
46
52
  def flatten_keys prefix: nil, delimiter: "_", cast: :to_sym
47
53
  fail StandardError, "Unknown cast: #{cast}." unless %i[to_sym to_s].include? cast
@@ -52,13 +58,13 @@ module Refinements
52
58
  next flat.merge flat_key.public_send(cast) => value unless value.is_a? self.class
53
59
 
54
60
  flat.merge(
55
- recurse { value.flatten_keys prefix: flat_key, delimiter: delimiter, cast: cast }
61
+ recurse { value.flatten_keys prefix: flat_key, delimiter:, cast: }
56
62
  )
57
63
  end
58
64
  end
59
65
 
60
66
  def flatten_keys! prefix: nil, delimiter: "_", cast: :to_sym
61
- replace flatten_keys(prefix: prefix, delimiter: delimiter, cast: cast)
67
+ replace flatten_keys(prefix:, delimiter:, cast:)
62
68
  end
63
69
 
64
70
  def recurse &block
@@ -5,7 +5,7 @@ module Refinements
5
5
  module Identity
6
6
  NAME = "refinements"
7
7
  LABEL = "Refinements"
8
- VERSION = "8.4.1"
8
+ VERSION = "9.0.0"
9
9
  VERSION_LABEL = "#{LABEL} #{VERSION}".freeze
10
10
  end
11
11
  end
@@ -26,7 +26,7 @@ module Refinements
26
26
 
27
27
  def reread(length = nil, buffer: nil) = tap(&:rewind).read(length, buffer)
28
28
 
29
- def squelch(&block) = self.class.void.then { |void| redirect(void, &block) }
29
+ def squelch(&) = self.class.void.then { |void| redirect(void, &) }
30
30
  end
31
31
  end
32
32
  end
@@ -28,8 +28,12 @@ module Refinements
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,12 +42,16 @@ module Refinements
38
42
  self
39
43
  end
40
44
 
45
+ def deep_touch(...) = make_ancestors.touch(...)
46
+
41
47
  def delete = super && self
42
48
 
43
49
  def directories pattern = "*", flag: File::FNM_SYSCASE
44
50
  glob(pattern, flag).select(&:directory?).sort
45
51
  end
46
52
 
53
+ def empty = file? ? (truncate(0) and self) : remove_tree.make_dir
54
+
47
55
  def extensions = basename.to_s.split(/(?=\.)+/).tap(&:shift)
48
56
 
49
57
  def files(pattern = "*", flag: File::FNM_SYSCASE) = glob(pattern, flag).select(&:file?).sort
@@ -16,39 +16,31 @@ module Refinements
16
16
  .then { |parts| combine parts, :up }
17
17
  end
18
18
 
19
- def down
20
- return self if empty?
21
-
22
- first.downcase + self[1, size]
23
- end
19
+ def down = empty? ? self : first.downcase + self[1, size]
24
20
 
25
- def first number = 0
21
+ def first maximum = 0
26
22
  return self if empty?
23
+ return self[0] if maximum.zero?
24
+ return "" if maximum.negative?
27
25
 
28
- max = Integer number
29
-
30
- return self[0] if max.zero?
31
- return "" if max.negative?
32
-
33
- self[..(max - 1)]
26
+ self[..(maximum - 1)]
34
27
  end
35
28
 
36
29
  def indent multiplier = 1, padding: " "
37
- return self if multiplier.negative?
38
-
39
- (padding * multiplier) + self
30
+ multiplier.negative? ? self : (padding * multiplier) + self
40
31
  end
41
32
 
42
- def last number = 0
33
+ def last minimum = 0
43
34
  return self if empty?
35
+ return self[size - 1] if minimum.zero?
36
+ return "" if minimum.negative?
44
37
 
45
- min = Integer number
38
+ self[(minimum + 1)..]
39
+ end
46
40
 
47
- return self[size - 1] if min.zero?
48
- return "" if min.negative?
41
+ def pluralize(suffix, replace: /$/, count: 0) = count.abs == 1 ? self : sub(replace, suffix)
49
42
 
50
- self[(min + 1)..]
51
- end
43
+ def singularize(suffix, replace: "", count: 1) = count.abs == 1 ? sub(suffix, replace) : self
52
44
 
53
45
  def snakecase
54
46
  return downcase unless match? DELIMITERS
@@ -68,11 +60,7 @@ module Refinements
68
60
 
69
61
  def to_bool = %w[true yes on t y 1].include?(downcase.strip)
70
62
 
71
- def up
72
- return self if empty?
73
-
74
- first.upcase + self[1, size]
75
- end
63
+ def up = empty? ? self : first.upcase + self[1, size]
76
64
 
77
65
  private
78
66
 
@@ -12,10 +12,10 @@ module Refinements
12
12
  end
13
13
 
14
14
  refine Struct do
15
- def merge(**attributes) = dup.merge!(**attributes)
15
+ def merge(object = nil) = dup.merge!(object)
16
16
 
17
- def merge! **attributes
18
- to_h.merge(**attributes).each { |key, value| self[key] = value }
17
+ def merge! object = nil
18
+ to_h.merge(**object.to_h).each { |key, value| self[key] = value }
19
19
  self
20
20
  end
21
21
 
@@ -0,0 +1,14 @@
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
+ # rubocop:disable Style/MethodDefParentheses
8
+ def call(*arguments, &)
9
+ proc { |receiver| receiver.public_send self, *arguments, & }
10
+ end
11
+ # rubocop:enable Style/MethodDefParentheses
12
+ end
13
+ end
14
+ end
data/lib/refinements.rb CHANGED
@@ -10,3 +10,4 @@ require "refinements/pathnames"
10
10
  require "refinements/strings"
11
11
  require "refinements/string_ios"
12
12
  require "refinements/structs"
13
+ 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.4.1
4
+ version: 9.0.0
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-10-03 00:00:00.000000000 Z
31
+ date: 2021-12-26 00:00:00.000000000 Z
32
32
  dependencies: []
33
33
  description:
34
34
  email:
@@ -52,13 +52,15 @@ files:
52
52
  - lib/refinements/string_ios.rb
53
53
  - lib/refinements/strings.rb
54
54
  - lib/refinements/structs.rb
55
+ - lib/refinements/symbols.rb
55
56
  homepage: https://www.alchemists.io/projects/refinements
56
57
  licenses:
57
- - Apache-2.0
58
+ - Hippocratic-3.0
58
59
  metadata:
59
60
  bug_tracker_uri: https://github.com/bkuhlmann/refinements/issues
60
61
  changelog_uri: https://www.alchemists.io/projects/refinements/changes.html
61
62
  documentation_uri: https://www.alchemists.io/projects/refinements
63
+ rubygems_mfa_required: 'true'
62
64
  source_code_uri: https://github.com/bkuhlmann/refinements
63
65
  post_install_message:
64
66
  rdoc_options: []
@@ -68,14 +70,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
68
70
  requirements:
69
71
  - - "~>"
70
72
  - !ruby/object:Gem::Version
71
- version: '3.0'
73
+ version: '3.1'
72
74
  required_rubygems_version: !ruby/object:Gem::Requirement
73
75
  requirements:
74
76
  - - ">="
75
77
  - !ruby/object:Gem::Version
76
78
  version: '0'
77
79
  requirements: []
78
- rubygems_version: 3.2.28
80
+ rubygems_version: 3.3.3
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