everythingrb 0.2.1 → 0.2.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 81c7beca1a15139ecb5a59977740b65244d05f16b157ca45fb0d2995c7503bd3
4
- data.tar.gz: d6b6fffa5d7be4890ac617b17c785e289e6e6f3385fa941a72f211f47e2a7c0c
3
+ metadata.gz: 8567a1a0f80fd1582946019cd0c378f916648f70d9a9087944f83f5ba29669be
4
+ data.tar.gz: 8f42ce110e7c57529f591615d7612d75556497fc3e80fe56f0ece352847df709
5
5
  SHA512:
6
- metadata.gz: b3089b90018085b856d7fd34a133ec9174753a371d7c6d56088c82fbbd5c46a1e5c4c3a2c0d9e967b1eaf28d84dd322b5b8a919a93bd5f71157e2638bdfaabfd
7
- data.tar.gz: 5f5e058681ad88168d0a37c7580fd29483f755a895d0fce088e8db9904136d7a118d6b7557375fc7675f8b157da16b61274eeaf0378a53e53863632723dcccc0
6
+ metadata.gz: 065ef8975736ca530d915f4d7780b1a35a6e85834f735aadbc244f5c05bd39845be26bd07102e52e649e9946769b255fb50d951ea4b335204d55c75eae213828
7
+ data.tar.gz: 6edcdc737cd2dda4395d5b685c463c3af0d2fedab7d1737193105a43ce4365e0b63bdbeb7bac336868b0dea1e9857d9e0d33b55ba0f693ab818f607611ca864d
data/CHANGELOG.md CHANGED
@@ -23,6 +23,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
23
23
 
24
24
  ### Removed
25
25
 
26
+ ## [0.2.3] - 12025-03-09
27
+
28
+ ### Added
29
+
30
+ - Added `Symbol#with_quotes` and `Symbol#in_quotes`
31
+
32
+
33
+ ## [0.2.2] - 12025-03-03
34
+
35
+ ### Added
36
+
37
+ - Added `Array#key_map` and `Array#dig_map` for mapping over `Hash`
38
+ - Added `with_index:` keyword argument to `Array#join_map`. Defaults to `false`
39
+ - Added `Enumerable#join_map`
40
+ - Added `Array#deep_freeze` and `Hash#deep_freeze` to recursively freeze the underlying values
41
+
26
42
  ## [0.2.1] - 12025-03-01
27
43
 
28
44
  ### Added
@@ -88,7 +104,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
104
 
89
105
  - Added alias `each` to `each_pair` in OpenStruct for better enumerable compatibility
90
106
 
91
- [unreleased]: https://github.com/itsthedevman/everythingrb/compare/v0.2.1...HEAD
107
+ [unreleased]: https://github.com/itsthedevman/everythingrb/compare/v0.2.3...HEAD
108
+ [0.2.3]: https://github.com/itsthedevman/everythingrb/compare/v0.2.2...v0.2.3
109
+ [0.2.2]: https://github.com/itsthedevman/everythingrb/compare/v0.2.1...v0.2.2
92
110
  [0.2.1]: https://github.com/itsthedevman/everythingrb/compare/v0.2.0...v0.2.1
93
111
  [0.2.0]: https://github.com/itsthedevman/everythingrb/compare/v0.1.2...v0.2.0
94
112
  [0.1.2]: https://github.com/itsthedevman/everythingrb/compare/v0.1.1...v0.1.2
