pixar-ruby-extensions 1.11.1
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 +7 -0
- data/LICENSE.txt +177 -0
- data/README.md +29 -0
- data/lib/pixar-ruby-extensions.rb +10 -0
- data/lib/pixar_ruby_extensions/array/predicates.rb +30 -0
- data/lib/pixar_ruby_extensions/array/utils.rb +88 -0
- data/lib/pixar_ruby_extensions/array.rb +17 -0
- data/lib/pixar_ruby_extensions/filetest/predicates.rb +38 -0
- data/lib/pixar_ruby_extensions/filetest.rb +14 -0
- data/lib/pixar_ruby_extensions/float/utils.rb +36 -0
- data/lib/pixar_ruby_extensions/float.rb +14 -0
- data/lib/pixar_ruby_extensions/hash/utils.rb +94 -0
- data/lib/pixar_ruby_extensions/hash.rb +15 -0
- data/lib/pixar_ruby_extensions/integer/utils.rb +187 -0
- data/lib/pixar_ruby_extensions/integer.rb +14 -0
- data/lib/pixar_ruby_extensions/ipaddr/predicates.rb +50 -0
- data/lib/pixar_ruby_extensions/ipaddr/utils.rb +77 -0
- data/lib/pixar_ruby_extensions/ipaddr.rb +20 -0
- data/lib/pixar_ruby_extensions/json/jsonl.rb +50 -0
- data/lib/pixar_ruby_extensions/json/utils.rb +78 -0
- data/lib/pixar_ruby_extensions/json.rb +17 -0
- data/lib/pixar_ruby_extensions/object/predicates.rb +49 -0
- data/lib/pixar_ruby_extensions/object.rb +15 -0
- data/lib/pixar_ruby_extensions/pathname/predicates.rb +38 -0
- data/lib/pixar_ruby_extensions/pathname/utils.rb +139 -0
- data/lib/pixar_ruby_extensions/pathname.rb +18 -0
- data/lib/pixar_ruby_extensions/string/conversions.rb +113 -0
- data/lib/pixar_ruby_extensions/string/predicates.rb +52 -0
- data/lib/pixar_ruby_extensions/string.rb +17 -0
- data/lib/pixar_ruby_extensions/time/utils.rb +46 -0
- data/lib/pixar_ruby_extensions/time.rb +16 -0
- data/lib/pixar_ruby_extensions/version.rb +15 -0
- data/lib/pixar_ruby_extensions.rb +44 -0
- metadata +78 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5a04889a5f74e6ac42d916b2eaa70fdb2c4fc32440e9e4197216b75f7fe14782
|
4
|
+
data.tar.gz: dd3b29c31ef90da3a272bf24f35647bdec3411495f61b5a0ec634dbc9a3aa17d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1211b16e2eff5e1b8868e0ddf3c7ba6e26f4d91b15f9e44cd752c5157e1b92776562c36e5ccd4caba2b9129f268af0839ed62326636f3a7003f7d1e2f84b20d0
|
7
|
+
data.tar.gz: 4d9ec47bf87cb4eb2d9d7389ac1a71c64a3d93cc789e6dbc00d3ab91a09fe688ec825abba7220415ebd04e037770c8e1a680ca5ff0bd5417e0162c01e1f21ad5
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,177 @@
|
|
1
|
+
|
2
|
+
TOMORROW OPEN SOURCE TECHNOLOGY LICENSE 1.0
|
3
|
+
|
4
|
+
The Tomorrow Open Source Technology License 1.0 differs from the original Apache License 2.0 in the following manner. The scope of the trademark grant in section 6 is different.
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor
|
141
|
+
and its affiliates, except as required to comply with Section 4(c) of
|
142
|
+
the License and to reproduce the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Pixar's Ruby Extensions
|
2
|
+
|
3
|
+
This gem defines extensions to modules and classes in Ruby's Core and Standard Library. We've put them here because we've found ourselves performing these tasks repeatedly in our code over the years and we like to stay DRY.
|
4
|
+
|
5
|
+
They are also used in our open-source projects, and those will be migrating to use this gem - also in pursuit of DRYness.
|
6
|
+
|
7
|
+
To ease troubleshooting and prevent name collisions:
|
8
|
+
|
9
|
+
- Methods are not directly monkey-patched into the Core or StdLib classes/modules.
|
10
|
+
- Instead they are defined in modules under `PixarRubyExtensions` with obvious namespaces, and mixed-in to the Core or StdLib classes/modules.
|
11
|
+
|
12
|
+
This provides _much_ easier debugging when there are exceptions, since the error and backtrace will indicate exactly where these methods are defined.
|
13
|
+
|
14
|
+
- With only a couple of exceptions, all new method names are prefixed with `pix_`
|
15
|
+
- See `PixarRubyExtensions::IPAddr::Predicates` for examples and explanation about the exceptions.
|
16
|
+
|
17
|
+
### Usage
|
18
|
+
|
19
|
+
To load all the extensions: `require 'pixar_ruby_extensions'`
|
20
|
+
|
21
|
+
To load only one: `require 'pixar_ruby_extensions/integer'`
|
22
|
+
|
23
|
+
### Credits
|
24
|
+
|
25
|
+
- The modularization idea came from [this page about responsible monkey-patching](https://www.justinweiss.com/articles/3-ways-to-monkey-patch-without-making-a-mess/), many thanks to the author and the discussion thread.
|
26
|
+
|
27
|
+
### ToDo
|
28
|
+
|
29
|
+
- Investigate re-doing all this using refinements rather than monkey-patching
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# Copyright 2025 Pixar
|
2
|
+
#
|
3
|
+
# Licensed under the terms set forth in the LICENSE.txt file available at
|
4
|
+
# at the root of this project.
|
5
|
+
|
6
|
+
# frozen_string_literal: true
|
7
|
+
|
8
|
+
module PixarRubyExtensions
|
9
|
+
|
10
|
+
module ArrayExtensions
|
11
|
+
|
12
|
+
module Predicates
|
13
|
+
|
14
|
+
# A case-insensitive version of #include? for Arrays.
|
15
|
+
# It only considers elements that are Strings
|
16
|
+
# (well, that respond to .casecmp?)
|
17
|
+
#
|
18
|
+
# @param somestring [String] the String to search for
|
19
|
+
#
|
20
|
+
# @return [Boolean] Does the Array contain the String, ignoring case?
|
21
|
+
#
|
22
|
+
def pix_ci_include?(somestring)
|
23
|
+
any? { |s| s.respond_to?(:casecmp?) && s.casecmp?(somestring) }
|
24
|
+
end
|
25
|
+
|
26
|
+
end # module
|
27
|
+
|
28
|
+
end # module
|
29
|
+
|
30
|
+
end # module
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# Copyright 2025 Pixar
|
2
|
+
#
|
3
|
+
# Licensed under the terms set forth in the LICENSE.txt file available at
|
4
|
+
# at the root of this project.
|
5
|
+
|
6
|
+
# frozen_string_literal: true
|
7
|
+
|
8
|
+
module PixarRubyExtensions
|
9
|
+
|
10
|
+
module ArrayExtensions
|
11
|
+
|
12
|
+
# Useful monkey patches for Array
|
13
|
+
module Utils
|
14
|
+
|
15
|
+
# Fetch a string from an Array case-insensitively,
|
16
|
+
# e.g. if my_array contains 'thrasher',
|
17
|
+
# my_array.pix_ci_fetch('ThRashEr')
|
18
|
+
# will return 'thrasher'
|
19
|
+
#
|
20
|
+
# It only considers elements that are Strings
|
21
|
+
# (well, that respond to .casecmp?)
|
22
|
+
#
|
23
|
+
# returns nil if no match
|
24
|
+
#
|
25
|
+
# @param somestring [String] the String to search for
|
26
|
+
#
|
27
|
+
# @return [String, nil] The matching string as it exists in the Array,
|
28
|
+
# nil if it doesn't exist
|
29
|
+
#
|
30
|
+
def pix_ci_fetch(somestring)
|
31
|
+
each { |s| return s if s.respond_to?(:casecmp?) && s.casecmp?(somestring) }
|
32
|
+
nil
|
33
|
+
end
|
34
|
+
|
35
|
+
# Array.pix_string_split
|
36
|
+
# Splits string elements of an Array on a given delimiter.
|
37
|
+
# Preserves order relative to the position in the original Array.
|
38
|
+
#
|
39
|
+
# @param [String] Split delimeter
|
40
|
+
# @return [Array]
|
41
|
+
#
|
42
|
+
def pix_string_split!(delim)
|
43
|
+
map! { |s| s.is_a?(String) ? s.split(delim) : s }
|
44
|
+
flatten!
|
45
|
+
end # end pix_string_split
|
46
|
+
|
47
|
+
# These handy padding methods from
|
48
|
+
# https://stackoverflow.com/questions/5608918/pad-an-array-to-be-a-certain-size
|
49
|
+
#
|
50
|
+
# The [l|r]just method names mirror those in String
|
51
|
+
# The aliases are a little more Englishy
|
52
|
+
def pix_rjust!(to_len, val)
|
53
|
+
insert(0, *Array.new([0, to_len - length].max, val))
|
54
|
+
end
|
55
|
+
alias pix_padleft! pix_rjust!
|
56
|
+
|
57
|
+
def pix_ljust!(to_len, val)
|
58
|
+
fill(val, length...to_len)
|
59
|
+
end
|
60
|
+
alias pix_padright! pix_ljust!
|
61
|
+
|
62
|
+
def pix_rjust(to_len, val)
|
63
|
+
Array.new([0, to_len - length].max, val) + self
|
64
|
+
end
|
65
|
+
alias pix_padleft pix_rjust
|
66
|
+
|
67
|
+
def pix_ljust(to_len, val)
|
68
|
+
dup.fill(val, length...to_len)
|
69
|
+
end
|
70
|
+
alias pix_padright pix_ljust
|
71
|
+
|
72
|
+
# Array#pix_to_jsonl
|
73
|
+
#
|
74
|
+
# Convert an Array of objects to a jsonlines string
|
75
|
+
#
|
76
|
+
# @param opts [Hash] Options to pass to JSON.dump
|
77
|
+
#
|
78
|
+
# @return [String] The jsonlines string
|
79
|
+
#
|
80
|
+
def pix_to_jsonl(**opts)
|
81
|
+
map { |obj| JSON.generate(obj, **opts) }.join("\n")
|
82
|
+
end
|
83
|
+
|
84
|
+
end # module
|
85
|
+
|
86
|
+
end # module
|
87
|
+
|
88
|
+
end # module
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# Copyright 2025 Pixar
|
2
|
+
#
|
3
|
+
# Licensed under the terms set forth in the LICENSE.txt file available at
|
4
|
+
# at the root of this project.
|
5
|
+
|
6
|
+
# frozen_string_literal: true
|
7
|
+
|
8
|
+
require 'pixar_ruby_extensions/array/predicates'
|
9
|
+
require 'pixar_ruby_extensions/array/utils'
|
10
|
+
|
11
|
+
# an array
|
12
|
+
class Array
|
13
|
+
|
14
|
+
include PixarRubyExtensions::ArrayExtensions::Predicates
|
15
|
+
include PixarRubyExtensions::ArrayExtensions::Utils
|
16
|
+
|
17
|
+
end # class
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# Copyright 2025 Pixar
|
2
|
+
#
|
3
|
+
# Licensed under the terms set forth in the LICENSE.txt file available at
|
4
|
+
# at the root of this project.
|
5
|
+
|
6
|
+
# frozen_string_literal: true
|
7
|
+
|
8
|
+
module PixarRubyExtensions
|
9
|
+
|
10
|
+
module FileTestExtensions
|
11
|
+
|
12
|
+
module Predicates
|
13
|
+
|
14
|
+
# FileTest.file? returns true if
|
15
|
+
# the item is a symlink pointing to a regular file.
|
16
|
+
#
|
17
|
+
# This test, real_file?, returns true if the item is
|
18
|
+
# a regular file but NOT a symlink.
|
19
|
+
#
|
20
|
+
def pix_real_file?(path)
|
21
|
+
FileTest.file?(path) && !FileTest.symlink?(path)
|
22
|
+
end # real_file?
|
23
|
+
|
24
|
+
# FileTest.directory? returns true if
|
25
|
+
# the item is a symlink pointing to a directory.
|
26
|
+
#
|
27
|
+
# This test, real_directory?, returns true if the item is
|
28
|
+
# a directory but NOT a symlink.
|
29
|
+
#
|
30
|
+
def pix_real_directory?(path)
|
31
|
+
FileTest.directory?(path) && !FileTest.symlink?(path)
|
32
|
+
end # real_file?
|
33
|
+
|
34
|
+
end # module
|
35
|
+
|
36
|
+
end # module
|
37
|
+
|
38
|
+
end # module
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# Copyright 2025 Pixar
|
2
|
+
#
|
3
|
+
# Licensed under the terms set forth in the LICENSE.txt file available at
|
4
|
+
# at the root of this project.
|
5
|
+
|
6
|
+
# frozen_string_literal: true
|
7
|
+
|
8
|
+
require 'pixar_ruby_extensions/filetest/predicates'
|
9
|
+
|
10
|
+
module FileTest
|
11
|
+
|
12
|
+
extend PixarRubyExtensions::FileTestExtensions::Predicates
|
13
|
+
|
14
|
+
end # module FileTest
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# Copyright 2025 Pixar
|
2
|
+
#
|
3
|
+
# Licensed under the terms set forth in the LICENSE.txt file available at
|
4
|
+
# at the root of this project.
|
5
|
+
|
6
|
+
# frozen_string_literal: true
|
7
|
+
|
8
|
+
require 'pixar_ruby_extensions/integer'
|
9
|
+
|
10
|
+
module PixarRubyExtensions
|
11
|
+
|
12
|
+
module FloatExtensions
|
13
|
+
|
14
|
+
module Utils
|
15
|
+
|
16
|
+
# See IntegerExtensions::Utils.pix_humanize_secs for more info
|
17
|
+
# Runs that method on the integer part of the float, then append the fraction
|
18
|
+
# as a decimal part of a second.
|
19
|
+
#
|
20
|
+
# @return [String] The integer number of seconds, expressed in descending English time units
|
21
|
+
#
|
22
|
+
def pix_humanize_secs(omit_zeros: false, down_to: :seconds, up_to: :years)
|
23
|
+
int, fraction = to_s.split('.')
|
24
|
+
int.to_i.pix_humanize_secs(
|
25
|
+
omit_zeros: omit_zeros,
|
26
|
+
down_to: down_to,
|
27
|
+
up_to: up_to,
|
28
|
+
fraction: fraction
|
29
|
+
)
|
30
|
+
end
|
31
|
+
|
32
|
+
end # module
|
33
|
+
|
34
|
+
end # module
|
35
|
+
|
36
|
+
end # module
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# Copyright 2025 Pixar
|
2
|
+
#
|
3
|
+
# Licensed under the terms set forth in the LICENSE.txt file available at
|
4
|
+
# at the root of this project.
|
5
|
+
|
6
|
+
# frozen_string_literal: true
|
7
|
+
|
8
|
+
require 'pixar_ruby_extensions/float/utils'
|
9
|
+
|
10
|
+
class Float
|
11
|
+
|
12
|
+
include PixarRubyExtensions::FloatExtensions::Utils
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
# Copyright 2025 Pixar
|
2
|
+
#
|
3
|
+
# Licensed under the terms set forth in the LICENSE.txt file available at
|
4
|
+
# at the root of this project.
|
5
|
+
|
6
|
+
# frozen_string_literal: true
|
7
|
+
|
8
|
+
module PixarRubyExtensions
|
9
|
+
|
10
|
+
module HashExtensions
|
11
|
+
|
12
|
+
module Utils
|
13
|
+
|
14
|
+
# Convert certain Hash values to nil, optionally recursively
|
15
|
+
#
|
16
|
+
# This is useful when a dataset lacks consistency as to
|
17
|
+
# whether unset values come as nils or empty strings.
|
18
|
+
#
|
19
|
+
# By default this method converts all empty strings to nils,
|
20
|
+
# but other target strings can be provided.
|
21
|
+
#
|
22
|
+
# With no block, values equalling the given String (default is the empty string),
|
23
|
+
# or any member of a given Array of Strings will be converted to nil.
|
24
|
+
# Equality is evaluated with Array#include?
|
25
|
+
#
|
26
|
+
# With a block, the to_nils param is ignored, and if the result of the block
|
27
|
+
# evaluates to true, the value is converted to nil.
|
28
|
+
#
|
29
|
+
# Subhashes are ignored unless recurse is true.
|
30
|
+
#
|
31
|
+
# @param to_nils[String,Array] Hash values equal to (==) these become nil. Defaults to empty string
|
32
|
+
#
|
33
|
+
# @param recurse[Boolean] should sub-Hashes be nillified?
|
34
|
+
#
|
35
|
+
# @yield [value] Hash values for which the block returns true will become nil.
|
36
|
+
#
|
37
|
+
# @return [Hash] the hash with the desired values converted to nil
|
38
|
+
#
|
39
|
+
# @example
|
40
|
+
# hash = {:foo => '', :bar => {:baz => '' }}
|
41
|
+
# hash.jss_nillify! # {:foo => nil, :bar => {:baz => '' }}
|
42
|
+
#
|
43
|
+
# hash = {:foo => '', :bar => {:baz => '' }}
|
44
|
+
# hash.jss_nillify! '', :recurse # {:foo => nil, :bar => {:baz => nil }}
|
45
|
+
#
|
46
|
+
# hash = {:foo => 123, :bar => {:baz => '', :bim => "123" }}
|
47
|
+
# hash.jss_nillify! ['', 123], :recurse # {:foo => nil, :bar => {:baz => nil, :bim => "123" }}
|
48
|
+
#
|
49
|
+
# hash = {:foo => 123, :bar => {:baz => '', :bim => "123" }}
|
50
|
+
# hash.jss_nillify!(:anything, :recurse){|v| v.to_i == 123 } # {:foo => nil, :bar => {:baz => '', :bim => nil }}
|
51
|
+
#
|
52
|
+
def pix_nillify(to_nils = '', recurse: false, &block)
|
53
|
+
p_nillifier(to_nils, recurse: recurse, destructive: false, &block)
|
54
|
+
end # def nillify
|
55
|
+
|
56
|
+
# Modify self in-place.
|
57
|
+
#
|
58
|
+
# @see p_nillify
|
59
|
+
#
|
60
|
+
def pix_nillify!(to_nils = '', recurse: false, &block)
|
61
|
+
p_nillifier(to_nils, recurse: recurse, destructive: true, &block)
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def pix_nillifier(to_nils, recurse:, destructive:, &block)
|
67
|
+
nillify_these = [to_nils].flatten
|
68
|
+
|
69
|
+
if destructive
|
70
|
+
transformer = :transform_values!
|
71
|
+
nillifier = :p_nillify!
|
72
|
+
else
|
73
|
+
nillifier = :p_nillify
|
74
|
+
transformer = :transform_values
|
75
|
+
end
|
76
|
+
|
77
|
+
send transformer do |v|
|
78
|
+
if v.is_a?(Hash) && recurse
|
79
|
+
# puts :recursing
|
80
|
+
v = v.send(nillifier, to_nils, recurse: recurse, &block)
|
81
|
+
# puts "new v is: #{v}"
|
82
|
+
v
|
83
|
+
else
|
84
|
+
do_it = block_given? ? yield(v) : nillify_these.include?(v)
|
85
|
+
do_it ? nil : v
|
86
|
+
end # if
|
87
|
+
end # transform values
|
88
|
+
end
|
89
|
+
|
90
|
+
end # module
|
91
|
+
|
92
|
+
end # module
|
93
|
+
|
94
|
+
end # module
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# Copyright 2025 Pixar
|
2
|
+
#
|
3
|
+
# Licensed under the terms set forth in the LICENSE.txt file available at
|
4
|
+
# at the root of this project.
|
5
|
+
|
6
|
+
# frozen_string_literal: true
|
7
|
+
|
8
|
+
require 'pixar_ruby_extensions/hash/utils'
|
9
|
+
|
10
|
+
# include the modules loaded above
|
11
|
+
class Hash
|
12
|
+
|
13
|
+
include PixarRubyExtensions::HashExtensions::Utils
|
14
|
+
|
15
|
+
end
|