facets 4.0.1 → 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 +10 -0
- data/HISTORY.md +140 -0
- data/README.md +2 -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/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/extend.rb +20 -4
- 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/to_time.rb +3 -7
- data/lib/core/facets/version.rb +1 -1
- data/lib/standard/facets/digest/salted_digest.rb +6 -2
- data/lib/standard/facets/fileutils/ln_r.rb +7 -56
- 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 +53 -5
- data/lib/standard/facets/zlib.rb +5 -9
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c0b8832c335f2a91e27795701f042906df5e734a3e95875f9d5eff91d32a4fff
|
|
4
|
+
data.tar.gz: 4eb0cd3ab5f12aea90d875d04ec996e52c14fd139fcbe456bdd76ab3b3dae368
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d2d8dddda82cd3fc70359c563bd39235fcd7876e3a1698a9c5d0b47ee79661cadbe713237297b437fb298e0fbe2ebb94ba0700d8f489d0e96396143a807fceab
|
|
7
|
+
data.tar.gz: 6f1551cbae0a3b0235352c749640ea5861f1946166d76d08c86c8473d909e4e54697a4ca0ee7dea252651a1b5b24e7bdcb565a5235964f7befc887d5349f03fc
|
data/CONTRIBUTING.md
CHANGED
|
@@ -27,6 +27,16 @@
|
|
|
27
27
|
require line, and comment out their line in the aggregator file with
|
|
28
28
|
`# uncommon`.
|
|
29
29
|
|
|
30
|
+
* Facets must not change what Ruby's own methods do, in the core or the
|
|
31
|
+
standard library. A Facets method with the same name as one of Ruby's is
|
|
32
|
+
allowed only if it is compatible, giving Ruby's result for everything
|
|
33
|
+
Ruby's version accepts (or differing only where Ruby's gives no useful
|
|
34
|
+
answer, such as raising or returning nil), and it adds something useful.
|
|
35
|
+
Otherwise Facets gets out of Ruby's way, and the Facets version is either
|
|
36
|
+
dropped or kept under a new name. Never redefine a method just to copy
|
|
37
|
+
Ruby's. If Ruby later adds a method Facets already has, the same rule
|
|
38
|
+
applies.
|
|
39
|
+
|
|
30
40
|
* Standard libraries that are not extensions of existing standard libraries
|
|
31
41
|
do not have to be divvied up into individual method files. But note that
|
|
32
42
|
full classes and/or modules are less likely to make it into Facets, as
|
data/HISTORY.md
CHANGED
|
@@ -1,5 +1,145 @@
|
|
|
1
1
|
# Facets Release History
|
|
2
2
|
|
|
3
|
+
## 4.1.0 / 2026-11-08
|
|
4
|
+
|
|
5
|
+
Facets and ActiveSupport define many of the same methods, and whichever
|
|
6
|
+
loads last replaces the other's. This release makes a set of those methods
|
|
7
|
+
behave the same in both libraries, so it no longer matters which one wins.
|
|
8
|
+
Facets now matches ActiveSupport 8.1 in these cases, which fixes bugs in
|
|
9
|
+
Rails apps that load Facets, and adds the extra options ActiveSupport's
|
|
10
|
+
versions take.
|
|
11
|
+
|
|
12
|
+
It also stops Facets from changing what Ruby's own methods do. Under a new
|
|
13
|
+
rule, now in CONTRIBUTING.md, a Facets method may share a name with one of
|
|
14
|
+
Ruby's only if it gives Ruby's results and adds something useful. Otherwise
|
|
15
|
+
Facets gets out of Ruby's way.
|
|
16
|
+
|
|
17
|
+
Changes:
|
|
18
|
+
|
|
19
|
+
* Bug Fixes
|
|
20
|
+
|
|
21
|
+
* `Hash#count`, loaded by `require 'facets'`, gives Ruby's own answers
|
|
22
|
+
again. Facets' version counted values rather than key-value pairs, which
|
|
23
|
+
changed Ruby's answer for every library in the program:
|
|
24
|
+
`{a: 1}.count([:a, 1])` returned 0. To count values, use
|
|
25
|
+
`hash.values.count(value)`. Until 2027-09-30, `Hash#count` warns when
|
|
26
|
+
given an argument that can't be a key-value pair, which is how Facets'
|
|
27
|
+
version was called.
|
|
28
|
+
* `Array#shelljoin` from `facets/shellwords` is Ruby's own again. Facets'
|
|
29
|
+
version flattened nested arrays and turned hashes into options, so a
|
|
30
|
+
value meant as one argument could become several. A web request's
|
|
31
|
+
parameter can be an array (`?term[]=&term[]=/home`), and with it
|
|
32
|
+
`['grep', '-r', '--', term, 'public/'].shelljoin` gave
|
|
33
|
+
`grep -r -- '' /home public/`, which prints every file under `/home`.
|
|
34
|
+
Ruby's escapes each element as one argument. To turn hashes into
|
|
35
|
+
options, use `array.shellwords.shelljoin`. Until 2027-09-30,
|
|
36
|
+
`Array#shelljoin` warns when the array has a hash or an array in it.
|
|
37
|
+
* `Hash#shellwords`, and so `Hash#shelljoin` and `Array#shellwords`, no
|
|
38
|
+
longer put quote characters around values. Once the words were escaped
|
|
39
|
+
with `shelljoin`, the quotes became part of the value:
|
|
40
|
+
`{name: 'jim'}.shelljoin` passed `"jim"`, quotes included. Each flag and
|
|
41
|
+
its value is now one plain word, `--name=jim`.
|
|
42
|
+
* `require 'facets'` loads `String#trim`. When `String#margin` was renamed
|
|
43
|
+
to `#trim` in 3.0, the new file was never added to the core aggregator,
|
|
44
|
+
so `trim` worked only with its own require.
|
|
45
|
+
* `Enumerable#defer` with a block works again. Since 3.2 deprecated it,
|
|
46
|
+
`defer` ignored its block, so
|
|
47
|
+
`(1..10).defer { |out, i| out << i if i.even? }.first(5)` returned
|
|
48
|
+
`[1, 2, 3, 4, 5]` instead of `[2, 4, 6, 8, 10]`. It is still
|
|
49
|
+
deprecated; the block form is `Enumerator::Lazy.new(enum) { ... }`.
|
|
50
|
+
* `Range#overlap?` on Ruby 3.1 and 3.2, where Ruby has no version of its
|
|
51
|
+
own, gives the same answers as Ruby's. Facets' version said an empty
|
|
52
|
+
range like `3...3` overlapped any range containing 3, raised RangeError
|
|
53
|
+
for `(3..).overlap?(..5)`, and raised NoMethodError rather than
|
|
54
|
+
TypeError for a non-Range argument. It now follows Ruby's algorithm.
|
|
55
|
+
* `Time.current` uses ActiveSupport's `Time.zone` when it is set. Before,
|
|
56
|
+
when Facets loaded after ActiveSupport, `Time.current` ignored the Rails
|
|
57
|
+
app's time zone and returned the system's local time.
|
|
58
|
+
* `Time#change` keeps the time's UTC offset or time zone. Before, a time
|
|
59
|
+
such as `15:30 +09:00` changed to the system's local time zone. It also
|
|
60
|
+
keeps fractions of a microsecond.
|
|
61
|
+
* Facets' deprecated `Time#to_time`, used only when `date` isn't loaded,
|
|
62
|
+
returns `self`, as Ruby's own does. Before, it converted to local time.
|
|
63
|
+
* `String#blank?` and `String#squish` treat Unicode whitespace, such as a
|
|
64
|
+
non-breaking space, as whitespace. `String#blank?` also works for strings
|
|
65
|
+
in encodings such as UTF-16.
|
|
66
|
+
* `Class#descendants` no longer raises `ArgumentError` when ActiveSupport
|
|
67
|
+
is loaded. Calling it with a number of generations still does, because
|
|
68
|
+
ActiveSupport wraps the method in one that takes no argument.
|
|
69
|
+
* `Integer#ordinal` and `#ordinalize` give `"-1st"` for -1, not `"-1th"`.
|
|
70
|
+
* `String#unindent` with no argument removes only the least indented
|
|
71
|
+
line's indentation from each line. Before, it removed more from the more
|
|
72
|
+
indented lines, so its own example, `"ohaie\n wurld"`, came out as
|
|
73
|
+
`"ohaie\nwurld"`. It also raised `NoMethodError` for an empty string.
|
|
74
|
+
* `String#indent` with a negative number and an indent string of more than
|
|
75
|
+
one character removes only whole copies of that string.
|
|
76
|
+
* `require 'facets/fileutils/ln_r'` replaced Ruby's `FileUtils.link_entry`
|
|
77
|
+
and an internal method it uses with old copies, so Ruby's own
|
|
78
|
+
`FileUtils.cp_lr` raised `NameError`. `FileUtils.ln_r` now uses Ruby's
|
|
79
|
+
`link_entry`, and the file requires `fileutils` itself.
|
|
80
|
+
* `Pathname#glob` from `facets/pathname/glob` ignored a block; it now gives
|
|
81
|
+
the block each match, as Ruby's does. With symbol flags, such as
|
|
82
|
+
`:dotmatch`, it now uses Ruby's glob too, so it works in a directory
|
|
83
|
+
whose name has glob characters such as `[`, and returns paths in the
|
|
84
|
+
same form as it does without them. The file requires `pathname` itself.
|
|
85
|
+
* `Kernel#extend` with no arguments and no block raises `ArgumentError`,
|
|
86
|
+
as Ruby's does; before, it returned the object. `ruby -w` no longer warns
|
|
87
|
+
that `extend` was redefined.
|
|
88
|
+
* `require 'facets/digest'` raised `LoadError`, because it required a file
|
|
89
|
+
that had moved. `#salted_digest`'s automatic salt now comes from
|
|
90
|
+
`SecureRandom` instead of `rand`.
|
|
91
|
+
* `facets/zlib` no longer defines its own copies of Ruby's `Zlib.inflate`
|
|
92
|
+
and `Zlib.deflate`. It now requires `zlib` and `stringio` itself;
|
|
93
|
+
before, `Zlib.compress` raised `NameError` unless they were already
|
|
94
|
+
loaded.
|
|
95
|
+
* `OpenStruct#initialize` from `facets/ostruct` calls Ruby's own
|
|
96
|
+
`initialize` for the hash, instead of a copy of it, and `ruby -w` no
|
|
97
|
+
longer warns that it was redefined.
|
|
98
|
+
|
|
99
|
+
* Additions
|
|
100
|
+
|
|
101
|
+
* `Time#change` takes `nsec:` and `offset:`.
|
|
102
|
+
* `String#remove` and `#remove!` take several patterns.
|
|
103
|
+
* `Hash#deep_merge` and `#deep_merge!` take a block, as `Hash#merge` does,
|
|
104
|
+
to decide the value of a key both hashes have.
|
|
105
|
+
* `Array#split` takes a block instead of a pattern, and splits on `nil`
|
|
106
|
+
when given neither.
|
|
107
|
+
* `String#indent` and `#indent!` take a third argument; when true, empty
|
|
108
|
+
lines are indented too.
|
|
109
|
+
|
|
110
|
+
* Deprecations
|
|
111
|
+
|
|
112
|
+
* `Kernel#extend` with a block, which Facets has added since 2.8.0, now
|
|
113
|
+
warns, and will be removed after 2027-09-30. It's the only reason Facets
|
|
114
|
+
overrides Ruby's `extend`, which every `extend` call in a program then
|
|
115
|
+
goes through. Use `obj.extend(Module.new { ... })` instead. Without
|
|
116
|
+
Facets, Ruby raises if given only a block, and ignores a block given
|
|
117
|
+
with modules.
|
|
118
|
+
|
|
119
|
+
* Changes that can affect existing code
|
|
120
|
+
|
|
121
|
+
* `Hash#count(value)` and `Array#shelljoin` no longer do what Facets'
|
|
122
|
+
versions did; see Bug Fixes. Both warn until 2027-09-30 when called
|
|
123
|
+
the old way.
|
|
124
|
+
* `Hash#shellwords` gives `--name=jim` rather than `--name="jim"`. Code
|
|
125
|
+
that joined the words with spaces into a shell command should use
|
|
126
|
+
`shelljoin` instead.
|
|
127
|
+
* `Array#split` keeps an empty piece where a match is at either end:
|
|
128
|
+
`[2, 1, 2].split(2)` is `[[], [1], []]`, and `[].split(2)` is `[[]]`.
|
|
129
|
+
Before, empty pieces at the end were dropped.
|
|
130
|
+
* `String#indent` leaves empty lines unchanged unless asked. Without an
|
|
131
|
+
indent string, it indents with the string's own indentation character,
|
|
132
|
+
so a tab-indented string gets more tabs.
|
|
133
|
+
* `String#remove!` returns the string even when nothing was removed.
|
|
134
|
+
Before, it returned `nil` then.
|
|
135
|
+
* `Hash#deep_merge!` replaces a sub-hash with a merged copy instead of
|
|
136
|
+
changing it, so other references to the sub-hash no longer see the
|
|
137
|
+
change.
|
|
138
|
+
* `Class#descendants` lists all subclasses before their own descendants.
|
|
139
|
+
On Ruby 4.1, which adds its own `Module#descendants`, a call without a
|
|
140
|
+
number of generations is passed to Ruby's, so it gives Ruby's list, in
|
|
141
|
+
Ruby's order. Only `descendants(n)` is still Facets' own there.
|
|
142
|
+
|
|
3
143
|
## 4.0.1 / 2026-11-02
|
|
4
144
|
|
|
5
145
|
A patch release with a security fix: `require 'facets/shellwords'` replaced
|
data/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
**More of Ruby, one method at a time.** Facets is a collection of extensions to Ruby's core classes and standard library, plus a few small, reusable classes and modules. Most methods live in their own files, so you can load one extension, a class's extensions, or the core collection.
|
|
7
7
|
|
|
8
|
-
Facets took its name in 2004, growing out of a small methods library started in 2002, and is still maintained. The current release is **4.0
|
|
8
|
+
Facets took its name in 2004, growing out of a small methods library started in 2002, and is still maintained. The current release is **4.1.0**, which requires **Ruby 3.1 or newer**. See the [release history](HISTORY.md) for changes and migration notes from earlier versions. The `main` branch also contains changes awaiting the next release.
|
|
9
9
|
|
|
10
10
|
## Install
|
|
11
11
|
|
|
@@ -62,7 +62,7 @@ require 'facets'
|
|
|
62
62
|
require 'facets/ostruct'
|
|
63
63
|
```
|
|
64
64
|
|
|
65
|
-
This loads `ostruct` and Facets' OpenStruct extensions. On Ruby
|
|
65
|
+
This loads `ostruct` and Facets' OpenStruct extensions. On Ruby 4.0+, list the `ostruct` gem in your Gemfile, because it is now a bundled gem rather than a default gem.
|
|
66
66
|
|
|
67
67
|
## Documentation
|
|
68
68
|
|
|
@@ -1,20 +1,30 @@
|
|
|
1
1
|
class Array
|
|
2
2
|
|
|
3
|
-
# Split on matching
|
|
4
|
-
# elements.
|
|
5
|
-
#
|
|
6
|
-
# Examples
|
|
3
|
+
# Split on matching elements. Unlike #divide this does not include the
|
|
4
|
+
# matching elements. An element matches if `pattern === element`, so a
|
|
5
|
+
# pattern can be a value, a Regexp or a class.
|
|
7
6
|
#
|
|
8
7
|
# ['a1','a2','b1','a3','b2','a4'].split(/^b/)
|
|
9
8
|
# #=> [['a1','a2'],['a3'],['a4']]
|
|
10
9
|
#
|
|
10
|
+
# Or give a block, and split where it returns true.
|
|
11
|
+
#
|
|
12
|
+
# [1, 2, 3, 4, 5].split { |x| x % 3 == 0 }
|
|
13
|
+
# #=> [[1, 2], [4, 5]]
|
|
14
|
+
#
|
|
15
|
+
# As with ActiveSupport's Array#split, every match ends a piece, so a match
|
|
16
|
+
# at either end gives an empty piece there.
|
|
17
|
+
#
|
|
18
|
+
# [2, 1, 2].split(2) #=> [[], [1], []]
|
|
19
|
+
# [].split(2) #=> [[]]
|
|
20
|
+
#
|
|
11
21
|
# Returns list of split-up arrays. [Array<Array>]
|
|
12
22
|
|
|
13
|
-
def split(pattern)
|
|
23
|
+
def split(pattern=nil, &block)
|
|
14
24
|
memo = []
|
|
15
25
|
sect = []
|
|
16
26
|
each do |obj|
|
|
17
|
-
if pattern === obj
|
|
27
|
+
if block ? block.call(obj) : pattern === obj
|
|
18
28
|
memo << sect
|
|
19
29
|
sect = []
|
|
20
30
|
else
|
|
@@ -22,9 +32,6 @@ class Array
|
|
|
22
32
|
end
|
|
23
33
|
end
|
|
24
34
|
memo << sect
|
|
25
|
-
memo.pop while memo.last == []
|
|
26
|
-
memo
|
|
27
35
|
end
|
|
28
36
|
|
|
29
37
|
end
|
|
30
|
-
|
|
@@ -16,19 +16,32 @@ class Class
|
|
|
16
16
|
# X.descendants #=> [Y,Z]
|
|
17
17
|
# X.descendants(1) #=> [Y]
|
|
18
18
|
#
|
|
19
|
+
# Ruby 4.1 adds its own Module#descendants. Where Ruby has it, a call
|
|
20
|
+
# without a limit is passed to Ruby's, and Facets only handles the
|
|
21
|
+
# generations limit. Ruby doesn't define the order of its list.
|
|
22
|
+
#
|
|
23
|
+
# On older Rubies, the list is in the same order as ActiveSupport's
|
|
24
|
+
# Class#descendants: the subclasses first, then each one's own
|
|
25
|
+
# descendants.
|
|
26
|
+
#
|
|
27
|
+
# The generations walk goes through #subclasses only, never #descendants,
|
|
28
|
+
# because ActiveSupport wraps #descendants in a version that takes no
|
|
29
|
+
# argument.
|
|
30
|
+
#
|
|
31
|
+
# NOTE: With ActiveSupport loaded, calling descendants with an argument
|
|
32
|
+
# raises ArgumentError, because of that wrapper.
|
|
33
|
+
#
|
|
19
34
|
# NOTE: This is a intensive operation. Do not expect it to be very fast.
|
|
20
35
|
#
|
|
21
36
|
# @author Roger Pack
|
|
22
37
|
def descendants(generations=-1)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if
|
|
27
|
-
|
|
28
|
-
end
|
|
38
|
+
return super() if generations < 1 && Module.method_defined?(:descendants)
|
|
39
|
+
walk = lambda do |klass, gen|
|
|
40
|
+
subs = klass.subclasses
|
|
41
|
+
next subs if gen == 1
|
|
42
|
+
subs + subs.flat_map { |k| walk.(k, gen - 1) }
|
|
29
43
|
end
|
|
30
|
-
|
|
44
|
+
walk.(self, generations)
|
|
31
45
|
end
|
|
32
46
|
|
|
33
47
|
end
|
|
34
|
-
|
|
@@ -26,9 +26,12 @@ module Enumerable
|
|
|
26
26
|
# Array or Hash built with the results, or each{...} if you just want
|
|
27
27
|
# to output each result and discard it.
|
|
28
28
|
#
|
|
29
|
+
# @deprecated Use Enumerable#lazy, built into Ruby since 2.0. The block
|
|
30
|
+
# form is Enumerator::Lazy.new(enum) { |out, i| ... }. Scheduled for
|
|
31
|
+
# removal after 2027-09-30.
|
|
29
32
|
def defer(&blk)
|
|
30
33
|
warn "Enumerable#defer is deprecated. Use Enumerable#lazy instead. It will be removed after 2027-09-30.", uplevel: 1
|
|
31
|
-
lazy
|
|
34
|
+
blk ? Enumerator::Lazy.new(self, &blk) : lazy
|
|
32
35
|
end
|
|
33
36
|
|
|
34
37
|
end
|
|
@@ -1,16 +1,27 @@
|
|
|
1
1
|
class Hash
|
|
2
2
|
|
|
3
|
-
#
|
|
3
|
+
# Ruby's own Hash#count, from Enumerable, counts key-value pairs:
|
|
4
4
|
#
|
|
5
|
-
# {:A=>1, :B=>1}.count(1) #=>
|
|
5
|
+
# {:A=>1, :B=>1}.count([:A, 1]) #=> 1
|
|
6
6
|
#
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
# Facets' version used to count values instead, which changed Ruby's
|
|
8
|
+
# answer for every library in the program. It now returns Ruby's answer,
|
|
9
|
+
# and warns when given something that can't be a key-value pair, since
|
|
10
|
+
# that's how the old Facets version was called. To count values, use
|
|
11
|
+
# `hash.values.count(value)`.
|
|
12
|
+
#
|
|
13
|
+
# {:A=>1, :B=>1}.values.count(1) #=> 2
|
|
14
|
+
#
|
|
15
|
+
# @deprecated Facets no longer changes Hash#count. This warning wrapper is
|
|
16
|
+
# scheduled for removal after 2027-09-30.
|
|
17
|
+
#
|
|
18
|
+
def count(*args, &block)
|
|
19
|
+
if args.size == 1 && !(Array === args.first && args.first.size == 2)
|
|
20
|
+
warn "Facets' Hash#count(value) is deprecated. Hash#count now counts key-value pairs, " \
|
|
21
|
+
"as Ruby's own does, so this returns 0. To count values, use hash.values.count(value). " \
|
|
22
|
+
"It will be removed after 2027-09-30.", uplevel: 1
|
|
12
23
|
end
|
|
24
|
+
super
|
|
13
25
|
end
|
|
14
26
|
|
|
15
27
|
end
|
|
16
|
-
|
|
@@ -1,33 +1,38 @@
|
|
|
1
1
|
class Hash
|
|
2
2
|
|
|
3
3
|
# Same as Hash#merge but recursively merges sub-hashes.
|
|
4
|
+
#
|
|
5
|
+
# {a: {b: 1, c: 2}}.deep_merge(a: {b: 3}) #=> {a: {b: 3, c: 2}}
|
|
6
|
+
#
|
|
7
|
+
# Like Hash#merge, it takes a block to decide the value of a key both
|
|
8
|
+
# hashes have, other than one that is itself merged.
|
|
9
|
+
#
|
|
10
|
+
# {a: {b: 1}}.deep_merge(a: {b: 2}) { |key, mine, theirs| mine + theirs }
|
|
11
|
+
# #=> {a: {b: 3}}
|
|
12
|
+
#
|
|
13
|
+
# A sub-hash is merged only when the other value is a hash too. This
|
|
14
|
+
# matches ActiveSupport's Hash#deep_merge, so the two agree whichever is
|
|
15
|
+
# loaded, and it honors ActiveSupport's #deep_merge? when that's defined.
|
|
4
16
|
|
|
5
|
-
def deep_merge(other)
|
|
6
|
-
|
|
7
|
-
other.each do |key, value|
|
|
8
|
-
myval = self[key]
|
|
9
|
-
if value.is_a?(Hash) && myval.is_a?(Hash)
|
|
10
|
-
hash[key] = myval.deep_merge(value)
|
|
11
|
-
else
|
|
12
|
-
hash[key] = value
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
hash
|
|
17
|
+
def deep_merge(other, &block)
|
|
18
|
+
dup.deep_merge!(other, &block)
|
|
16
19
|
end
|
|
17
20
|
|
|
18
|
-
# Same as Hash#merge! but recursively merges sub-hashes.
|
|
21
|
+
# Same as Hash#merge! but recursively merges sub-hashes. The sub-hashes
|
|
22
|
+
# themselves aren't changed; merged copies replace them.
|
|
19
23
|
|
|
20
|
-
def deep_merge!(other)
|
|
21
|
-
other
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
def deep_merge!(other, &block)
|
|
25
|
+
merge!(other) do |key, mine, theirs|
|
|
26
|
+
deep = Hash === mine &&
|
|
27
|
+
(mine.respond_to?(:deep_merge?) ? mine.deep_merge?(theirs) : Hash === theirs)
|
|
28
|
+
if deep
|
|
29
|
+
mine.deep_merge(theirs, &block)
|
|
30
|
+
elsif block
|
|
31
|
+
block.call(key, mine, theirs)
|
|
25
32
|
else
|
|
26
|
-
|
|
33
|
+
theirs
|
|
27
34
|
end
|
|
28
35
|
end
|
|
29
|
-
self
|
|
30
36
|
end
|
|
31
37
|
|
|
32
38
|
end
|
|
33
|
-
|
|
@@ -1,24 +1,29 @@
|
|
|
1
1
|
class Integer
|
|
2
2
|
|
|
3
|
+
# The number with its English ordinal suffix.
|
|
4
|
+
#
|
|
5
|
+
# 1.ordinal #=> "1st"
|
|
6
|
+
# 12.ordinal #=> "12th"
|
|
7
|
+
# 103.ordinal #=> "103rd"
|
|
8
|
+
# -1.ordinal #=> "-1st"
|
|
9
|
+
#
|
|
3
10
|
def ordinal
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
when 1
|
|
9
|
-
"#{self}st"
|
|
10
|
-
when 2
|
|
11
|
-
"#{self}nd"
|
|
12
|
-
when 3
|
|
13
|
-
"#{self}rd"
|
|
11
|
+
n = abs % 100
|
|
12
|
+
suffix =
|
|
13
|
+
if (11..13).include?(n)
|
|
14
|
+
'th'
|
|
14
15
|
else
|
|
15
|
-
|
|
16
|
+
case n % 10
|
|
17
|
+
when 1 then 'st'
|
|
18
|
+
when 2 then 'nd'
|
|
19
|
+
when 3 then 'rd'
|
|
20
|
+
else 'th'
|
|
21
|
+
end
|
|
16
22
|
end
|
|
17
|
-
|
|
23
|
+
"#{self}#{suffix}"
|
|
18
24
|
end
|
|
19
25
|
|
|
20
26
|
# Rails term.
|
|
21
27
|
alias_method :ordinalize, :ordinal
|
|
22
28
|
|
|
23
29
|
end
|
|
24
|
-
|
|
@@ -67,12 +67,17 @@ class Hash
|
|
|
67
67
|
end
|
|
68
68
|
|
|
69
69
|
class String
|
|
70
|
-
# Is this string just whitespace?
|
|
70
|
+
# Is this string empty or just whitespace? Unicode whitespace, such as a
|
|
71
|
+
# non-breaking space, counts too, as it does in ActiveSupport.
|
|
71
72
|
#
|
|
72
|
-
# "abc".blank?
|
|
73
|
-
# " ".blank?
|
|
73
|
+
# "abc".blank? #=> false
|
|
74
|
+
# " ".blank? #=> true
|
|
75
|
+
# " ".blank? #=> true
|
|
74
76
|
def blank?
|
|
75
|
-
|
|
77
|
+
empty? || /\A[[:space:]]*\z/.match?(self)
|
|
78
|
+
rescue Encoding::CompatibilityError
|
|
79
|
+
# e.g. UTF-16 text, which a UTF-8 regexp can't match
|
|
80
|
+
Regexp.new('\A[[:space:]]*\z'.encode(encoding), Regexp::FIXEDENCODING).match?(self)
|
|
76
81
|
end
|
|
77
82
|
end
|
|
78
83
|
|
|
@@ -2,6 +2,12 @@ module Kernel
|
|
|
2
2
|
|
|
3
3
|
extend_method = instance_method(:extend)
|
|
4
4
|
|
|
5
|
+
# Removing Ruby's #extend before defining Facets' keeps `ruby -w` from
|
|
6
|
+
# warning that it was redefined; Facets calls it through extend_method.
|
|
7
|
+
# (A module prepended to Kernel would be simpler, but TruffleRuby doesn't
|
|
8
|
+
# apply it to classes that already include Kernel, such as Object.)
|
|
9
|
+
remove_method :extend
|
|
10
|
+
|
|
5
11
|
# Extend an object with a module.
|
|
6
12
|
#
|
|
7
13
|
# If a block is given, it will be evaluated into an anonymous
|
|
@@ -15,14 +21,24 @@ module Kernel
|
|
|
15
21
|
#
|
|
16
22
|
# obj.foo #=> "foo"
|
|
17
23
|
#
|
|
24
|
+
# Without a block it is Ruby's own #extend.
|
|
25
|
+
#
|
|
18
26
|
# NOTE: This is one of the few core overrides in Facets.
|
|
19
27
|
#
|
|
28
|
+
# @deprecated The block form is deprecated, so that Facets no longer has
|
|
29
|
+
# to override Ruby's #extend. Use `obj.extend(Module.new { ... })`
|
|
30
|
+
# instead. Scheduled for removal after 2027-09-30. Without Facets,
|
|
31
|
+
# Ruby raises if given only a block, and ignores a block given with
|
|
32
|
+
# modules.
|
|
33
|
+
#
|
|
20
34
|
# @author Trans
|
|
21
35
|
# @author Marco Otte-Witte (bug fix)
|
|
22
|
-
define_method :extend do |*
|
|
23
|
-
extend_method.
|
|
24
|
-
|
|
25
|
-
|
|
36
|
+
define_method :extend do |*mods, &blk|
|
|
37
|
+
return extend_method.bind_call(self, *mods) unless blk
|
|
38
|
+
warn "Kernel#extend with a block is deprecated. Use obj.extend(Module.new { ... }) instead. " \
|
|
39
|
+
"It will be removed after 2027-09-30.", uplevel: 1
|
|
40
|
+
extend_method.bind_call(self, *mods) unless mods.empty?
|
|
41
|
+
extend_method.bind_call(self, Module.new(&blk))
|
|
26
42
|
end
|
|
27
43
|
|
|
28
44
|
end
|
|
@@ -7,12 +7,43 @@ class Range
|
|
|
7
7
|
#
|
|
8
8
|
# Ruby 3.3 added a native Range#overlap?, so only define Facets' version
|
|
9
9
|
# on older Rubies that lack it -- this avoids replacing the built-in.
|
|
10
|
+
# It follows Ruby's own algorithm, so it gives the same answers: an empty
|
|
11
|
+
# range overlaps nothing, beginless and endless ranges work, and a
|
|
12
|
+
# non-Range argument raises TypeError.
|
|
10
13
|
#
|
|
11
14
|
# CREDIT: Daniel Schierbeck, Brandon Keepers
|
|
12
15
|
|
|
13
16
|
unless method_defined?(:overlap?)
|
|
14
17
|
def overlap?(other)
|
|
15
|
-
|
|
18
|
+
unless other.is_a?(Range)
|
|
19
|
+
raise TypeError, "wrong argument type #{other.class} (expected Range)"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
self_beg, self_end, self_excl = self.begin, self.end, exclude_end?
|
|
23
|
+
other_beg, other_end, other_excl = other.begin, other.end, other.exclude_end?
|
|
24
|
+
|
|
25
|
+
# Is the span from b to e empty? Values that can't be compared count
|
|
26
|
+
# as empty, as in Ruby.
|
|
27
|
+
empty = lambda do |b, e, excl|
|
|
28
|
+
next false if b.nil? || e.nil?
|
|
29
|
+
cmp = b <=> e
|
|
30
|
+
cmp.nil? || cmp > 0 || (excl && cmp == 0)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
return false if empty.(self_beg, other_end, other_excl)
|
|
34
|
+
return false if empty.(other_beg, self_end, self_excl)
|
|
35
|
+
|
|
36
|
+
if !self_beg.nil? && !other_beg.nil?
|
|
37
|
+
cmp = self_beg <=> other_beg
|
|
38
|
+
return false if cmp.nil?
|
|
39
|
+
return true if cmp == 0
|
|
40
|
+
elsif self_beg.nil? && !self_end.nil? && other_beg.nil? && !other_end.nil?
|
|
41
|
+
return !(self_end <=> other_end).nil?
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
return false if empty.(self_beg, self_end, self_excl)
|
|
45
|
+
return false if empty.(other_beg, other_end, other_excl)
|
|
46
|
+
true
|
|
16
47
|
end
|
|
17
48
|
end
|
|
18
49
|
|
|
@@ -1,9 +1,28 @@
|
|
|
1
1
|
class String
|
|
2
2
|
|
|
3
|
-
# Indent
|
|
3
|
+
# Indent right by `n` copies of `c`, or left by removing up to `n` copies
|
|
4
|
+
# of `c` from the start of each line if `n` is negative.
|
|
4
5
|
#
|
|
5
|
-
# n
|
|
6
|
-
#
|
|
6
|
+
# "a\nb".indent(2) #=> " a\n b"
|
|
7
|
+
# " a".indent(-2) #=> " a"
|
|
8
|
+
# "a".indent(2, "-") #=> "--a"
|
|
9
|
+
#
|
|
10
|
+
# Empty lines aren't indented, unless `empty_lines` is true.
|
|
11
|
+
#
|
|
12
|
+
# "a\n\nb".indent(2) #=> " a\n\n b"
|
|
13
|
+
# "a\n\nb".indent(2, ' ', true) #=> " a\n \n b"
|
|
14
|
+
#
|
|
15
|
+
# Without `c`, it indents with the string's own indentation character,
|
|
16
|
+
# a tab or a space, or a space if the string has no indentation.
|
|
17
|
+
#
|
|
18
|
+
# "\ta".indent(1) #=> "\t\ta"
|
|
19
|
+
#
|
|
20
|
+
# This matches ActiveSupport's String#indent, so the two agree whichever
|
|
21
|
+
# is loaded. A negative `n` is a Facets addition.
|
|
22
|
+
#
|
|
23
|
+
# n - The number of copies of `c` to add or remove. [Integer]
|
|
24
|
+
# c - The indentation string. [String]
|
|
25
|
+
# empty_lines - Whether to indent empty lines too. [Boolean]
|
|
7
26
|
#
|
|
8
27
|
# Returns a new string with the indention. [String]
|
|
9
28
|
#
|
|
@@ -11,23 +30,21 @@ class String
|
|
|
11
30
|
# Credit: Trans
|
|
12
31
|
# Credit: Tyler Rick
|
|
13
32
|
|
|
14
|
-
def indent(n, c=
|
|
33
|
+
def indent(n, c=nil, empty_lines=false)
|
|
34
|
+
c ||= self[/^[ \t]/] || ' '
|
|
15
35
|
if n >= 0
|
|
16
|
-
gsub(
|
|
36
|
+
gsub(empty_lines ? /^/ : /^(?!$)/, c * n)
|
|
17
37
|
else
|
|
18
|
-
gsub(
|
|
38
|
+
gsub(/^(?:#{Regexp.escape(c)}){0,#{-n}}/, "")
|
|
19
39
|
end
|
|
20
40
|
end
|
|
21
41
|
|
|
22
42
|
# Equivalent to String#indent, but modifies the receiver in place.
|
|
23
43
|
#
|
|
24
|
-
# n - The number of spaces to indent. [Integer]
|
|
25
|
-
# c - Character to use, if other then space. [String]
|
|
26
|
-
#
|
|
27
44
|
# Returns this string with the indention. [String]
|
|
28
45
|
|
|
29
|
-
def indent!(n, c=
|
|
30
|
-
replace(indent(n,c))
|
|
46
|
+
def indent!(n, c=nil, empty_lines=false)
|
|
47
|
+
replace(indent(n, c, empty_lines))
|
|
31
48
|
end
|
|
32
49
|
|
|
33
50
|
# Remove excessive indentation. Useful for multi-line strings
|
|
@@ -52,15 +69,10 @@ class String
|
|
|
52
69
|
if size
|
|
53
70
|
indent(-size)
|
|
54
71
|
else
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
char = m[0...-1]
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
size -= 1
|
|
63
|
-
indent(-size, char)
|
|
72
|
+
# The indentation of the least indented line that isn't blank.
|
|
73
|
+
prefix = scan(/^[ \t]*(?=\S)/).min_by(&:size)
|
|
74
|
+
return dup if prefix.nil? || prefix.empty?
|
|
75
|
+
gsub(/^#{Regexp.escape(prefix)}/, '')
|
|
64
76
|
end
|
|
65
77
|
end
|
|
66
78
|
|
|
@@ -1,17 +1,22 @@
|
|
|
1
1
|
class String
|
|
2
2
|
|
|
3
|
-
# Removes all occurrences of
|
|
3
|
+
# Removes all occurrences of one or more patterns in a string.
|
|
4
4
|
#
|
|
5
|
-
#
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
# "foo bar".remove("o") #=> "f bar"
|
|
6
|
+
# "foo bar".remove(/o/, "b") #=> "f ar"
|
|
7
|
+
#
|
|
8
|
+
# Returns a new [String] with all occurrences of the patterns removed.
|
|
9
|
+
def remove(*patterns)
|
|
10
|
+
dup.remove!(*patterns)
|
|
8
11
|
end
|
|
9
12
|
|
|
10
|
-
# Removes all occurrences of
|
|
13
|
+
# Removes all occurrences of one or more patterns in a string.
|
|
11
14
|
#
|
|
12
|
-
# Returns the [String] with all occurrences of the
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
# Returns the [String] with all occurrences of the patterns removed,
|
|
16
|
+
# even if nothing was removed.
|
|
17
|
+
def remove!(*patterns)
|
|
18
|
+
patterns.each { |pattern| gsub!(pattern, '') }
|
|
19
|
+
self
|
|
15
20
|
end
|
|
16
21
|
|
|
17
22
|
# Removes occurances of a string or regexp. This is an operator
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
class String
|
|
2
|
+
|
|
2
3
|
# Returns the string, first removing all whitespace on both ends of
|
|
3
4
|
# the string, and then changing remaining consecutive whitespace
|
|
4
|
-
# groups into one space each.
|
|
5
|
+
# groups into one space each. Unicode whitespace, such as a
|
|
6
|
+
# non-breaking space, counts too, as it does in ActiveSupport.
|
|
5
7
|
#
|
|
6
8
|
# %{ Multi-line
|
|
7
9
|
# string }.squish # => "Multi-line string"
|
|
@@ -14,9 +16,9 @@ class String
|
|
|
14
16
|
|
|
15
17
|
# Performs a destructive squish. See String#squish.
|
|
16
18
|
def squish!
|
|
19
|
+
gsub!(/[[:space:]]+/, ' ')
|
|
17
20
|
strip!
|
|
18
|
-
gsub!(/\s+/, ' ')
|
|
19
21
|
self
|
|
20
22
|
end
|
|
21
|
-
end
|
|
22
23
|
|
|
24
|
+
end
|
data/lib/core/facets/string.rb
CHANGED
|
@@ -54,6 +54,7 @@ require_relative 'string/store.rb'
|
|
|
54
54
|
require_relative 'string/subtract.rb'
|
|
55
55
|
require_relative 'string/titlecase.rb'
|
|
56
56
|
require_relative 'string/to_re.rb'
|
|
57
|
+
require_relative 'string/trim.rb'
|
|
57
58
|
require_relative 'string/unbracket.rb'
|
|
58
59
|
require_relative 'string/underscore.rb'
|
|
59
60
|
require_relative 'string/unfold.rb'
|
|
@@ -2,7 +2,7 @@ class Time
|
|
|
2
2
|
|
|
3
3
|
# Returns a new Time where one or more of the elements
|
|
4
4
|
# have been changed according to the +options+ parameter.
|
|
5
|
-
# The time options (hour,
|
|
5
|
+
# The time options (hour, min, sec, usec) reset
|
|
6
6
|
# cascadingly, so if only the hour is passed, then
|
|
7
7
|
# minute, sec, and usec is set to 0. If the hour and
|
|
8
8
|
# minute is passed, then sec and usec is set to 0.
|
|
@@ -13,34 +13,56 @@ class Time
|
|
|
13
13
|
# t2 = t1.change(:hour => 11)
|
|
14
14
|
# t2.ctime #=> "Wed Dec 31 11:00:00 1969"
|
|
15
15
|
#
|
|
16
|
+
# The options are :year, :month, :day, :hour, :min, :sec, :usec, :nsec
|
|
17
|
+
# and :offset. The new time keeps this time's UTC offset or time zone,
|
|
18
|
+
# unless :offset gives a new one.
|
|
19
|
+
#
|
|
20
|
+
# t = Time.new(2026, 11, 4, 15, 30, 0, "+09:00")
|
|
21
|
+
# t.change(hour: 1) #=> 2026-11-04 01:00:00 +0900
|
|
22
|
+
# t.change(offset: "+01:00") #=> 2026-11-04 15:30:00 +0100
|
|
23
|
+
#
|
|
24
|
+
# It takes the same options and gives the same results as ActiveSupport's
|
|
25
|
+
# Time#change, so the two agree whichever is loaded.
|
|
26
|
+
#
|
|
16
27
|
def change(options)
|
|
17
|
-
|
|
18
|
-
self.
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
opts[:hour] || self.hour,
|
|
24
|
-
opts[:min] || (opts[:hour] ? 0 : self.min),
|
|
25
|
-
opts[:sec] || ((opts[:hour] || opts[:min]) ? 0 : self.sec),
|
|
26
|
-
opts[:usec] || ((opts[:hour] || opts[:min] || opts[:sec]) ? 0 : self.usec)
|
|
27
|
-
)
|
|
28
|
-
end
|
|
28
|
+
year = options.fetch(:year, self.year)
|
|
29
|
+
month = options.fetch(:month, self.month)
|
|
30
|
+
day = options.fetch(:day, self.day)
|
|
31
|
+
hour = options.fetch(:hour, self.hour)
|
|
32
|
+
min = options.fetch(:min, options[:hour] ? 0 : self.min)
|
|
33
|
+
sec = options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : self.sec)
|
|
29
34
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
# options[:min] || (options[:hour] ? 0 : self.min),
|
|
40
|
-
# options[:sec] || ((options[:hour] || options[:min]) ? 0 : self.sec),
|
|
41
|
-
# options[:usec] || ((options[:hour] || options[:min] || options[:sec]) ? 0 : self.usec)
|
|
42
|
-
# )
|
|
43
|
-
# end
|
|
35
|
+
if options[:nsec]
|
|
36
|
+
raise ArgumentError, "can't change both :nsec and :usec: #{options.inspect}" if options[:usec]
|
|
37
|
+
usec = Rational(options[:nsec], 1000)
|
|
38
|
+
else
|
|
39
|
+
reset = options[:hour] || options[:min] || options[:sec]
|
|
40
|
+
usec = options.fetch(:usec, reset ? 0 : Rational(nsec, 1000))
|
|
41
|
+
end
|
|
42
|
+
raise ArgumentError, "argument out of range" if usec >= 1_000_000
|
|
43
|
+
sec += Rational(usec, 1_000_000)
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
if options[:offset]
|
|
46
|
+
self.class.new(year, month, day, hour, min, sec, options[:offset])
|
|
47
|
+
elsif utc?
|
|
48
|
+
self.class.utc(year, month, day, hour, min, sec)
|
|
49
|
+
elsif zone.respond_to?(:utc_to_local)
|
|
50
|
+
# A time zone object, e.g. from TZInfo.
|
|
51
|
+
time = self.class.new(year, month, day, hour, min, sec, zone)
|
|
52
|
+
time += 0 unless time.utc_offset.integer? # a Ruby bug before 3.2.4 and 3.3.1
|
|
53
|
+
# In the hour that repeats when daylight saving time ends, Time.new
|
|
54
|
+
# picks the earlier of the two times. Pick the one with this time's
|
|
55
|
+
# offset instead, if that's the later one.
|
|
56
|
+
shift = time.utc_offset - utc_offset
|
|
57
|
+
later = time + shift if shift > 0
|
|
58
|
+
later && later.utc_offset == utc_offset ? later : time
|
|
59
|
+
elsif zone
|
|
60
|
+
# Local time. isdst picks the right time in the hour that repeats.
|
|
61
|
+
self.class.local(sec, min, hour, day, month, year, nil, nil, isdst, nil)
|
|
62
|
+
else
|
|
63
|
+
# A fixed UTC offset, e.g. +09:00.
|
|
64
|
+
self.class.new(year, month, day, hour, min, sec, utc_offset)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
46
67
|
|
|
68
|
+
end
|
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
class Time
|
|
2
2
|
|
|
3
|
+
# The current time. That's Time.now, unless ActiveSupport's Time.zone is
|
|
4
|
+
# set, in which case it's the current time in that zone, as in
|
|
5
|
+
# ActiveSupport's own Time.current. So the two agree whichever is loaded.
|
|
3
6
|
def self.current
|
|
4
|
-
now
|
|
7
|
+
respond_to?(:zone) && zone ? zone.now : now
|
|
5
8
|
end
|
|
6
9
|
|
|
10
|
+
# Is this time before +other+, or before now if not given?
|
|
7
11
|
def past?(other=nil)
|
|
8
12
|
self < (other || self.class.current)
|
|
9
13
|
end
|
|
10
14
|
|
|
15
|
+
# Is this time after +other+, or after now if not given?
|
|
11
16
|
def future?(other=nil)
|
|
12
17
|
self > (other || self.class.current)
|
|
13
18
|
end
|
|
14
19
|
|
|
15
20
|
end
|
|
16
|
-
|
|
@@ -8,18 +8,14 @@ class Time
|
|
|
8
8
|
# @deprecated Ruby defines Time#to_time once 'date' is loaded.
|
|
9
9
|
# Scheduled for removal after 2027-09-30.
|
|
10
10
|
#
|
|
11
|
+
# Like Ruby's own, it returns +self+, keeping the time's UTC offset.
|
|
12
|
+
#
|
|
11
13
|
def to_time
|
|
12
14
|
warn "Facets' Time#to_time is deprecated. Require 'date' for Ruby's own Time#to_time. " \
|
|
13
15
|
"It will be removed after 2027-09-30.", uplevel: 1
|
|
14
|
-
|
|
16
|
+
self
|
|
15
17
|
end
|
|
16
18
|
|
|
17
|
-
# A method to keep Time, Date and DateTime instances interchangeable
|
|
18
|
-
# on conversions. In this case, it simply returns +self+.
|
|
19
|
-
#def to_time
|
|
20
|
-
# self
|
|
21
|
-
#end
|
|
22
|
-
|
|
23
19
|
end
|
|
24
20
|
|
|
25
21
|
end
|
data/lib/core/facets/version.rb
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
require 'digest'
|
|
2
|
-
require '
|
|
2
|
+
require 'securerandom'
|
|
3
3
|
|
|
4
4
|
module Digest
|
|
5
5
|
|
|
6
6
|
module Instance
|
|
7
|
+
# The digest of +str+ followed by +salt+, with the salt appended.
|
|
8
|
+
# With +salt+ :auto, the salt is +digest_length+ random bytes from
|
|
9
|
+
# SecureRandom.
|
|
10
|
+
#
|
|
7
11
|
# CREDIT: Guido De Rosa
|
|
8
12
|
def salted_digest(str='', salt=:auto)
|
|
9
13
|
if salt == :auto
|
|
10
|
-
salt =
|
|
14
|
+
salt = SecureRandom.random_bytes(digest_length)
|
|
11
15
|
end
|
|
12
16
|
digest(str + salt) + salt
|
|
13
17
|
end
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
require 'fileutils'
|
|
2
|
+
|
|
1
3
|
module FileUtils
|
|
2
4
|
|
|
3
|
-
unless
|
|
5
|
+
unless respond_to?(:ln_r)
|
|
4
6
|
|
|
5
7
|
#
|
|
6
8
|
# Options: noop verbose dereference_root remove_destination
|
|
@@ -25,6 +27,9 @@ module FileUtils
|
|
|
25
27
|
# FileUtils.ln_r 'src/.', 'dest' # cp_r('src', 'dest') makes src/dest,
|
|
26
28
|
# # but this doesn't.
|
|
27
29
|
#
|
|
30
|
+
# It is much like Ruby's own FileUtils.cp_lr, and uses Ruby's
|
|
31
|
+
# FileUtils.link_entry to make the links.
|
|
32
|
+
#
|
|
28
33
|
# TODO: Why --remove-destination and not just --force?
|
|
29
34
|
def ln_r(src, dest, options = {})
|
|
30
35
|
fu_output_message "ln -r#{options[:remove_destination] ? ' --remove-destination' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
|
|
@@ -37,61 +42,7 @@ module FileUtils
|
|
|
37
42
|
end
|
|
38
43
|
module_function :ln_r
|
|
39
44
|
|
|
40
|
-
OPT_TABLE =
|
|
41
|
-
OPT_TABLE['ln_r'] = [:noop, :verbose, :dereference_root, :remove_destination]
|
|
42
|
-
|
|
43
|
-
#
|
|
44
|
-
# Hard links a file system entry +src+ to +dest+.
|
|
45
|
-
# If +src+ is a directory, this method links its contents recursively.
|
|
46
|
-
#
|
|
47
|
-
# Both of +src+ and +dest+ must be a path name.
|
|
48
|
-
# +src+ must exist, +dest+ must not exist.
|
|
49
|
-
#
|
|
50
|
-
# If +dereference_root+ is true, this method dereference tree root.
|
|
51
|
-
#
|
|
52
|
-
# If +remove_destination+ is true, this method removes each destination file before copy.
|
|
53
|
-
#
|
|
54
|
-
def link_entry(src, dest, dereference_root = false, remove_destination = false)
|
|
55
|
-
Entry_.new(src, nil, dereference_root).traverse do |ent|
|
|
56
|
-
destent = Entry_.new(dest, ent.rel, false)
|
|
57
|
-
File.unlink destent.path if remove_destination && File.file?(destent.path)
|
|
58
|
-
ent.link destent.path
|
|
59
|
-
end
|
|
60
|
-
end
|
|
61
|
-
module_function :link_entry
|
|
62
|
-
|
|
63
|
-
class Entry_ #:nodoc:
|
|
64
|
-
def link(dest)
|
|
65
|
-
case
|
|
66
|
-
when directory?
|
|
67
|
-
if !File.exist?(dest) and descendant_diretory?(dest, path)
|
|
68
|
-
raise ArgumentError, "cannot link directory %s to itself %s" % [path, dest]
|
|
69
|
-
end
|
|
70
|
-
begin
|
|
71
|
-
Dir.mkdir dest
|
|
72
|
-
rescue
|
|
73
|
-
raise unless File.directory?(dest)
|
|
74
|
-
end
|
|
75
|
-
else file?
|
|
76
|
-
File.link path(), dest
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
unless const_defined?(:DIRECTORY_TERM)
|
|
81
|
-
if File::ALT_SEPARATOR
|
|
82
|
-
DIRECTORY_TERM = "(?=[/#{Regexp.quote(File::ALT_SEPARATOR)}]|\\z)".freeze
|
|
83
|
-
else
|
|
84
|
-
DIRECTORY_TERM = "(?=/|\\z)".freeze
|
|
85
|
-
end
|
|
86
|
-
SYSCASE = File::FNM_SYSCASE.nonzero? ? "-i" : ""
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
unless method_defined?(:descendant_diretory?)
|
|
90
|
-
def descendant_diretory?(descendant, ascendant)
|
|
91
|
-
/\A(?#{SYSCASE}:#{Regexp.quote(ascendant)})#{DIRECTORY_TERM}/ =~ File.dirname(descendant)
|
|
92
|
-
end
|
|
93
|
-
end
|
|
94
|
-
end
|
|
45
|
+
OPT_TABLE['ln_r'] = [:noop, :verbose, :dereference_root, :remove_destination] if const_defined?(:OPT_TABLE) && !OPT_TABLE.frozen?
|
|
95
46
|
|
|
96
47
|
end
|
|
97
48
|
|
|
@@ -2,6 +2,9 @@ require 'ostruct'
|
|
|
2
2
|
|
|
3
3
|
class OpenStruct
|
|
4
4
|
|
|
5
|
+
alias_method :__initialize_without_block__, :initialize
|
|
6
|
+
private :__initialize_without_block__
|
|
7
|
+
|
|
5
8
|
# Allows the initialization of an OpenStruct with a setter block:
|
|
6
9
|
#
|
|
7
10
|
# person = OpenStruct.new do |o|
|
|
@@ -17,14 +20,12 @@ class OpenStruct
|
|
|
17
20
|
# p.gender = :M
|
|
18
21
|
# end
|
|
19
22
|
#
|
|
23
|
+
# The hash is handled by Ruby's own OpenStruct#initialize, which this
|
|
24
|
+
# calls first.
|
|
25
|
+
#
|
|
20
26
|
# CREDIT: Noah Gibbs, Gavin Sinclair
|
|
21
27
|
def initialize(hash=nil, &block)
|
|
22
|
-
|
|
23
|
-
if hash
|
|
24
|
-
hash.each_pair do |k, v|
|
|
25
|
-
self[k.to_sym] = v
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
+
__initialize_without_block__(hash)
|
|
28
29
|
if block && block.arity == 1
|
|
29
30
|
yield self
|
|
30
31
|
end
|
|
@@ -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
|
|
|
@@ -67,21 +67,65 @@ class Array
|
|
|
67
67
|
# The array is accepted in the format of Ruby
|
|
68
68
|
# method arguments --ie. [arg1, arg2, ..., hash]
|
|
69
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
|
+
#
|
|
70
76
|
def shellwords
|
|
71
77
|
opts, args = *flatten.partition{ |e| Hash === e }
|
|
72
78
|
opts = opts.inject({}){ |m,h| m.update(h); m }
|
|
73
79
|
opts.shellwords + args
|
|
74
80
|
end
|
|
75
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.
|
|
76
92
|
#
|
|
77
|
-
|
|
78
|
-
|
|
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
|
|
79
103
|
end
|
|
80
104
|
|
|
81
105
|
end
|
|
82
106
|
|
|
107
|
+
Array.prepend(Facets::ArrayShelljoin)
|
|
108
|
+
|
|
83
109
|
class Hash
|
|
84
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"
|
|
85
129
|
#
|
|
86
130
|
def shellwords
|
|
87
131
|
argv = []
|
|
@@ -91,17 +135,21 @@ class Hash
|
|
|
91
135
|
when false, nil
|
|
92
136
|
when Array
|
|
93
137
|
v.each do |e|
|
|
94
|
-
argv <<
|
|
138
|
+
argv << "#{m}#{f}=#{e}"
|
|
95
139
|
end
|
|
96
140
|
when true
|
|
97
|
-
argv <<
|
|
141
|
+
argv << "#{m}#{f}"
|
|
98
142
|
else
|
|
99
|
-
argv <<
|
|
143
|
+
argv << "#{m}#{f}=#{v}"
|
|
100
144
|
end
|
|
101
145
|
end
|
|
102
146
|
argv
|
|
103
147
|
end
|
|
104
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"
|
|
105
153
|
#
|
|
106
154
|
def shelljoin
|
|
107
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
|
|