data/README.md CHANGED
@@ -19,24 +19,31 @@ I'm currently looking for opportunities where I can tackle meaningful problems a
19
19
  - [Core Extensions](#core-extensions)
20
20
  - [Array](#array)
21
21
  - [join_map](#join_map)
22
+ - [key_map](#key_map)
23
+ - [dig_map](#dig_map)
24
+ - [Enumerable](#enumerable)
25
+ - [join_map](#join_map-1)
22
26
  - [Hash](#hash)
23
27
  - [to_struct](#to_struct)
24
28
  - [to_ostruct](#to_ostruct)
25
29
  - [to_istruct](#to_istruct)
26
- - [join_map](#join_map-1)
30
+ - [join_map](#join_map-2)
27
31
  - [Module](#module)
28
32
  - [attr_predicate](#attr_predicate)
29
33
  - [OpenStruct](#openstruct)
30
34
  - [each](#each)
31
35
  - [map](#map)
32
36
  - [filter_map](#filter_map)
33
- - [join_map](#join_map-2)
37
+ - [join_map](#join_map-3)
34
38
  - [String](#string)
35
39
  - [to_h / to_a](#to_h--to_a)
36
40
  - [to_istruct](#to_istruct-1)
37
41
  - [to_ostruct](#to_ostruct-1)
38
42
  - [to_struct](#to_struct-1)
39
43
  - [to_deep_h](#to_deep_h)
44
+ - [with_quotes / in_quotes](#with_quotes--in_quotes)
45
+ - [Symbol](#symbol)
46
+ - [with_quotes / in_quotes](#with_quotes--in_quotes-1)
40
47
  - [Requirements](#requirements)
41
48
  - [Contributing](#contributing)
42
49
  - [License](#license)
@@ -76,11 +83,74 @@ $ gem install everythingrb
76
83
  ### Array
77
84
 
78
85
  #### `join_map`
79
- Combines `filter_map` and `join` operations in one convenient method.
86
+ Combines `filter_map` and `join` operations in one convenient method. Optionally provides the index to the block.
80
87
 
81
88
  ```ruby
89
+ # Without index
82
90
  [1, 2, nil, 3].join_map(" ") { |n| n&.to_s if n&.odd? }
83
91
  # => "1 3"
92
+
93
+ # With index
94
+ ["a", "b", "c"].join_map(", ", with_index: true) { |char, i| "#{i}:#{char}" }
95
+ # => "0:a, 1:b, 2:c"
96
+
97
+ # Default behavior without block
98
+ [1, 2, nil, 3].join_map(", ")
99
+ # => "1, 2, 3"
100
+ ```
101
+
102
+ #### `key_map`
103
+ Extracts a specific key from each hash in an array.
104
+
105
+ ```ruby
106
+ users = [
107
+ { name: "Alice", age: 30 },
108
+ { name: "Bob", age: 25 }
109
+ ]
110
+
111
+ users.key_map(:name)
112
+ # => ["Alice", "Bob"]
113
+ ```
114
+
115
+ #### `dig_map`
116
+ Extracts nested values from each hash in an array using the `dig` method.
117
+
118
+ ```ruby
119
+ data = [
120
+ { user: { profile: { name: "Alice" } } },
121
+ { user: { profile: { name: "Bob" } } }
122
+ ]
123
+
124
+ data.dig_map(:user, :profile, :name)
125
+ # => ["Alice", "Bob"]
126
+ ```
127
+
128
+ #### `deep_freeze`
129
+ Recursively freezes an array and all of its nested elements.
130
+
131
+ ```ruby
132
+ array = ["hello", { name: "Alice" }, [1, 2, 3]]
133
+ array.deep_freeze
134
+ # => All elements and nested structures are now frozen
135
+ ```
136
+
137
+ ### Enumerable
138
+
139
+ #### `join_map`
140
+ Combines filtering, mapping and joining operations into one convenient method.
141
+
142
+ ```ruby
143
+ # Basic usage with arrays
144
+ [1, 2, nil, 3].join_map(", ") { |n| n&.to_s if n&.odd? }
145
+ # => "1, 3"
146
+
147
+ # Works with any Enumerable
148
+ (1..10).join_map(" | ") { |n| "num#{n}" if n.even? }
149
+ # => "num2 | num4 | num6 | num8 | num10"
150
+
151
+ # Supports with_index option
152
+ %w[a b c].join_map("-", with_index: true) { |char, i| "#{i}:#{char}" }
153
+ # => "0:a-1:b-2:c"
84
154
  ```
85
155
 
86
156
  ### Hash
@@ -124,6 +194,15 @@ Similar to Array#join_map but operates on hash values.
124
194
  # => "a 1 b 2 d 3"
125
195
  ```
126
196
 
197
+ #### `deep_freeze`
198
+ Recursively freezes a hash and all of its nested values.
199
+
200
+ ```ruby
201
+ hash = { user: { name: "Alice", roles: ["admin", "user"] } }
202
+ hash.deep_freeze
203
+ # => Hash and all nested structures are now frozen
204
+ ```
205
+
127
206
  ### Module
128
207
 
129
208
  #### `attr_predicate`
@@ -242,6 +321,16 @@ Wraps the string in double quotes
242
321
  # => "\"Hello World\""
243
322
  ```
244
323
 
324
+ ### Symbol
325
+
326
+ #### `with_quotes` / `in_quotes`
327
+ Wraps the symbol in double quotes
328
+
329
+ ```ruby
330
+ :hello_world.with_quotes
331
+ # => :"\"hello_world\""
332
+ ```
333
+
245
334
  ## Contributing
246
335
 
247
336
  1. Fork it
@@ -5,22 +5,80 @@ class Array
5
5
  # Combines filter_map and join operations
6
6
  #
7
7
  # @param join_with [String] The delimiter to join elements with (defaults to empty string)
8
+ # @param with_index [Boolean] Whether to include the index in the block (defaults to false)
8
9
  #
9
- # @yield [Object] Block that filters and transforms array elements
10
+ # @yield [element, index] Block that filters and transforms array elements
11
+ # @yieldparam element [Object] The current element
12
+ # @yieldparam index [Integer] The index of the current element (only if with_index: true)
10
13
  #
11
14
  # @return [String] Joined string of filtered and transformed elements
12
15
  #
13
- # @example
16
+ # @example Without index
14
17
  # [1, 2, nil, 3].join_map(" ") { |n| n&.to_s if n&.odd? }
15
18
  # # => "1 3"
16
19
  #
17
- # @example
20
+ # @example With index
21
+ # ["a", "b", "c"].join_map(", ", with_index: true) { |char, i| "#{i}:#{char}" }
22
+ # # => "0:a, 1:b, 2:c"
23
+ #
24
+ # @example Default behavior without block
18
25
  # [1, 2, nil, 3].join_map(", ")
19
26
  # # => "1, 2, 3"
20
27
  #
21
- def join_map(join_with = "", &block)
28
+ def join_map(join_with = "", with_index: false, &block)
22
29
  block = ->(i) { i } if block.nil?
23
30
 
24
- filter_map(&block).join(join_with)
31
+ if with_index
32
+ filter_map.with_index(&block).join(join_with)
33
+ else
34
+ filter_map(&block).join(join_with)
35
+ end
36
+ end
37
+
38
+ #
39
+ # Maps over hash keys to extract values for a specific key
40
+ #
41
+ # @param key [Symbol, String] The key to extract
42
+ #
43
+ # @return [Array] Array of values
44
+ #
45
+ # @example
46
+ # [{name: 'Alice', age: 30}, {name: 'Bob', age: 25}].key_map(:name)
47
+ # # => ['Alice', 'Bob']
48
+ #
49
+ def key_map(key)
50
+ map { |v| v[key] }
51
+ end
52
+
53
+ #
54
+ # Maps over hash keys to extract nested values using dig
55
+ #
56
+ # @param keys [Array<Symbol, String>] The keys to dig through
57
+ #
58
+ # @return [Array] Array of nested values
59
+ #
60
+ # @example
61
+ # [
62
+ # {user: {profile: {name: 'Alice'}}},
63
+ # {user: {profile: {name: 'Bob'}}}
64
+ # ].dig_map(:user, :profile, :name)
65
+ # # => ['Alice', 'Bob']
66
+ #
67
+ def dig_map(*keys)
68
+ map { |v| v.dig(*keys) }
69
+ end
70
+
71
+ #
72
+ # Recursively freezes self and all of its contents
73
+ #
74
+ # @return [self] Returns the frozen array
75
+ #
76
+ # @example Freeze an array with nested structures
77
+ # ["hello", { name: "Alice" }, [1, 2, 3]].deep_freeze
78
+ # # => All elements and nested structures are now frozen
79
+ #
80
+ def deep_freeze
81
+ each { |v| v.respond_to?(:deep_freeze) ? v.deep_freeze : v.freeze }
82
+ freeze
25
83
  end
26
84
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Enumerable
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
+ # @param with_index [Boolean] Whether to include the index in the block (defaults to false)
9
+ #
10
+ # @yield [element, index] Block that filters and transforms array elements
11
+ # @yieldparam element [Object] The current element
12
+ # @yieldparam index [Integer] The index of the current element (only if with_index: true)
13
+ #
14
+ # @return [String] Joined string of filtered and transformed elements
15
+ #
16
+ # @example Without index
17
+ # [1, 2, nil, 3].join_map(" ") { |n| n&.to_s if n&.odd? }
18
+ # # => "1 3"
19
+ #
20
+ # @example With index
21
+ # ["a", "b", "c"].join_map(", ", with_index: true) { |char, i| "#{i}:#{char}" }
22
+ # # => "0:a, 1:b, 2:c"
23
+ #
24
+ # @example Default behavior without block
25
+ # [1, 2, nil, 3].join_map(", ")
26
+ # # => "1, 2, 3"
27
+ #
28
+ def join_map(join_with = "", with_index: false, &block)
29
+ block = ->(i) { i } if block.nil?
30
+
31
+ if with_index
32
+ filter_map.with_index(&block).join(join_with)
33
+ else
34
+ filter_map(&block).join(join_with)
35
+ end
36
+ end
37
+ end
@@ -85,4 +85,18 @@ class Hash
85
85
 
86
86
  OpenStruct.new(**transform_values { |value| recurse.call(value) })
87
87
  end
88
+
89
+ #
90
+ # Recursively freezes self and all of its values
91
+ #
92
+ # @return [self] Returns the frozen hash
93
+ #
94
+ # @example Freeze a hash with nested structures
95
+ # { user: { name: "Alice", roles: ["admin"] } }.deep_freeze
96
+ # # => Hash and all nested structures are now frozen
97
+ #
98
+ def deep_freeze
99
+ each_value { |v| v.respond_to?(:deep_freeze) ? v.deep_freeze : v.freeze }
100
+ freeze
101
+ end
88
102
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Symbol
4
+ #
5
+ # Returns self wrapped in double quotes.
6
+ #
7
+ # @return [Symbol]
8
+ #
9
+ def with_quotes
10
+ :"\"#{self}\""
11
+ end
12
+
13
+ alias_method :in_quotes, :with_quotes
14
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Everythingrb
4
- VERSION = "0.2.1"
4
+ VERSION = "0.2.3"
5
5
  end
data/lib/everythingrb.rb CHANGED
@@ -5,10 +5,12 @@ require "json"
5
5
 
6
6
  require_relative "everythingrb/version"
7
7
  require_relative "everythingrb/core/array"
8
+ require_relative "everythingrb/core/enumerable"
8
9
  require_relative "everythingrb/core/hash"
9
10
  require_relative "everythingrb/core/module"
10
11
  require_relative "everythingrb/core/ostruct"
11
12
  require_relative "everythingrb/core/string"
13
+ require_relative "everythingrb/core/symbol"
12
14
 
13
15
  module Everythingrb
14
16
  end
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.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-03-01 00:00:00.000000000 Z
11
+ date: 2025-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ostruct
@@ -58,12 +58,13 @@ files:
58
58
  - flake.nix
59
59
  - lib/everythingrb.rb
60
60
  - lib/everythingrb/core/array.rb
61
+ - lib/everythingrb/core/enumerable.rb
61
62
  - lib/everythingrb/core/hash.rb
62
63
  - lib/everythingrb/core/module.rb
63
64
  - lib/everythingrb/core/ostruct.rb
64
65
  - lib/everythingrb/core/string.rb
66
+ - lib/everythingrb/core/symbol.rb
65
67
  - lib/everythingrb/version.rb
66
- - sig/everythingrb.rbs
67
68
  homepage: https://github.com/itsthedevman/everythingrb
68
69
  licenses:
69
70
  - MIT
data/sig/everythingrb.rbs DELETED
@@ -1,4 +0,0 @@
1
- module Everythingrb
2
- VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
- end