refinements 8.4.1 → 8.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0b633f00ddb573cba22fcf647e293a7f4544fecc82ad3869d9c3528d0bd8e1aa
4
- data.tar.gz: 00ef7df42687d801237b8b27563aaa028e19b75a4764c32a3c2220f58186dbe0
3
+ metadata.gz: 91c5c0e066eac0f4f01a1b28a73f8aa122cb51725ff510fe69466db4f2cd4dac
4
+ data.tar.gz: fc7b29eb6507fe08f2ebedd9a0b596c2bf70b7b9cd48a3b9de12cdae82613928
5
5
  SHA512:
6
- metadata.gz: edb16cbce2ab22b6b95303751e87f09037dd570b38ffb7e6620d91a3e8e7bc9293fdcf4a4378e091847b12ccb80bd5d57c5bab22b0d930507327e54c29c9c855
7
- data.tar.gz: 5767133f63df1a255b0ac9d6f9ff1ab6c8dfc73d237968a3cea225e3d2be4964fd8aa2298c7047cb81a8594ba6a9f4a676738d485519b696efeee20ddfe4b51d
6
+ metadata.gz: b9268d53d65140f5456f6c26521e7f55254480b58cfb30a73d9106aecb7a30977a02d03e8e37d6563904419a1a26d7dfec68f0f64a25f82baf294e817c01ac48
7
+ data.tar.gz: b564b642883464ed667be47ecef73be99ef48665d1f2be8f34ae5ec48b852c6b9a07ee3c4b0c542154a538f54ceaed4e1eca9d528841150469d2c8a5957b6c3e
checksums.yaml.gz.sig CHANGED
Binary file
data/README.adoc CHANGED
@@ -253,6 +253,22 @@ Allows one to inspect a big decimal with numeric representation.
253
253
  BigDecimal.new("5.0E-10").inspect # => "#<BigDecimal:3fd3d458fe84 0.0000000005>"
254
254
  ----
255
255
 
256
+ ==== Class
257
+
258
+ ===== #descendants
259
+
260
+ Answers descendants of a class.
261
+
262
+ [source,ruby]
263
+ ----
264
+ a = Class.new
265
+ b = Class.new a
266
+ c = Class.new a
267
+
268
+ a.descendants # => [b, c]
269
+ Class.new.descendants # => []
270
+ ----
271
+
256
272
  ==== DateTime
257
273
 
258
274
  ===== .utc
@@ -380,6 +396,22 @@ example.deep_symbolize_keys! # => {a: {b: 1}}
380
396
  example # => {a: {b: 1}}
381
397
  ----
382
398
 
399
+ ===== #fetch_value
400
+
401
+ Fetches value for exiting or missing key. Behavior is identical to `#fetch` except when the value of
402
+ the key is `nil` you'll get the default value instead. This eliminates the need for using an _or_
403
+ expression `example.fetch(:desired_key) || "default_value"`.
404
+
405
+ [source,ruby]
406
+ ----
407
+ {a: "test"}.fetch_value :a, "default" # => "test"
408
+ {a: "test"}.fetch_value :a # => "test"
409
+ {a: nil}.fetch_value :a, "default" # => "default"
410
+ {}.fetch_value(:a) { "default" } # => "default"
411
+ {}.fetch_value :a # => KeyError
412
+ {a: "test"}.fetch_value # => ArgumentError
413
+ ----
414
+
383
415
  ===== #flatten_keys
384
416
 
385
417
  Flattens nested keys as top-level keys without mutating itself. Does not handle nested arrays,
@@ -642,6 +674,17 @@ Copies file from current location to new location while answering itself so it c
642
674
  Pathname("input.txt").copy Pathname("output.txt") # => Pathname("input.txt")
643
675
  ----
644
676
 
677
+ ===== #deep_touch
678
+
679
+ Has all of the same functionality as the `#touch` method while being able to create ancestor
680
+ directories no matter how deeply nested the file might be.
681
+
682
+ [source,ruby]
683
+ ----
684
+ Pathname("a/b/c/d.txt").touch # => Pathname("a/b/c/d.txt")
685
+ Pathname("a/b/c/d.txt").touch Time.now - 1 # => Pathname("a/b/c/d.txt")
686
+ ----
687
+
645
688
  ===== #delete
646
689
 
647
690
  Deletes file or directory and answers itself so it can be chained.
@@ -666,6 +709,20 @@ Pathname("/example").directories "a*" # => [Pathname("a")]
666
709
  Pathname("/example").directories flag: File::FNM_DOTMATCH # => [Pathname(".."), Pathname(".")]
667
710
  ----
668
711
 
712
+ ===== #empty
713
+
714
+ Empties a directory of children (i.e. folders, nested folders, or files) or clears an existing file
715
+ of contents. If a directory or file doesn't exist, it will be created.
716
+
717
+ [source,ruby]
718
+ ----
719
+ directory = Pathname("test").make_path
720
+ file = directory.join("test.txt").write("example")
721
+
722
+ file.empty.read # => ""
723
+ directory.empty.children # => []
724
+ ----
725
+
669
726
  ===== #extensions
670
727
 
671
728
  Answers file extensions as an array.
@@ -796,10 +853,13 @@ Pathname("/test.txt").rewrite { |body| body.sub "[token]", "example" } # => Pat
796
853
 
797
854
  ===== #touch
798
855
 
799
- Updates access and modification times for path. Defaults to current time.
856
+ Updates access and modification times for an existing path by defaulting to current time. When path
857
+ doesn't exist, it will be created as a file.
800
858
 
801
859
  [source,ruby]
802
860
  ----
861
+ Pathname("example").touch # => Pathname("example")
862
+ Pathname("example").touch Time.now - 1 # => Pathname("example")
803
863
  Pathname("example.txt").touch # => Pathname("example.txt")
