everythingrb 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 43920acf56e9d77e9897c581c1e4ad347e4f349eb1222d32a88a2205a6c3168e
4
+ data.tar.gz: 870ff0e384224fd9f266da3ca2000dd273a850978a2dbde56a305cc98349cb0d
5
+ SHA512:
6
+ metadata.gz: 46cee04bf8d80f1bb4afe92c80aa56f4538c2100066ea6fda229e91244ba53f1cce67e98e59b8e50767ad9a5a3c21cef3c5da530d9fb10a557c70b5f27238429
7
+ data.tar.gz: 5ba5dd8f119ee5bdee72291d8df5c7d8f33ef6b50320fe7e639745933f3125b6eca0fa15f19b54eb66d443a38642121bb649ce2815fe0d6477ee3dbd2ec0ce3e
data/.envrc ADDED
@@ -0,0 +1 @@
1
+ use flake
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 3.3
data/CHANGELOG.md ADDED
@@ -0,0 +1,40 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ### Added
11
+
12
+ ### Changed
13
+
14
+ ### Removed
15
+
16
+ ## [0.1.0] - 12025-01-17
17
+
18
+ ### Added
19
+
20
+ - `Array#join_map` method that combines `filter_map` and `join` operations
21
+ - `Hash#join_map` method for consistent interface with Array implementation
22
+ - `Hash#to_istruct` for converting hashes to immutable Data structures (Ruby 3.2+)
23
+ - `Hash#to_struct` for recursive hash to Struct conversion
24
+ - `Hash#to_ostruct` for recursive hash to OpenStruct conversion
25
+ - `Module#attr_predicate` for generating boolean accessor methods
26
+ - Extended `OpenStruct` with:
27
+ - `blank?` and `present?` methods when ActiveSupport is available
28
+ - `map` and `filter_map` methods
29
+ - `join_map` method consistent with Array/Hash implementations
30
+ - Enhanced `String` class with:
31
+ - `to_h` and `to_a` methods for JSON parsing with `nil` fallback on error
32
+ - `to_deep_h` for recursive JSON string parsing
33
+ - `to_istruct`, `to_ostruct`, and `to_struct` conversion methods
34
+
35
+ ### Changed
36
+
37
+ - Added alias `each` to `each_pair` in OpenStruct for better enumerable compatibility
38
+
39
+ [unreleased]: https://github.com/itsthedevman/everythingrb/compare/v0.1.0...HEAD
40
+ [0.1.0]: https://github.com/itsthedevman/everythingrb/compare/5870052e137cb430d084eab1ec3934f3c50b4501...v0.1.0
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Bryan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,213 @@
1
+ # EverythingRB
2
+
3
+ Useful extensions to Ruby core classes that you never knew you needed until now.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "everythingrb"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ $ bundle install
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```bash
22
+ $ gem install everythingrb
23
+ ```
24
+
25
+ ## Core Extensions
26
+
27
+ ### Array
28
+
29
+ #### `join_map`
30
+ Combines `filter_map` and `join` operations in one convenient method.
31
+
32
+ ```ruby
33
+ [1, 2, nil, 3].join_map(" ") { |n| n&.to_s if n&.odd? }
34
+ # => "1 3"
35
+ ```
36
+
37
+ ### Hash
38
+
39
+ #### `to_struct`
40
+ Recursively converts a hash into a Struct, including nested hashes and arrays.
41
+
42
+ ```ruby
43
+ hash = { user: { name: "Alice", roles: ["admin", "user"] } }
44
+ struct = hash.to_struct
45
+ struct.class # => Struct
46
+ struct.user.name # => "Alice"
47
+ struct.user.roles # => ["admin", "user"]
48
+ ```
49
+
50
+ #### `to_ostruct`
51
+ Recursively converts a hash into an OpenStruct, including nested hashes and arrays.
52
+
53
+ ```ruby
54
+ hash = { config: { api_key: "secret", endpoints: ["v1", "v2"] } }
55
+ config = hash.to_ostruct
56
+ config.class # => OpenStruct
57
+ config.config.api_key # => "secret"
58
+ ```
59
+
60
+ #### `to_istruct`
61
+ Recursively converts a hash into an immutable Data structure (requires Ruby 3.2+).
62
+
63
+ ```ruby
64
+ hash = { person: { name: "Bob", age: 30 } }
65
+ data = hash.to_istruct
66
+ data.class # => Data
67
+ data.person.name # => "Bob"
68
+ ```
69
+
70
+ #### `join_map`
71
+ Similar to Array#join_map but operates on hash values.
72
+
73
+ ```ruby
74
+ { a: 1, b: 2, c: nil, d: 3 }.join_map(" ") { |k, v| [k, v] if v }
75
+ # => "a 1 b 2 d 3"
76
+ ```
77
+
78
+ ### Module
79
+
80
+ #### `attr_predicate`
81
+ Creates predicate (boolean) methods for instance variables.
82
+
83
+ ```ruby
84
+ class User
85
+ attr_writer :admin
86
+ attr_predicate :admin, :active
87
+ end
88
+
89
+ user = User.new
90
+ user.active? # => false
91
+ user.admin = true
92
+ user.admin? # => true
93
+ ```
94
+
95
+ ### OpenStruct
96
+
97
+ #### `each`
98
+ Alias for `each_pair`.
99
+
100
+ ```ruby
101
+ struct = OpenStruct.new(a: 1, b: 2)
102
+ struct.each { |key, value| puts "#{key}: #{value}" }
103
+ ```
104
+
105
+ #### `map`
106
+ Maps over OpenStruct entries.
107
+
108
+ ```ruby
109
+ struct = OpenStruct.new(a: 1, b: 2)
110
+ struct.map { |key, value| [key, value * 2] }
111
+ # => [[:a, 2], [:b, 4]]
112
+ ```
113
+
114
+ #### `filter_map`
115
+ Combines `map` and `compact` operations in one convenient method.
116
+
117
+ ```ruby
118
+ struct = OpenStruct.new(a: 1, b: nil, c: 2)
119
+ struct.filter_map { |key, value| value * 2 if value }
120
+ # => [2, 4]
121
+ ```
122
+
123
+ #### `join_map`
124
+ Combines `filter_map` and `join` operations in one convenient method.
125
+
126
+ ```ruby
127
+ config = OpenStruct.new(
128
+ alice: {roles: ["admin"]},
129
+ bob: {roles: ["user"]},
130
+ carol: {roles: ["admin"]}
131
+ )
132
+
133
+ config.join_map(", ") { |key, value| key if value[:roles].include?("admin") }
134
+ # => "alice, carol"
135
+ ```
136
+
137
+ ### String
138
+
139
+ #### `to_h` / `to_a`
140
+ Parses JSON string into a Ruby Hash or Array.
141
+
142
+ ```ruby
143
+ '{"name": "Alice"}'.to_h
144
+ # => {name: "Alice"}
145
+
146
+ '["Alice"]'.to_h # Or you can use #to_a for different readability
147
+ # => ["Alice"]
148
+ ```
149
+
150
+ #### `to_istruct`
151
+ Parses JSON string into an immutable Data structure (requires Ruby 3.2+).
152
+
153
+ ```ruby
154
+ '{"user": {"name": "Alice"}}'.to_istruct
155
+ # => #<data user=#<data name="Alice">>
156
+ ```
157
+
158
+ #### `to_ostruct`
159
+ Parses JSON string into an OpenStruct.
160
+
161
+ ```ruby
162
+ '{"user": {"name": "Alice"}}'.to_ostruct
163
+ # => #<OpenStruct user=#<OpenStruct name="Alice">>
164
+ ```
165
+
166
+ #### `to_struct`
167
+ Parses JSON string into a Struct.
168
+
169
+ ```ruby
170
+ '{"user": {"name": "Alice"}}'.to_struct
171
+ # => #<struct user=#<struct name="Alice">>
172
+ ```
173
+
174
+ #### `to_deep_h`
175
+ Recursively parses nested JSON strings within a structure.
176
+
177
+ ```ruby
178
+ nested_json = {
179
+ users: [
180
+ {name: "Alice", roles: ["admin", "user"]}.to_json,
181
+ ]
182
+ }.to_json
183
+
184
+ nested_json.to_deep_h
185
+ # => {users: [{name: "Alice", roles: ["admin", "user"]}]}
186
+ ```
187
+
188
+ ## Requirements
189
+
190
+ - Ruby 3.0+
191
+ - Ruby 3.2+ for `to_istruct` functionality
192
+
193
+ ## Contributing
194
+
195
+ 1. Fork it
196
+ 2. Create your feature branch (`git checkout -b feature/my-new-feature`)
197
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
198
+ 4. Push to the branch (`git push origin feature/my-new-feature`)
199
+ 5. Create new Pull Request
200
+
201
+ Please note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms.
202
+
203
+ ## License
204
+
205
+ The gem is available as open source under the terms of the [MIT License](LICENSE.txt).
206
+
207
+ ## Changelog
208
+
209
+ See [CHANGELOG.md](CHANGELOG.md) for a list of changes.
210
+
211
+ ## Credits
212
+
213
+ - Author: Bryan "itsthedevman"
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[test standard]
data/flake.lock ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "nodes": {
3
+ "flake-utils": {
4
+ "inputs": {
5
+ "systems": "systems"
6
+ },
7
+ "locked": {
8
+ "lastModified": 1731533236,
9
+ "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
10
+ "owner": "numtide",
11
+ "repo": "flake-utils",
12
+ "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
13
+ "type": "github"
14
+ },
15
+ "original": {
16
+ "owner": "numtide",
17
+ "repo": "flake-utils",
18
+ "type": "github"
19
+ }
20
+ },
21
+ "nixpkgs": {
22
+ "locked": {
23
+ "lastModified": 1736883708,
24
+ "narHash": "sha256-uQ+NQ0/xYU0N1CnXsa2zghgNaOPxWpMJXSUJJ9W7140=",
25
+ "owner": "NixOS",
26
+ "repo": "nixpkgs",
27
+ "rev": "eb62e6aa39ea67e0b8018ba8ea077efe65807dc8",
28
+ "type": "github"
29
+ },
30
+ "original": {
31
+ "owner": "NixOS",
32
+ "ref": "nixos-unstable",
33
+ "repo": "nixpkgs",
34
+ "type": "github"
35
+ }
36
+ },
37
+ "root": {
38
+ "inputs": {
39
+ "flake-utils": "flake-utils",
40
+ "nixpkgs": "nixpkgs"
41
+ }
42
+ },
43
+ "systems": {
44
+ "locked": {
45
+ "lastModified": 1681028828,
46
+ "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
47
+ "owner": "nix-systems",
48
+ "repo": "default",
49
+ "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
50
+ "type": "github"
51
+ },
52
+ "original": {
53
+ "owner": "nix-systems",
54
+ "repo": "default",
55
+ "type": "github"
56
+ }
57
+ }
58
+ },
59
+ "root": "root",
60
+ "version": 7
61
+ }
data/flake.nix ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ description = "Ruby 3.3.6 development environment";
3
+
4
+ inputs = {
5
+ nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
6
+ flake-utils.url = "github:numtide/flake-utils";
7
+ };
8
+
9
+ outputs = { self, nixpkgs, flake-utils }:
10
+ flake-utils.lib.eachDefaultSystem (system:
11
+ let
12
+ pkgs = nixpkgs.legacyPackages.${system};
13
+ in
14
+ {
15
+ devShells.default = pkgs.mkShell {
16
+ buildInputs = with pkgs; [
17
+ (ruby_3_3.override {
18
+ jemallocSupport = true;
19
+ docSupport = false;
20
+ })
21
+
22
+ # Dependencies for native gems
23
+ pkg-config
24
+ openssl
25
+ readline
26
+ zstd
27
+ libyaml
28
+ ];
29
+
30
+ shellHook = ''
31
+ export GEM_HOME="$PWD/vendor/bundle"
32
+ export GEM_PATH="$GEM_HOME"
33
+ export PATH="$GEM_HOME/bin:$PATH"
34
+
35
+ echo "checking gems"
36
+ bundle check || bundle install
37
+ '';
38
+ };
39
+ }
40
+ );
41
+ }
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Array
4
+ #
5
+ # Combines filter_map and join operations
6
+ #
7
+ # @param join_with [String] The delimiter to join elements with (defaults to empty string)
8
+ #
9
+ # @yield [Object] Block that filters and transforms array elements
10
+ #
11
+ # @return [String] Joined string of filtered and transformed elements
12
+ #
13
+ # @example
14
+ # [1, 2, nil, 3].join_map(" ") { |n| n&.to_s if n&.odd? }
15
+ # # => "1 3"
16
+ #
17
+ # @example
18
+ # [1, 2, nil, 3].join_map(", ")
19
+ # # => "1, 2, 3"
20
+ #
21
+ def join_map(join_with = "", &block)
22
+ block = ->(i) { i } if block.nil?
23
+
24
+ filter_map(&block).join(join_with)
25
+ end
26
+ end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Hash
4
+ #
5
+ # Combines filter_map and join operations
6
+ #
7
+ # @see Array#join_map
8
+ #
9
+ # @param join_with [String] The delimiter to join elements with (defaults to empty string)
10
+ #
11
+ # @yield [Object] Block that filters and transforms hash values
12
+ #
13
+ # @return [String] Joined string of filtered and transformed values
14
+ #
15
+ # @example
16
+ # { a: 1, b: 2, c: nil, d: 3 }.join_map(" ") { |v| v&.to_s if v&.odd? }
17
+ # # => "1 3"
18
+ #
19
+ # @example
20
+ # { a: 1, b: 2, c: nil, d: 3 }.join_map(", ")
21
+ # # => "a, 2, b, 2, c, d, 3"
22
+ #
23
+ def join_map(join_with = "", &block)
24
+ block = ->(kv_pair) { kv_pair.compact } if block.nil?
25
+
26
+ filter_map(&block).join(join_with)
27
+ end
28
+
29
+ # to_istruct relies on Data class
30
+ if defined?(Data)
31
+ #
32
+ # Converts hash to an immutable Data structure
33
+ #
34
+ # @return [Data]
35
+ #
36
+ def to_istruct
37
+ recurse = lambda do |input|
38
+ case input
39
+ when Hash
40
+ input.to_istruct
41
+ when Array
42
+ input.map(&recurse)
43
+ else
44
+ input
45
+ end
46
+ end
47
+
48
+ Data.define(*keys.map(&:to_sym)).new(*values.map { |value| recurse.call(value) })
49
+ end
50
+ end
51
+
52
+ #
53
+ # Converts hash to a Struct recursively
54
+ #
55
+ # @return [Struct]
56
+ #
57
+ def to_struct
58
+ recurse = lambda do |value|
59
+ case value
60
+ when Hash
61
+ value.to_struct
62
+ when Array
63
+ value.map(&recurse)
64
+ else
65
+ value
66
+ end
67
+ end
68
+
69
+ Struct.new(*keys.map(&:to_sym)).new(*values.map { |value| recurse.call(value) })
70
+ end
71
+
72
+ #
73
+ # Converts hash to an OpenStruct recursively
74
+ #
75
+ # @return [OpenStruct]
76
+ #
77
+ def to_ostruct
78
+ recurse = lambda do |value|
79
+ case value
80
+ when Hash
81
+ value.to_ostruct
82
+ when Array
83
+ value.map(&recurse)
84
+ else
85
+ value
86
+ end
87
+ end
88
+
89
+ OpenStruct.new(**transform_values { |value| recurse.call(value) })
90
+ end
91
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Module
4
+ #
5
+ # Creates predicate (boolean) methods for instance variables
6
+ # Similar to attr_reader, attr_writer, etc.
7
+ #
8
+ # @param *attributes [Array<Symbol, String>] Instance variable names
9
+ # @return [nil]
10
+ #
11
+ # @example
12
+ # class User
13
+ # attr_predicate :admin, :active
14
+ # end
15
+ #
16
+ # user.admin? # => true/false based on @admin
17
+ #
18
+ def attr_predicate(*attributes)
19
+ attributes.each do |attribute|
20
+ module_eval <<-STR, __FILE__, __LINE__ + 1
21
+ def #{attribute}?
22
+ !!instance_variable_get("@#{attribute}")
23
+ end
24
+ STR
25
+ end
26
+
27
+ nil
28
+ end
29
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OpenStruct
4
+ if defined?(ActiveSupport)
5
+ #
6
+ # Checks if the OpenStruct has no attributes
7
+ #
8
+ # @return [Boolean]
9
+ #
10
+ def blank?
11
+ @table.blank?
12
+ end
13
+
14
+ #
15
+ # Checks if the OpenStruct has any attributes
16
+ #
17
+ # @return [Boolean]
18
+ #
19
+ def present?
20
+ @table.present?
21
+ end
22
+ end
23
+
24
+ alias_method :each, :each_pair
25
+
26
+ #
27
+ # Maps over OpenStruct entries and returns an array
28
+ #
29
+ # @return [Enumerator, Array] Returns a new array, or enumerator if block is nil
30
+ #
31
+ def map(&)
32
+ @table.map(&)
33
+ end
34
+
35
+ #
36
+ # Maps over OpenStruct entries and returns an array without nil values
37
+ #
38
+ # @return [Enumerator, Array] Returns a new array, or enumerator if block is nil
39
+ #
40
+ def filter_map(&block)
41
+ return enum_for(:filter_map) unless block
42
+
43
+ map(&block).compact
44
+ end
45
+
46
+ #
47
+ # Combines filter_map and join operations
48
+ #
49
+ # @param join_with [String] The delimiter to join elements with (defaults to empty string)
50
+ #
51
+ # @yield [Object] Block that filters and transforms hash elements
52
+ #
53
+ # @return [String] Joined string of filtered and transformed elements
54
+ #
55
+ # @example
56
+ # object = OpenStruct.new(a: 1, b: nil, c: 3)
57
+ # object.join_map(" ") { |k, v| "#{k}-#{v}" if v }
58
+ # # => "a-1 c-3"
59
+ #
60
+ # @example
61
+ # object = OpenStruct.new(a: 1, b: nil, c: 3)
62
+ # object.join_map(", ")
63
+ # # => "a, 1, b, c, 3"
64
+ #
65
+ def join_map(join_with = "", &block)
66
+ block = ->(kv_pair) { kv_pair.compact } if block.nil?
67
+
68
+ filter_map(&block).join(join_with)
69
+ end
70
+
71
+ #
72
+ # @return [self]
73
+ #
74
+ def to_ostruct
75
+ self
76
+ end
77
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ class String
4
+ #
5
+ # Converts JSON string to Hash, returning nil if it failed
6
+ #
7
+ # @return [Hash, nil] Parsed JSON as hash
8
+ #
9
+ def to_h
10
+ JSON.parse(self, symbolize_names: true)
11
+ rescue JSON::ParserError
12
+ nil
13
+ end
14
+
15
+ alias_method :to_a, :to_h
16
+
17
+ #
18
+ # Deep parsing of nested JSON strings
19
+ # Recursively attempts to parse string values as JSON
20
+ #
21
+ # @return [Hash] Deeply parsed hash
22
+ #
23
+ def to_deep_h
24
+ recursive_convert = lambda do |object|
25
+ case object
26
+ when Array
27
+ object.map { |v| recursive_convert.call(v) }
28
+ when String
29
+ result = object.to_deep_h
30
+
31
+ # Nested JSON
32
+ if result.is_a?(Array) || result.is_a?(Hash)
33
+ recursive_convert.call(result)
34
+ else
35
+ object
36
+ end
37
+ when Hash
38
+ object.transform_values { |v| recursive_convert.call(v) }
39
+ else
40
+ object
41
+ end
42
+ end
43
+
44
+ recursive_convert.call(to_h)
45
+ end
46
+
47
+ #
48
+ # Attempts to parse JSON and convert to Data struct.
49
+ # Returns nil if string does not contain valid JSON
50
+ #
51
+ # @return [nil, Data]
52
+ #
53
+ def to_istruct
54
+ to_h&.to_istruct
55
+ end
56
+
57
+ #
58
+ # Attempts to parse JSON and convert to OpenStruct.
59
+ # Returns nil if string does not contain valid JSON
60
+ #
61
+ # @return [nil, OpenStruct]
62
+ def to_ostruct
63
+ to_h&.to_ostruct
64
+ end
65
+
66
+ #
67
+ # Attempts to parse JSON and convert to Struct.
68
+ # Returns nil if string does not contain valid JSON
69
+ #
70
+ # @return [nil, Struct]
71
+ #
72
+ def to_struct
73
+ to_h&.to_struct
74
+ end
75
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Everythingrb
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ostruct"
4
+ require "json"
5
+
6
+ require_relative "everythingrb/version"
7
+ require_relative "everythingrb/core/array"
8
+ require_relative "everythingrb/core/hash"
9
+ require_relative "everythingrb/core/module"
10
+ require_relative "everythingrb/core/ostruct"
11
+ require_relative "everythingrb/core/string"
12
+
13
+ module Everythingrb
14
+ end
@@ -0,0 +1,4 @@
1
+ module Everythingrb
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: everythingrb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bryan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 1980-01-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ostruct
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.6.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.9'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.9'
41
+ description: EverythingRB extends Ruby core classes with useful methods for combining
42
+ operations (join_map), converting data structures (to_struct, to_ostruct, to_istruct),
43
+ and handling JSON with nested parsing support.
44
+ email:
45
+ - bryan@itsthedevman.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".envrc"
51
+ - ".standard.yml"
52
+ - CHANGELOG.md
53
+ - CODE_OF_CONDUCT.md
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - flake.lock
58
+ - flake.nix
59
+ - lib/everythingrb.rb
60
+ - lib/everythingrb/core/array.rb
61
+ - lib/everythingrb/core/hash.rb
62
+ - lib/everythingrb/core/module.rb
63
+ - lib/everythingrb/core/ostruct.rb
64
+ - lib/everythingrb/core/string.rb
65
+ - lib/everythingrb/version.rb
66
+ - sig/everythingrb.rbs
67
+ homepage: https://github.com/itsthedevman/everythingrb
68
+ licenses:
69
+ - MIT
70
+ metadata:
71
+ source_code_uri: https://github.com/itsthedevman/everythingrb
72
+ changelog_uri: https://github.com/itsthedevman/everythingrb/blob/main/CHANGELOG.md
73
+ bug_tracker_uri: https://github.com/itsthedevman/everythingrb/issues
74
+ documentation_uri: https://github.com/itsthedevman/everythingrb#readme
75
+ rubygems_mfa_required: 'true'
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 3.0.0
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubygems_version: 3.5.22
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Practical extensions to Ruby core classes that combine common operations,
95
+ add convenient data structure conversions, and enhance JSON handling.
96
+ test_files: []