facets 4.0.0 → 4.1.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 +4 -4
- data/CONTRIBUTING.md +18 -2
- data/HISTORY.md +205 -0
- data/README.md +8 -2
- data/lib/core/facets/array/split.rb +16 -9
- data/lib/core/facets/class/descendants.rb +21 -8
- data/lib/core/facets/enumerable/defer.rb +4 -1
- data/lib/core/facets/enumerable/graph.rb +1 -1
- data/lib/core/facets/enumerable/map_by.rb +6 -2
- data/lib/core/facets/hash/count.rb +19 -8
- data/lib/core/facets/hash/deep_merge.rb +25 -20
- data/lib/core/facets/integer/ordinal.rb +18 -13
- data/lib/core/facets/kernel/blank.rb +9 -4
- data/lib/core/facets/kernel/deep_clone.rb +36 -8
- data/lib/core/facets/kernel/extend.rb +20 -4
- data/lib/core/facets/module/alias_class_method.rb +8 -3
- data/lib/core/facets/module/module_def.rb +0 -11
- data/lib/core/facets/module/redefine_method.rb +12 -3
- data/lib/core/facets/range/overlap.rb +32 -1
- data/lib/core/facets/string/indent.rb +32 -20
- data/lib/core/facets/string/remove.rb +13 -8
- data/lib/core/facets/string/squish.rb +5 -3
- data/lib/core/facets/string.rb +1 -0
- data/lib/core/facets/time/change.rb +50 -28
- data/lib/core/facets/time/future.rb +6 -2
- data/lib/core/facets/time/round_to.rb +1 -1
- data/lib/core/facets/time/to_time.rb +3 -7
- data/lib/core/facets/version.rb +1 -1
- data/lib/standard/facets/cloneable.rb +4 -3
- data/lib/standard/facets/digest/salted_digest.rb +6 -2
- data/lib/standard/facets/fileutils/ln_r.rb +7 -56
- data/lib/standard/facets/find/select.rb +3 -1
- data/lib/standard/facets/interval.rb +1 -1
- data/lib/standard/facets/math/percentile.rb +9 -4
- data/lib/standard/facets/ostruct/initialize.rb +7 -6
- data/lib/standard/facets/ostruct.rb +3 -3
- data/lib/standard/facets/pathname/glob.rb +11 -7
- data/lib/standard/facets/shellwords.rb +59 -15
- data/lib/standard/facets/zlib.rb +5 -9
- metadata +1 -1
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# Facets' extensions to Ruby's OpenStruct.
|
|
2
2
|
#
|
|
3
|
-
# NOTE: `ostruct` is deprecated as of Ruby 3.4 and
|
|
4
|
-
#
|
|
5
|
-
#
|
|
3
|
+
# NOTE: `ostruct` is deprecated as of Ruby 3.4, and as of Ruby 4.0 it is a
|
|
4
|
+
# bundled gem rather than a default gem. Ruby still installs it, but a Bundler
|
|
5
|
+
# project must list `ostruct` in its Gemfile or gemspec to load it. The gem
|
|
6
6
|
# itself still exists, so these extensions continue to work on top of it.
|
|
7
7
|
#
|
|
8
8
|
# Given OpenStruct's deprecation, Facets may divest these extensions in a
|
|
@@ -1,5 +1,10 @@
|
|
|
1
|
+
require 'pathname'
|
|
2
|
+
|
|
1
3
|
class Pathname
|
|
2
4
|
|
|
5
|
+
alias_method :_glob_original, :glob
|
|
6
|
+
private :_glob_original
|
|
7
|
+
|
|
3
8
|
# Extends Ruby's built-in Pathname#glob to accept symbol-based
|
|
4
9
|
# flags in addition to the standard File::FNM_* constants.
|
|
5
10
|
#
|
|
@@ -7,15 +12,14 @@ class Pathname
|
|
|
7
12
|
#
|
|
8
13
|
# Supported symbols: :noescape, :pathname, :dotmatch, :casefold
|
|
9
14
|
#
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def glob(match, *opts)
|
|
15
|
+
# Otherwise it is Ruby's own Pathname#glob. With a block, it gives the
|
|
16
|
+
# block each match and returns nil.
|
|
17
|
+
#
|
|
18
|
+
def glob(match, *opts, &block)
|
|
14
19
|
if opts.any? { |o| o.is_a?(Symbol) || o.is_a?(String) }
|
|
15
|
-
|
|
16
|
-
Dir.glob(::File.join(self.to_s, match), flags).map { |m| self.class.new(m) }
|
|
20
|
+
_glob_original(match, glob_flags(opts), &block)
|
|
17
21
|
else
|
|
18
|
-
_glob_original(match, *opts)
|
|
22
|
+
_glob_original(match, *opts, &block)
|
|
19
23
|
end
|
|
20
24
|
end
|
|
21
25
|
|
|
@@ -4,21 +4,17 @@ module Shellwords
|
|
|
4
4
|
|
|
5
5
|
module_function
|
|
6
6
|
|
|
7
|
-
# Escape
|
|
8
|
-
#
|
|
7
|
+
# Escape backslash, tab, space, and the characters | & ` < > ( ) ' "
|
|
8
|
+
# with a backslash.
|
|
9
9
|
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
10
|
+
# Unlike Ruby's Shellwords.escape, this leaves other shell
|
|
11
|
+
# metacharacters alone, including $ (so shell variables still
|
|
12
|
+
# expand), ; * ? ~ and newlines. It is not safe for untrusted
|
|
13
|
+
# input: use Shellwords.escape for that.
|
|
12
14
|
def alt_escape(cmdline)
|
|
13
15
|
cmdline.gsub(/([\\\t\| &`<>)('"])/) { |s| '\\' << s }
|
|
14
16
|
end
|
|
15
17
|
|
|
16
|
-
unless method_defined?(:escape)
|
|
17
|
-
def escape(cmdline)
|
|
18
|
-
cmdline.gsub(/([\\\t\| &`<>)('"])\$/) { |s| '\\' << s }
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
|
|
22
18
|
# Escape special character used in DOS-based shells.
|
|
23
19
|
#
|
|
24
20
|
# TODO: How to integrate with rest of system?
|
|
@@ -71,21 +67,65 @@ class Array
|
|
|
71
67
|
# The array is accepted in the format of Ruby
|
|
72
68
|
# method arguments --ie. [arg1, arg2, ..., hash]
|
|
73
69
|
#
|
|
70
|
+
# ['a', 'b', {d: true}].shellwords #=> ["-d", "a", "b"]
|
|
71
|
+
#
|
|
72
|
+
# To make a command line of them, join them with Ruby's Array#shelljoin:
|
|
73
|
+
#
|
|
74
|
+
# ['a', 'b', {d: true}].shellwords.shelljoin #=> "-d a b"
|
|
75
|
+
#
|
|
74
76
|
def shellwords
|
|
75
77
|
opts, args = *flatten.partition{ |e| Hash === e }
|
|
76
78
|
opts = opts.inject({}){ |m,h| m.update(h); m }
|
|
77
79
|
opts.shellwords + args
|
|
78
80
|
end
|
|
79
81
|
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
module Facets
|
|
85
|
+
|
|
86
|
+
# Facets' Array#shelljoin used to convert the array with #shellwords first,
|
|
87
|
+
# which changed Ruby's own Array#shelljoin: nested arrays were flattened
|
|
88
|
+
# and hashes became options, so structured input, such as a web request's
|
|
89
|
+
# parameters, could add arguments to a command. Ruby's version escapes
|
|
90
|
+
# each element as one argument. Facets now leaves it alone, and only
|
|
91
|
+
# warns when the old Facets version would have given a different result.
|
|
80
92
|
#
|
|
81
|
-
|
|
82
|
-
|
|
93
|
+
# @deprecated Scheduled for removal after 2027-09-30.
|
|
94
|
+
module ArrayShelljoin
|
|
95
|
+
def shelljoin
|
|
96
|
+
if any? { |e| Hash === e || Array === e }
|
|
97
|
+
warn "Facets' Array#shelljoin is deprecated. Array#shelljoin now escapes each element, " \
|
|
98
|
+
"as Ruby's own does, so hashes and nested arrays are no longer turned into options. " \
|
|
99
|
+
"To get those, use array.shellwords.shelljoin. It will be removed after 2027-09-30.", uplevel: 1
|
|
100
|
+
end
|
|
101
|
+
super
|
|
102
|
+
end
|
|
83
103
|
end
|
|
84
104
|
|
|
85
105
|
end
|
|
86
106
|
|
|
107
|
+
Array.prepend(Facets::ArrayShelljoin)
|
|
108
|
+
|
|
87
109
|
class Hash
|
|
88
110
|
|
|
111
|
+
# Convert a hash into command line flags, one word per flag.
|
|
112
|
+
#
|
|
113
|
+
# {debug: true, name: 'jim', v: true}.shellwords
|
|
114
|
+
# #=> ["--debug", "--name=jim", "-v"]
|
|
115
|
+
#
|
|
116
|
+
# A one-letter key gives a short flag (`-v`) and a longer key a long one
|
|
117
|
+
# (`--debug`). A value of `true` gives the bare flag, and `false` or `nil`
|
|
118
|
+
# leaves the flag out. An array value repeats the flag for each element.
|
|
119
|
+
# Any other value is joined to the flag with `=`.
|
|
120
|
+
#
|
|
121
|
+
# {I: ['lib', 'test'], quiet: false}.shellwords
|
|
122
|
+
# #=> ["-I=lib", "-I=test"]
|
|
123
|
+
#
|
|
124
|
+
# Each flag and its value is one word, unquoted, ready to pass to
|
|
125
|
+
# `system(*words)` or to join with Ruby's Array#shelljoin, which escapes
|
|
126
|
+
# what the shell needs.
|
|
127
|
+
#
|
|
128
|
+
# {name: 'a b'}.shellwords.shelljoin #=> "--name\\=a\\ b"
|
|
89
129
|
#
|
|
90
130
|
def shellwords
|
|
91
131
|
argv = []
|
|
@@ -95,17 +135,21 @@ class Hash
|
|
|
95
135
|
when false, nil
|
|
96
136
|
when Array
|
|
97
137
|
v.each do |e|
|
|
98
|
-
argv <<
|
|
138
|
+
argv << "#{m}#{f}=#{e}"
|
|
99
139
|
end
|
|
100
140
|
when true
|
|
101
|
-
argv <<
|
|
141
|
+
argv << "#{m}#{f}"
|
|
102
142
|
else
|
|
103
|
-
argv <<
|
|
143
|
+
argv << "#{m}#{f}=#{v}"
|
|
104
144
|
end
|
|
105
145
|
end
|
|
106
146
|
argv
|
|
107
147
|
end
|
|
108
148
|
|
|
149
|
+
# Convert a hash into command line flags, escaped and joined into one
|
|
150
|
+
# string. See Hash#shellwords.
|
|
151
|
+
#
|
|
152
|
+
# {debug: true, name: 'jim'}.shelljoin #=> "--debug --name\\=jim"
|
|
109
153
|
#
|
|
110
154
|
def shelljoin
|
|
111
155
|
shellwords.shelljoin
|
data/lib/standard/facets/zlib.rb
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
require 'zlib'
|
|
2
|
+
require 'stringio'
|
|
3
|
+
|
|
1
4
|
# A convenient wrapper for the zlib standard library that allows
|
|
2
5
|
# compression/decompression of strings with gzip.
|
|
3
6
|
|
|
@@ -24,15 +27,8 @@ module Zlib
|
|
|
24
27
|
output.string
|
|
25
28
|
end
|
|
26
29
|
|
|
27
|
-
#
|
|
28
|
-
|
|
29
|
-
Inflate.inflate(string)
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
# Deflate a string.
|
|
33
|
-
def self.deflate(string, level=DEFAULT_COMPRESSION)
|
|
34
|
-
Deflate.deflate(string, level)
|
|
35
|
-
end
|
|
30
|
+
# Zlib.inflate and Zlib.deflate are Ruby's own. Facets used to define
|
|
31
|
+
# copies of them here.
|
|
36
32
|
|
|
37
33
|
end
|
|
38
34
|
|