804
864
  Pathname("example.txt").touch Time.now - 1 # => Pathname("example.txt")
805
865
  ----
@@ -878,6 +938,46 @@ Answers last character of a string or last set of characters if given a number.
878
938
  "instant".last 3 # => "ant"
879
939
  ----
880
940
 
941
+ ===== #pluralize
942
+
943
+ Answers plural form of self when given a suffix to add. The plural form of the word can be
944
+ dynamically calculated when given a count and a replacement pattern (i.e. string or regular
945
+ expression) can be supplied for further specificity. Usage is based on
946
+ link:https://en.wikipedia.org/wiki/English_plurals[plurals in English] which may or may not work
947
+ well in other languages.
948
+
949
+ [source,ruby]
950
+ ----
951
+ "apple".pluralize "s" # => apples
952
+ "apple".pluralize "s", count: 0 # => apples
953
+ "apple".pluralize "s", count: 1 # => apple
954
+ "apple".pluralize "s", count: -1 # => apple
955
+ "apple".pluralize "s", count: 2 # => apples
956
+ "apple".pluralize "s", count: -2 # => apples
957
+ "cactus".pluralize "i", replace: "us" # => cacti
958
+ "cul-de-sac".pluralize "ls", replace: "l" # => culs-de-sac
959
+ ----
960
+
961
+ ===== #singularize
962
+
963
+ Answers singular form of self when given a suffix to remove (can be a string or a regular
964
+ expression). The singular form of the word can be dynamically calculated when given a count and a
965
+ replacement string can be supplied for further specificity. Usage is based on
966
+ link:https://en.wikipedia.org/wiki/English_plurals[plurals in English] which may or may not work
967
+ well in other languages.
968
+
969
+ [source,ruby]
970
+ ----
971
+ "apples".singularize "s" # => apple
972
+ "apples".singularize "s", count: 0 # => apples
973
+ "apples".singularize "s", count: 1 # => apple
974
+ "apples".singularize "s", count: -1 # => apple
975
+ "apples".singularize "s", count: 2 # => apples
976
+ "apples".singularize "s", count: -2 # => apples
977
+ "cacti".singularize "i", replace: "us" # => cactus
978
+ "culs-de-sac".singularize "ls", replace: "l" # => cul-de-sac
979
+ ----
980
+
881
981
  ===== #snakecase
882
982
 
883
983
  Answers a snakecased string.
@@ -1072,6 +1172,24 @@ example.revalue! # => #<struct
1072
1172
  example # => #<struct a=1, b=2, c=3>
1073
1173
  ----
1074
1174
 
1175
+ ==== Symbol
1176
+
1177
+ ===== #call
1178
+
1179
+ Enhances symbol-to-proc by allowing you to send additional arguments and/or a block. This only works
1180
+ with public methods in order to not break encapsulation.
1181
+
1182
+ [source,ruby]
1183
+ ----
1184
+ %w[clue crow cow].map(&:tr.call("c", "b")) # => ["blue", "brow", "bow"]
1185
+ [%w[a b c], %w[c a b]].map(&:index.call { |element| element == "b" }) # => [1, 2]
1186
+ %w[1.outside 2.inside].map(&:sub.call(/\./) { |bullet| bullet + " " }) # => ["1. outside", "2. inside"]
1187
+ [1, 2, 3].map(&:to_s.call) # => ["1", "2", "3"]
1188
+ ----
1189
+
1190
+ ⚠️ Use of `#call` without any arguments or block should be avoided in order to not incur extra
1191
+ processing costs since the original symbol-to-proc call can used instead.
1192
+
1075
1193
  == Development
1076
1194
 
1077
1195
  To contribute, run:
@@ -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
@@ -42,6 +42,10 @@ module Refinements
42
42
 
43
43
  def deep_symbolize_keys! = replace(deep_symbolize_keys)
44
44
 
45
+ def fetch_value key, *default_value, &block
46
+ fetch(key, *default_value, &block) || default_value.first
47
+ end
48
+
45
49
  # :reek:TooManyStatements
46
50
  def flatten_keys prefix: nil, delimiter: "_", cast: :to_sym
47
51
  fail StandardError, "Unknown cast: #{cast}." unless %i[to_sym to_s].include? cast
@@ -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 = "8.5.0"
9
9
  VERSION_LABEL = "#{LABEL} #{VERSION}".freeze
10
10
  end
11
11
  end
@@ -38,12 +38,16 @@ module Refinements
38
38
  self
39
39
  end
40
40
 
41
+ def deep_touch(...) = make_ancestors.touch(...)
42
+
41
43
  def delete = super && self
42
44
 
43
45
  def directories pattern = "*", flag: File::FNM_SYSCASE
44
46
  glob(pattern, flag).select(&:directory?).sort
45
47
  end
46
48
 
49
+ def empty = file? ? (truncate(0) and self) : remove_tree.make_dir
50
+
47
51
  def extensions = basename.to_s.split(/(?=\.)+/).tap(&:shift)
48
52
 
49
53
  def files(pattern = "*", flag: File::FNM_SYSCASE) = glob(pattern, flag).select(&:file?).sort
@@ -50,6 +50,10 @@ module Refinements
50
50
  self[(min + 1)..]
51
51
  end
52
52
 
53
+ def pluralize(suffix, replace: /$/, count: 0) = count.abs == 1 ? self : sub(replace, suffix)
54
+
55
+ def singularize(suffix, replace: "", count: 1) = count.abs == 1 ? sub(suffix, replace) : self
56
+
53
57
  def snakecase
54
58
  return downcase unless match? DELIMITERS
55
59
 
@@ -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.4.1
4
+ version: 8.5.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-10-16 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.28
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