everythingrb 0.6.1 → 0.7.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/CHANGELOG.md +45 -1
- data/README.md +74 -4
- data/lib/everythingrb/all.rb +8 -0
- data/lib/everythingrb/array.rb +2 -0
- data/lib/everythingrb/boolean.rb +33 -0
- data/lib/everythingrb/data.rb +3 -0
- data/lib/everythingrb/date.rb +31 -0
- data/lib/everythingrb/extensions/quotable.rb +79 -0
- data/lib/everythingrb/extensions.rb +3 -0
- data/lib/everythingrb/hash.rb +98 -0
- data/lib/everythingrb/kernel.rb +31 -0
- data/lib/everythingrb/nil.rb +15 -0
- data/lib/everythingrb/numeric.rb +18 -0
- data/lib/everythingrb/ostruct.rb +3 -0
- data/lib/everythingrb/prelude.rb +1 -0
- data/lib/everythingrb/range.rb +17 -0
- data/lib/everythingrb/regexp.rb +16 -0
- data/lib/everythingrb/string.rb +2 -15
- data/lib/everythingrb/struct.rb +3 -0
- data/lib/everythingrb/symbol.rb +3 -3
- data/lib/everythingrb/time.rb +16 -0
- data/lib/everythingrb/version.rb +1 -1
- metadata +12 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a8d091206968f382cb9d1539fb05c31eaf80ac63842db773c7b0c10ae81a757f
|
4
|
+
data.tar.gz: be5753c698445e4efc5c476f08ebc696d304cf8566303bd57df771ff0a6a0ba2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ece0c1ccf513a9bfb2a77bd405b88e3dd09cffd2e1c07ef377b94582018ce18a2a3b9dd981c73b0107eb9fb1b1b82d0df852b54cf1daff38e61a2ca6850cc6c9
|
7
|
+
data.tar.gz: 5ede278236fc54a3f6297d8a54ef0bd77bcea530b7e58cab98861ee3d8676bedd090aceccfdeb8527349027fbf1d8f80d3a493c2c8140c798e143fa7b2761b6e
|
data/CHANGELOG.md
CHANGED
@@ -15,6 +15,49 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
15
15
|
### Removed
|
16
16
|
-->
|
17
17
|
|
18
|
+
## [Unreleased]
|
19
|
+
|
20
|
+
### Added
|
21
|
+
|
22
|
+
### Changed
|
23
|
+
|
24
|
+
### Removed
|
25
|
+
|
26
|
+
## [0.7.0] - 12025-05-04
|
27
|
+
|
28
|
+
### Added
|
29
|
+
|
30
|
+
- Created `Everythingrb::InspectQuotable` and `Everythingrb::StringQuotable` modules for consistent quote functionality
|
31
|
+
- Extended quotable functionality to many more Ruby core classes:
|
32
|
+
- `TrueClass` and `FalseClass` (boolean values)
|
33
|
+
- `NilClass`
|
34
|
+
- `Numeric`
|
35
|
+
- `Range`
|
36
|
+
- `Regexp`
|
37
|
+
- `Time`, `Date`, and `DateTime`
|
38
|
+
- `Data` and `Struct` classes
|
39
|
+
- Added new Hash methods for filtering based on values only:
|
40
|
+
- `Hash#select_values` - Returns a new hash with entries where the block returns true for the value
|
41
|
+
- `Hash#select_values!` - Same as `select_values` but modifies the hash in place
|
42
|
+
- `Hash#reject_values` - Returns a new hash with entries where the block returns false for the value
|
43
|
+
- `Hash#reject_values!` - Same as `reject_values` but modifies the hash in place
|
44
|
+
- Added `filter_values` and `filter_values!` as aliases for `select_values` and `select_values!` respectively
|
45
|
+
- Added `Kernel#morph` as an alias for `then` (and `yield_self`) that better communicates transformation intent:
|
46
|
+
```ruby
|
47
|
+
# Instead of this:
|
48
|
+
value.then { |v| transform_somehow(v) }
|
49
|
+
|
50
|
+
# You can write this:
|
51
|
+
value.morph { |v| transform_somehow(v) }
|
52
|
+
```
|
53
|
+
|
54
|
+
### Changed
|
55
|
+
|
56
|
+
- Refactored existing quotable implementations to use the new modules
|
57
|
+
- Standardized method order to prefer `in_quotes` as the primary method with `with_quotes` as an alias
|
58
|
+
|
59
|
+
### Removed
|
60
|
+
|
18
61
|
## [0.6.1] - 12025-04-26
|
19
62
|
|
20
63
|
### Changed
|
@@ -221,7 +264,8 @@ This change aligns our method signatures with Ruby's conventions and matches our
|
|
221
264
|
|
222
265
|
- Added alias `each` to `each_pair` in OpenStruct for better enumerable compatibility
|
223
266
|
|
224
|
-
[unreleased]: https://github.com/itsthedevman/everythingrb/compare/v0.
|
267
|
+
[unreleased]: https://github.com/itsthedevman/everythingrb/compare/v0.7.0...HEAD
|
268
|
+
[0.7.0]: https://github.com/itsthedevman/everythingrb/compare/v0.6.1...v0.7.0
|
225
269
|
[0.6.1]: https://github.com/itsthedevman/everythingrb/compare/v0.6.0...v0.6.1
|
226
270
|
[0.6.0]: https://github.com/itsthedevman/everythingrb/compare/v0.5.0...v0.6.0
|
227
271
|
[0.5.0]: https://github.com/itsthedevman/everythingrb/compare/v0.4.0...v0.5.0
|
data/README.md
CHANGED
@@ -64,14 +64,22 @@ require "everythingrb/string" # Just String extensions
|
|
64
64
|
|
65
65
|
Available modules:
|
66
66
|
- `array`: Array extensions (join_map, key_map, etc.)
|
67
|
-
- `
|
67
|
+
- `boolean`: Boolean extensions (in_quotes, with_quotes)
|
68
|
+
- `data`: Data extensions (to_deep_h, in_quotes)
|
69
|
+
- `date`: Date and DateTime extensions (in_quotes)
|
68
70
|
- `enumerable`: Enumerable extensions (join_map, group_by_key)
|
69
71
|
- `hash`: Hash extensions (to_ostruct, deep_freeze, etc.)
|
72
|
+
- `kernel`: Kernel extensions (morph alias for then)
|
70
73
|
- `module`: Extensions like attr_predicate
|
74
|
+
- `nil`: NilClass extensions (in_quotes)
|
75
|
+
- `numeric`: Numeric extensions (in_quotes)
|
71
76
|
- `ostruct`: OpenStruct extensions (map, join_map, etc.)
|
77
|
+
- `range`: Range extensions (in_quotes)
|
78
|
+
- `regexp`: Regexp extensions (in_quotes)
|
72
79
|
- `string`: String extensions (to_h, to_ostruct, etc.)
|
73
|
-
- `struct`: Struct extensions (to_deep_h)
|
80
|
+
- `struct`: Struct extensions (to_deep_h, in_quotes)
|
74
81
|
- `symbol`: Symbol extensions (with_quotes)
|
82
|
+
- `time`: Time extensions (in_quotes)
|
75
83
|
|
76
84
|
## What's Included
|
77
85
|
|
@@ -88,6 +96,10 @@ config.server.host # => "example.com"
|
|
88
96
|
|
89
97
|
# Parse JSON directly to your preferred structure
|
90
98
|
'{"user":{"name":"Alice"}}'.to_istruct.user.name # => "Alice"
|
99
|
+
|
100
|
+
# Deep conversion to plain hashes
|
101
|
+
data = OpenStruct.new(user: Data.define(:name).new(name: "Bob"))
|
102
|
+
data.to_deep_h # => {user: {name: "Bob"}}
|
91
103
|
```
|
92
104
|
|
93
105
|
**Extensions:** `to_struct`, `to_ostruct`, `to_istruct`, `to_h`, `to_deep_h`
|
@@ -106,6 +118,10 @@ users.dig_map(:roles, 0) # => ["admin", "user"]
|
|
106
118
|
[1, 2, nil, 3, 4].join_map(" | ") { |n| "Item #{n}" if n&.odd? }
|
107
119
|
# => "Item 1 | Item 3"
|
108
120
|
|
121
|
+
# Group elements by key or nested keys
|
122
|
+
users.group_by_key(:roles, 0)
|
123
|
+
# => {"admin" => [{name: "Alice", roles: ["admin"]}], "user" => [{name: "Bob", roles: ["user"]}]}
|
124
|
+
|
109
125
|
# With ActiveSupport loaded, join arrays in natural language with "or"
|
110
126
|
["red", "blue", "green"].to_or_sentence # => "red, blue, or green"
|
111
127
|
```
|
@@ -145,10 +161,20 @@ stats[:server][:region][:us_east][:errors] << "Connection timeout"
|
|
145
161
|
users.transform_values(with_key: true) { |v, k| "User #{k}: #{v[:name]}" }
|
146
162
|
|
147
163
|
# Find values based on conditions
|
148
|
-
users.
|
164
|
+
users.value_where { |_k, v| v[:role] == "admin" } # Returns first admin
|
165
|
+
users.values_where { |_k, v| v[:role] == "admin" } # Returns all admins
|
166
|
+
|
167
|
+
# Rename keys while preserving order
|
168
|
+
config = {api_key: "secret", timeout: 30}
|
169
|
+
config.rename_keys(api_key: :key, timeout: :request_timeout)
|
170
|
+
# => {key: "secret", request_timeout: 30}
|
171
|
+
|
172
|
+
# Filter hash by values
|
173
|
+
{a: 1, b: nil, c: 2}.select_values { |v| v.is_a?(Integer) && v > 1 }
|
174
|
+
# => {c: 2}
|
149
175
|
```
|
150
176
|
|
151
|
-
**Extensions:** `new_nested_hash`, `with_key`, `value_where`, `values_where`
|
177
|
+
**Extensions:** `new_nested_hash`, `with_key`, `value_where`, `values_where`, `rename_key(s)`, `select_values`, `reject_values`
|
152
178
|
|
153
179
|
### Array Cleaning
|
154
180
|
|
@@ -164,6 +190,36 @@ Clean up array boundaries while preserving internal structure:
|
|
164
190
|
|
165
191
|
**Extensions:** `trim_nils`, `compact_prefix`, `compact_suffix`, `trim_blanks` (with ActiveSupport)
|
166
192
|
|
193
|
+
### String Formatting
|
194
|
+
|
195
|
+
Add quotation marks to any value with a consistent interface - useful for user messages, formatting, and more:
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
# Wrap any value in quotes - works on ALL types!
|
199
|
+
"hello".in_quotes # => "\"hello\""
|
200
|
+
42.in_quotes # => "\"42\""
|
201
|
+
nil.in_quotes # => "\"nil\""
|
202
|
+
:symbol.in_quotes # => "\"symbol\""
|
203
|
+
[1, 2].in_quotes # => "\"[1, 2]\""
|
204
|
+
Time.now.in_quotes # => "\"2025-05-04 12:34:56 +0000\""
|
205
|
+
|
206
|
+
# Perfect for user-facing messages with mixed types
|
207
|
+
puts "You selected #{selection.in_quotes}"
|
208
|
+
puts "Item #{id.in_quotes} was added to category #{category.in_quotes}"
|
209
|
+
|
210
|
+
# Great for formatting responses
|
211
|
+
response = "Command #{command.in_quotes} completed successfully"
|
212
|
+
|
213
|
+
# Ideal for error messages and logging
|
214
|
+
log.info "Received values: #{values.join_map(", ", &:in_quotes)}"
|
215
|
+
raise "Expected #{expected.in_quotes} but got #{actual.in_quotes}"
|
216
|
+
|
217
|
+
# CLI argument descriptions
|
218
|
+
puts "Valid modes are: #{MODES.join_map(", ", &:in_quotes)}"
|
219
|
+
```
|
220
|
+
|
221
|
+
**Extensions:** `in_quotes`, `with_quotes` (alias)
|
222
|
+
|
167
223
|
### Boolean Methods
|
168
224
|
|
169
225
|
Create predicate methods with minimal code:
|
@@ -188,6 +244,20 @@ person.active? # => false
|
|
188
244
|
|
189
245
|
**Extensions:** `attr_predicate`
|
190
246
|
|
247
|
+
### Value Transformation
|
248
|
+
|
249
|
+
Chain transformations with a more descriptive syntax:
|
250
|
+
|
251
|
+
```ruby
|
252
|
+
# Instead of this:
|
253
|
+
result = value.then { |v| transform_it(v) }
|
254
|
+
|
255
|
+
# Write this:
|
256
|
+
result = value.morph { |v| transform_it(v) }
|
257
|
+
```
|
258
|
+
|
259
|
+
**Extensions:** `morph` (alias for `then`/`yield_self`)
|
260
|
+
|
191
261
|
## Full Documentation
|
192
262
|
|
193
263
|
For complete method listings, examples, and detailed usage, see the [API Documentation](https://itsthedevman.com/docs/everythingrb).
|
data/lib/everythingrb/all.rb
CHANGED
@@ -1,11 +1,19 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative "array"
|
4
|
+
require_relative "boolean"
|
4
5
|
require_relative "data"
|
6
|
+
require_relative "date"
|
5
7
|
require_relative "enumerable"
|
6
8
|
require_relative "hash"
|
9
|
+
require_relative "kernel"
|
7
10
|
require_relative "module"
|
11
|
+
require_relative "nil"
|
12
|
+
require_relative "numeric"
|
8
13
|
require_relative "ostruct"
|
14
|
+
require_relative "range"
|
15
|
+
require_relative "regexp"
|
9
16
|
require_relative "string"
|
10
17
|
require_relative "struct"
|
11
18
|
require_relative "symbol"
|
19
|
+
require_relative "time"
|
data/lib/everythingrb/array.rb
CHANGED
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Extensions to Ruby's core TrueClass
|
5
|
+
#
|
6
|
+
# Provides:
|
7
|
+
# - #in_quotes, #with_quotes: Wrap boolean values in quotes
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# require "everythingrb/boolean"
|
11
|
+
#
|
12
|
+
# true.in_quotes # => "\"true\""
|
13
|
+
# false.in_quotes # => "\"false\""
|
14
|
+
#
|
15
|
+
class TrueClass
|
16
|
+
include Everythingrb::InspectQuotable
|
17
|
+
end
|
18
|
+
|
19
|
+
#
|
20
|
+
# Extensions to Ruby's core FalseClass
|
21
|
+
#
|
22
|
+
# Provides:
|
23
|
+
# - #in_quotes, #with_quotes: Wrap boolean values in quotes
|
24
|
+
#
|
25
|
+
# @example
|
26
|
+
# require "everythingrb/boolean"
|
27
|
+
#
|
28
|
+
# true.in_quotes # => "\"true\""
|
29
|
+
# false.in_quotes # => "\"false\""
|
30
|
+
#
|
31
|
+
class FalseClass
|
32
|
+
include Everythingrb::InspectQuotable
|
33
|
+
end
|
data/lib/everythingrb/data.rb
CHANGED
@@ -5,8 +5,11 @@
|
|
5
5
|
#
|
6
6
|
# Provides:
|
7
7
|
# - #to_deep_h: Recursively convert to hash with all nested objects
|
8
|
+
# - #in_quotes, #with_quotes: Wrap object in quotes
|
8
9
|
#
|
9
10
|
class Data
|
11
|
+
include Everythingrb::InspectQuotable
|
12
|
+
|
10
13
|
#
|
11
14
|
# Recursively converts the Data object and all nested objects to hashes
|
12
15
|
#
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Extensions to Ruby's core Date class
|
5
|
+
#
|
6
|
+
# Provides:
|
7
|
+
# - #in_quotes, #with_quotes: Wrap date representations in quotes
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# require "everythingrb/date"
|
11
|
+
#
|
12
|
+
# Date.today.in_quotes
|
13
|
+
#
|
14
|
+
class Date
|
15
|
+
include Everythingrb::StringQuotable
|
16
|
+
end
|
17
|
+
|
18
|
+
#
|
19
|
+
# Extensions to Ruby's core DateTime class
|
20
|
+
#
|
21
|
+
# Provides:
|
22
|
+
# - #in_quotes, #with_quotes: Wrap time representations in quotes
|
23
|
+
#
|
24
|
+
# @example
|
25
|
+
# require "everythingrb/date"
|
26
|
+
#
|
27
|
+
# DateTime.now.in_quotes
|
28
|
+
#
|
29
|
+
class DateTime < Date
|
30
|
+
include Everythingrb::StringQuotable
|
31
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Extensions for making objects quotable with double quotes
|
5
|
+
#
|
6
|
+
# These modules add the ability to quote objects by wrapping
|
7
|
+
# their string representations in double quotes. This is useful for
|
8
|
+
# generating error messages, debug output, or formatted logs where
|
9
|
+
# you want to clearly distinguish between different value types.
|
10
|
+
#
|
11
|
+
# @example Basic usage with different types
|
12
|
+
# 1.in_quotes # => "\"1\""
|
13
|
+
# nil.in_quotes # => "\"nil\""
|
14
|
+
# "hello".in_quotes # => "\"\\\"hello\\\"\""
|
15
|
+
# [1, 2, 3].in_quotes # => "\"[1, 2, 3]\""
|
16
|
+
#
|
17
|
+
# @example Using with collections
|
18
|
+
# values = [1, nil, "hello"]
|
19
|
+
# values.map(&:in_quotes) # => ["\"1\"", "\"nil\"", "\"\\\"hello\\\"\""]
|
20
|
+
#
|
21
|
+
# @example Error messages
|
22
|
+
# expected = 42
|
23
|
+
# actual = nil
|
24
|
+
# raise "Expected #{expected.in_quotes} but got #{actual.in_quotes}"
|
25
|
+
# # => "Expected \"42\" but got \"nil\""
|
26
|
+
#
|
27
|
+
module Everythingrb
|
28
|
+
#
|
29
|
+
# Adds quotable functionality using inspect representation
|
30
|
+
#
|
31
|
+
# Provides methods for wrapping an object's inspection
|
32
|
+
# representation in double quotes for debugging and error messages.
|
33
|
+
#
|
34
|
+
# @example
|
35
|
+
# [1, 2, 3].in_quotes # => "\"[1, 2, 3]\""
|
36
|
+
#
|
37
|
+
module InspectQuotable
|
38
|
+
#
|
39
|
+
# Returns the object's inspection representation wrapped in double quotes
|
40
|
+
#
|
41
|
+
# @return [String] The object's inspect representation surrounded by double quotes
|
42
|
+
#
|
43
|
+
# @example
|
44
|
+
# [1, 2, 3].in_quotes # => "\"[1, 2, 3]\""
|
45
|
+
# {a: 1}.in_quotes # => "\"{:a=>1}\""
|
46
|
+
#
|
47
|
+
def in_quotes
|
48
|
+
%("#{inspect}")
|
49
|
+
end
|
50
|
+
|
51
|
+
alias_method :with_quotes, :in_quotes
|
52
|
+
end
|
53
|
+
|
54
|
+
#
|
55
|
+
# Adds quotable functionality using to_s representation
|
56
|
+
#
|
57
|
+
# Provides methods for wrapping an object's string
|
58
|
+
# representation in double quotes for cleaner output.
|
59
|
+
#
|
60
|
+
# @example
|
61
|
+
# Time.now.in_quotes # => "\"2025-05-03 12:34:56 -0400\""
|
62
|
+
#
|
63
|
+
module StringQuotable
|
64
|
+
#
|
65
|
+
# Returns the object's string representation wrapped in double quotes
|
66
|
+
#
|
67
|
+
# @return [String] The object's to_s representation surrounded by double quotes
|
68
|
+
#
|
69
|
+
# @example
|
70
|
+
# Date.today.in_quotes # => "\"2025-05-03\""
|
71
|
+
# Time.now.in_quotes # => "\"2025-05-03 12:34:56 -0400\""
|
72
|
+
#
|
73
|
+
def in_quotes
|
74
|
+
%("#{self}")
|
75
|
+
end
|
76
|
+
|
77
|
+
alias_method :with_quotes, :in_quotes
|
78
|
+
end
|
79
|
+
end
|
data/lib/everythingrb/hash.rb
CHANGED
@@ -19,6 +19,8 @@
|
|
19
19
|
# config.server.port # => 443
|
20
20
|
#
|
21
21
|
class Hash
|
22
|
+
include Everythingrb::InspectQuotable
|
23
|
+
|
22
24
|
#
|
23
25
|
# A minimal empty struct for Ruby 3.2+ compatibility
|
24
26
|
#
|
@@ -663,4 +665,100 @@ class Hash
|
|
663
665
|
self[new_key] = delete(old_key)
|
664
666
|
self
|
665
667
|
end
|
668
|
+
|
669
|
+
#
|
670
|
+
# Selects hash entries based only on their values
|
671
|
+
#
|
672
|
+
# @yield [value] Block that determines whether to include the entry
|
673
|
+
# @yieldparam value [Object] The current value
|
674
|
+
# @yieldreturn [Boolean] Whether to include this entry
|
675
|
+
#
|
676
|
+
# @return [Hash] A new hash including only entries where the block returned truthy
|
677
|
+
# @return [Enumerator] If no block is given
|
678
|
+
#
|
679
|
+
# @example Filter to include only present values (with ActiveSupport)
|
680
|
+
# {name: "Alice", bio: nil, role: ""}.select_values(&:present?)
|
681
|
+
# # => {name: "Alice"}
|
682
|
+
#
|
683
|
+
# @example Filter using more complex logic
|
684
|
+
# {id: 1, count: 0, items: [1, 2, 3]}.select_values { |v| v.is_a?(Array) || v > 0 }
|
685
|
+
# # => {id: 1, items: [1, 2, 3]}
|
686
|
+
#
|
687
|
+
def select_values(&block)
|
688
|
+
return to_enum(:select_values) if block.nil?
|
689
|
+
|
690
|
+
select { |_k, v| block.call(v) }
|
691
|
+
end
|
692
|
+
|
693
|
+
alias_method :filter_values, :select_values
|
694
|
+
|
695
|
+
#
|
696
|
+
# Selects hash entries based only on their values, modifying the hash in place
|
697
|
+
#
|
698
|
+
# @yield [value] Block that determines whether to keep the entry
|
699
|
+
# @yieldparam value [Object] The current value
|
700
|
+
# @yieldreturn [Boolean] Whether to keep this entry
|
701
|
+
#
|
702
|
+
# @return [self, nil] The modified hash, or nil if no changes were made
|
703
|
+
# @return [Enumerator] If no block is given
|
704
|
+
#
|
705
|
+
# @example Remove entries with empty values (with ActiveSupport)
|
706
|
+
# hash = {name: "Alice", bio: nil, role: ""}
|
707
|
+
# hash.select_values!(&:present?)
|
708
|
+
# # => {name: "Alice"}
|
709
|
+
# # hash is now {name: "Alice"}
|
710
|
+
#
|
711
|
+
def select_values!(&block)
|
712
|
+
return to_enum(:select_values!) if block.nil?
|
713
|
+
|
714
|
+
select! { |_k, v| block.call(v) }
|
715
|
+
end
|
716
|
+
|
717
|
+
alias_method :filter_values!, :select_values!
|
718
|
+
|
719
|
+
#
|
720
|
+
# Rejects hash entries based only on their values
|
721
|
+
#
|
722
|
+
# @yield [value] Block that determines whether to exclude the entry
|
723
|
+
# @yieldparam value [Object] The current value
|
724
|
+
# @yieldreturn [Boolean] Whether to exclude this entry
|
725
|
+
#
|
726
|
+
# @return [Hash] A new hash excluding entries where the block returned truthy
|
727
|
+
# @return [Enumerator] If no block is given
|
728
|
+
#
|
729
|
+
# @example Remove blank values (with ActiveSupport)
|
730
|
+
# {name: "Alice", bio: nil, role: ""}.reject_values(&:blank?)
|
731
|
+
# # => {name: "Alice"}
|
732
|
+
#
|
733
|
+
# @example Remove specific types of values
|
734
|
+
# {id: 1, count: 0, items: [1, 2, 3]}.reject_values { |v| v.is_a?(Integer) && v == 0 }
|
735
|
+
# # => {id: 1, items: [1, 2, 3]}
|
736
|
+
#
|
737
|
+
def reject_values(&block)
|
738
|
+
return to_enum(:reject_values) if block.nil?
|
739
|
+
|
740
|
+
reject { |_k, v| block.call(v) }
|
741
|
+
end
|
742
|
+
|
743
|
+
#
|
744
|
+
# Rejects hash entries based only on their values, modifying the hash in place
|
745
|
+
#
|
746
|
+
# @yield [value] Block that determines whether to remove the entry
|
747
|
+
# @yieldparam value [Object] The current value
|
748
|
+
# @yieldreturn [Boolean] Whether to remove this entry
|
749
|
+
#
|
750
|
+
# @return [self, nil] The modified hash, or nil if no changes were made
|
751
|
+
# @return [Enumerator] If no block is given
|
752
|
+
#
|
753
|
+
# @example Remove blank values in place (with ActiveSupport)
|
754
|
+
# hash = {name: "Alice", bio: nil, role: ""}
|
755
|
+
# hash.reject_values!(&:blank?)
|
756
|
+
# # => {name: "Alice"}
|
757
|
+
# # hash is now {name: "Alice"}
|
758
|
+
#
|
759
|
+
def reject_values!(&block)
|
760
|
+
return to_enum(:reject_values!) if block.nil?
|
761
|
+
|
762
|
+
reject! { |_k, v| block.call(v) }
|
763
|
+
end
|
666
764
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Extensions to Ruby's core Kernel module
|
5
|
+
#
|
6
|
+
# Provides:
|
7
|
+
# - #morph: A more intuitive alias for Kernel#then (yield_self)
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# require "everythingrb/kernel"
|
11
|
+
#
|
12
|
+
# version.get_info.morph { |v| "#{v.major}.#{v.minor}" }
|
13
|
+
#
|
14
|
+
module Kernel
|
15
|
+
#
|
16
|
+
# Transforms the receiver by passing it to the given block and returning the block's result
|
17
|
+
#
|
18
|
+
# This method is an alias for `then` (and `yield_self`) that more clearly communicates
|
19
|
+
# the transformation intent.
|
20
|
+
#
|
21
|
+
# @yield [receiver] Block that transforms the receiver
|
22
|
+
# @yieldparam receiver [Object] The receiver object
|
23
|
+
# @yieldreturn [Object] The transformed result
|
24
|
+
#
|
25
|
+
# @return [Object] The result of the block
|
26
|
+
#
|
27
|
+
# @example Convert a semantic version to a string
|
28
|
+
# version.to_sem_version.morph { |v| "#{v.major}.#{v.minor}" }
|
29
|
+
#
|
30
|
+
alias_method :morph, :then
|
31
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Extensions to Ruby's NilClass
|
5
|
+
#
|
6
|
+
# Provides:
|
7
|
+
# - #in_quotes, #with_quotes: Wrap nil's string representation in quotes
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# require "everythingrb/extensions/nil_class"
|
11
|
+
# nil.in_quotes # => "\"nil\""
|
12
|
+
#
|
13
|
+
class NilClass
|
14
|
+
include Everythingrb::InspectQuotable
|
15
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Extensions to Ruby's core Numeric class and subclasses
|
5
|
+
#
|
6
|
+
# Provides:
|
7
|
+
# - #in_quotes, #with_quotes: Wrap numeric values in quotes
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# require "everythingrb/numeric"
|
11
|
+
#
|
12
|
+
# 42.in_quotes # => "\"42\""
|
13
|
+
# 3.14.in_quotes # => "\"3.14\""
|
14
|
+
# (1+2i).in_quotes # => "\"1+2i\""
|
15
|
+
#
|
16
|
+
class Numeric
|
17
|
+
include Everythingrb::InspectQuotable
|
18
|
+
end
|
data/lib/everythingrb/ostruct.rb
CHANGED
@@ -8,6 +8,7 @@
|
|
8
8
|
# - #join_map: Combine filter_map and join operations
|
9
9
|
# - #blank?, #present?: ActiveSupport integrations when available
|
10
10
|
# - #to_deep_h: Recursively convert to hash with all nested objects
|
11
|
+
# - #in_quotes, #with_quotes: Wrap struct in quotes
|
11
12
|
#
|
12
13
|
# @example
|
13
14
|
# require "everythingrb/ostruct"
|
@@ -16,6 +17,8 @@
|
|
16
17
|
# person.map { |k, v| "#{k}: #{v}" } # => ["name: Alice", "age: 30"]
|
17
18
|
#
|
18
19
|
class OpenStruct
|
20
|
+
include Everythingrb::InspectQuotable
|
21
|
+
|
19
22
|
# ActiveSupport integrations
|
20
23
|
if defined?(ActiveSupport)
|
21
24
|
#
|
data/lib/everythingrb/prelude.rb
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Extensions to Ruby's core Range class
|
5
|
+
#
|
6
|
+
# Provides:
|
7
|
+
# - #in_quotes, #with_quotes: Wrap range representations in quotes
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# require "everythingrb/range"
|
11
|
+
#
|
12
|
+
# (1..5).in_quotes # => "\"1..5\""
|
13
|
+
# ('a'..'z').in_quotes # => "\"a..z\""
|
14
|
+
#
|
15
|
+
class Range
|
16
|
+
include Everythingrb::InspectQuotable
|
17
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Extensions to Ruby's core Regexp class
|
5
|
+
#
|
6
|
+
# Provides:
|
7
|
+
# - #in_quotes, #with_quotes: Wrap regular expression representations in quotes
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# require "everythingrb/regexp"
|
11
|
+
#
|
12
|
+
# /\d+/.in_quotes # => "\"/\\d+/\""
|
13
|
+
#
|
14
|
+
class Regexp
|
15
|
+
include Everythingrb::InspectQuotable
|
16
|
+
end
|
data/lib/everythingrb/string.rb
CHANGED
@@ -16,6 +16,8 @@
|
|
16
16
|
# "Hello".with_quotes # => "\"Hello\""
|
17
17
|
#
|
18
18
|
class String
|
19
|
+
include Everythingrb::StringQuotable
|
20
|
+
|
19
21
|
#
|
20
22
|
# Converts JSON string to Hash, returning nil if it failed
|
21
23
|
#
|
@@ -111,19 +113,4 @@ class String
|
|
111
113
|
def to_struct
|
112
114
|
to_h&.to_struct
|
113
115
|
end
|
114
|
-
|
115
|
-
#
|
116
|
-
# Returns self wrapped in double quotes
|
117
|
-
#
|
118
|
-
# @return [String] The string with surrounding double quotes
|
119
|
-
#
|
120
|
-
# @example
|
121
|
-
# "Hello World".with_quotes # => "\"Hello World\""
|
122
|
-
# "Quote \"me\"".with_quotes # => "\"Quote \\\"me\\\"\""
|
123
|
-
#
|
124
|
-
def with_quotes
|
125
|
-
%("#{self}")
|
126
|
-
end
|
127
|
-
|
128
|
-
alias_method :in_quotes, :with_quotes
|
129
116
|
end
|
data/lib/everythingrb/struct.rb
CHANGED
@@ -5,6 +5,7 @@
|
|
5
5
|
#
|
6
6
|
# Provides:
|
7
7
|
# - #to_deep_h: Recursively convert to hash with all nested objects
|
8
|
+
# - #in_quotes, #with_quotes: Wrap struct in quotes
|
8
9
|
#
|
9
10
|
# @example
|
10
11
|
# require "everythingrb/struct"
|
@@ -14,6 +15,8 @@
|
|
14
15
|
# person.to_deep_h # => {name: "Alice", profile: {roles: ["admin"]}}
|
15
16
|
#
|
16
17
|
class Struct
|
18
|
+
include Everythingrb::InspectQuotable
|
19
|
+
|
17
20
|
#
|
18
21
|
# Recursively converts the Struct and all nested objects to hashes
|
19
22
|
#
|
data/lib/everythingrb/symbol.rb
CHANGED
@@ -18,12 +18,12 @@ class Symbol
|
|
18
18
|
# @return [Symbol] The symbol with surrounding double quotes
|
19
19
|
#
|
20
20
|
# @example
|
21
|
-
# :hello_world.
|
21
|
+
# :hello_world.in_quotes # => :"\"hello_world\""
|
22
22
|
# :hello_world.in_quotes # => :"\"hello_world\""
|
23
23
|
#
|
24
|
-
def
|
24
|
+
def in_quotes
|
25
25
|
:"\"#{self}\""
|
26
26
|
end
|
27
27
|
|
28
|
-
alias_method :
|
28
|
+
alias_method :with_quotes, :in_quotes
|
29
29
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Extensions to Ruby's core Time class
|
5
|
+
#
|
6
|
+
# Provides:
|
7
|
+
# - #in_quotes, #with_quotes: Wrap time representations in quotes
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# require "everythingrb/time"
|
11
|
+
#
|
12
|
+
# Time.new(2025, 5, 3).in_quotes
|
13
|
+
#
|
14
|
+
class Time
|
15
|
+
include Everythingrb::StringQuotable
|
16
|
+
end
|
data/lib/everythingrb/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: everythingrb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bryan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-
|
11
|
+
date: 2025-05-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ostruct
|
@@ -59,15 +59,25 @@ files:
|
|
59
59
|
- lib/everythingrb.rb
|
60
60
|
- lib/everythingrb/all.rb
|
61
61
|
- lib/everythingrb/array.rb
|
62
|
+
- lib/everythingrb/boolean.rb
|
62
63
|
- lib/everythingrb/data.rb
|
64
|
+
- lib/everythingrb/date.rb
|
63
65
|
- lib/everythingrb/enumerable.rb
|
66
|
+
- lib/everythingrb/extensions.rb
|
67
|
+
- lib/everythingrb/extensions/quotable.rb
|
64
68
|
- lib/everythingrb/hash.rb
|
69
|
+
- lib/everythingrb/kernel.rb
|
65
70
|
- lib/everythingrb/module.rb
|
71
|
+
- lib/everythingrb/nil.rb
|
72
|
+
- lib/everythingrb/numeric.rb
|
66
73
|
- lib/everythingrb/ostruct.rb
|
67
74
|
- lib/everythingrb/prelude.rb
|
75
|
+
- lib/everythingrb/range.rb
|
76
|
+
- lib/everythingrb/regexp.rb
|
68
77
|
- lib/everythingrb/string.rb
|
69
78
|
- lib/everythingrb/struct.rb
|
70
79
|
- lib/everythingrb/symbol.rb
|
80
|
+
- lib/everythingrb/time.rb
|
71
81
|
- lib/everythingrb/version.rb
|
72
82
|
homepage: https://github.com/itsthedevman/everythingrb
|
73
83
|
licenses:
|