hash_of 1.0.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 +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +19 -0
- data/CHANGELOG.md +9 -0
- data/LICENSE.txt +21 -0
- data/README.md +82 -0
- data/Rakefile +12 -0
- data/lib/hash_of/version.rb +5 -0
- data/lib/hash_of.rb +25 -0
- data/sig/hash_of.rbs +13 -0
- metadata +57 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3f6322a6bce710e4750c5b19e16dac50bc6825bb38d2b5bbebf16ca3b274589e
|
4
|
+
data.tar.gz: 304958f8ad10f093939b6d1659b0412323675a1f841c964e61fa92ca86448e1c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 65c815ac49e03d228e59d917fbfe70f79c8a47c1d6c26f1f5f91045d848f8a0a8dcec8861aca077973b8fff467a6acac3f7c83ed7d84dde3203e59cee1992d1c
|
7
|
+
data.tar.gz: 274bbdcfc14daa3f16a12faa0c64fd960080626219178783c00d2069f8f679f47aa0351911bb86bace66861f290d8d23bf109525c614466326fa1eafc5be95a9
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-rake
|
3
|
+
- rubocop-rspec
|
4
|
+
- rubocop-performance
|
5
|
+
|
6
|
+
AllCops:
|
7
|
+
NewCops: enable
|
8
|
+
TargetRubyVersion: 3.0
|
9
|
+
|
10
|
+
Style/StringLiterals:
|
11
|
+
Enabled: true
|
12
|
+
EnforcedStyle: double_quotes
|
13
|
+
|
14
|
+
Style/StringLiteralsInInterpolation:
|
15
|
+
Enabled: true
|
16
|
+
EnforcedStyle: double_quotes
|
17
|
+
|
18
|
+
Layout/LineLength:
|
19
|
+
Max: 120
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# 1.0.0 (February 4, 2024)
|
2
|
+
|
3
|
+
## Features:
|
4
|
+
|
5
|
+
- Initial release
|
6
|
+
- Introduces `Hash.of` to create a new hash where unrecognized keys are new objects as specified
|
7
|
+
- `Hash.of(:array)[:any_key] #=> []`
|
8
|
+
- `Hash.of(:hash)[:any_key] #=> {}`
|
9
|
+
- `Hash.of(:hash, recursive: true)[:any_key][:and_another][:or_more] #=> {}`
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Aaron Rosenberg
|
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,82 @@
|
|
1
|
+
# HashOf
|
2
|
+
|
3
|
+
Mostly syntactic sugar to quickly and tersely create hashes of arrays or hashes, and optionally, recursive hashes.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
$ bundle add hash_of
|
10
|
+
|
11
|
+
Alternatively add to your Gemfile via
|
12
|
+
|
13
|
+
gem "hash_of", "~> 1.0"
|
14
|
+
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
|
+
|
17
|
+
$ gem install hash_of
|
18
|
+
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Adds the `Hash.of` method providing the following uses
|
23
|
+
|
24
|
+
### 1. `Hash.of(:array)`
|
25
|
+
|
26
|
+
Returns a new hash whose unrecognized keys will be a new array.
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
hash_of_arrays = Hash.of(:array)
|
30
|
+
hash_of_arrays[:any_key]
|
31
|
+
# => []
|
32
|
+
hash_of_arrays[:numbers_with_one] << 1
|
33
|
+
# => [1]
|
34
|
+
hash_of_arrays
|
35
|
+
# => {:any_key=>[], :numbers_with_one=>[1]}
|
36
|
+
```
|
37
|
+
|
38
|
+
### 2. `Hash.of(:hash)`
|
39
|
+
|
40
|
+
Returns a new hash whose unrecognized keys will be a new hash.
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
hash_of_hashes = Hash.of(:hash)
|
44
|
+
hash_of_hashes[:any_key]
|
45
|
+
# => {}
|
46
|
+
hash_of_hashes[:names_and_numbers][:one]
|
47
|
+
# => nil
|
48
|
+
hash_of_hashes[:names_and_numbers][:one] = 1
|
49
|
+
# => 1
|
50
|
+
hash_of_hashes
|
51
|
+
# => {:any_key=>{}, :names_and_numbers=>{:one=>1}}
|
52
|
+
```
|
53
|
+
|
54
|
+
### 3. `Hash.of(:hash, recursive: true)`
|
55
|
+
|
56
|
+
Returns a hash where each unrecognized key is a hash where each unrecognized key is a hash … and so on.
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
recursive_hash_of_hashes = Hash.of(:hash, recursive: true)
|
60
|
+
recursive_hash_of_hashes[:any_key]
|
61
|
+
# => {}
|
62
|
+
recursive_hash_of_hashes[:any_key][:and_another]
|
63
|
+
# => {}
|
64
|
+
recursive_hash_of_hashes[:any_key][:and_another][:one_more]
|
65
|
+
# => {}
|
66
|
+
recursive_hash_of_hashes[:any_key][:and_another][:one_more] = "abc"
|
67
|
+
# => "abc"
|
68
|
+
recursive_hash_of_hashes
|
69
|
+
# => {:any_key=>{:and_another=>{:one_more=>"abc"}}}
|
70
|
+
```
|
71
|
+
|
72
|
+
## Development
|
73
|
+
|
74
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake` to run the tests and Rubocop. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
75
|
+
|
76
|
+
## Contributing
|
77
|
+
|
78
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hash_of.
|
79
|
+
|
80
|
+
## License
|
81
|
+
|
82
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/lib/hash_of.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "hash_of/version"
|
4
|
+
|
5
|
+
# Module to contain the constants, subclass, and class method Hash will extend
|
6
|
+
module HashOf
|
7
|
+
OF_ARRAY_LAMBDA = ->(hash, key) { hash[key] = [] }
|
8
|
+
OF_HASH_LAMBDA = ->(hash, key) { hash[key] = {} }
|
9
|
+
|
10
|
+
# Produce new Hashes with their default proc set to create hashes recursively
|
11
|
+
class OfHash < Hash
|
12
|
+
def initialize = super { |hash, key| hash[key] = OfHash.new }
|
13
|
+
end
|
14
|
+
|
15
|
+
def of(type, recursive: false)
|
16
|
+
case type
|
17
|
+
when :array
|
18
|
+
Hash.new(&OF_ARRAY_LAMBDA)
|
19
|
+
when :hash
|
20
|
+
recursive ? OfHash.new : Hash.new(&OF_HASH_LAMBDA)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
Hash.extend(HashOf)
|
data/sig/hash_of.rbs
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Module to contain the constants, subclass, and class method Hash will extend
|
2
|
+
module HashOf
|
3
|
+
OF_ARRAY_LAMBDA: Lambda
|
4
|
+
OF_HASH_LAMBDA: Lambda
|
5
|
+
VERSION: String
|
6
|
+
|
7
|
+
# Produce new Hashes with their default proc set to create hashes recursively
|
8
|
+
class OfHash < Hash
|
9
|
+
def initialize: () -> void
|
10
|
+
end
|
11
|
+
|
12
|
+
def of: (Symbol type, ?recursive: bool) -> Hash
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_of
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aaron Rosenberg
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-02-04 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- aarongrosenberg@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".rspec"
|
21
|
+
- ".rubocop.yml"
|
22
|
+
- CHANGELOG.md
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/hash_of.rb
|
27
|
+
- lib/hash_of/version.rb
|
28
|
+
- sig/hash_of.rbs
|
29
|
+
homepage: https://github.com/agrberg/hash_of
|
30
|
+
licenses:
|
31
|
+
- MIT
|
32
|
+
metadata:
|
33
|
+
changelog_uri: https://github.com/agrberg/hash_of/blob/main/CHANGELOG.md
|
34
|
+
homepage_uri: https://github.com/agrberg/hash_of
|
35
|
+
rubygems_mfa_required: 'true'
|
36
|
+
source_code_uri: https://github.com/agrberg/hash_of
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 3.0.0
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubygems_version: 3.5.5
|
53
|
+
signing_key:
|
54
|
+
specification_version: 4
|
55
|
+
summary: Syntactic sugar to create hashes of hashes or arrays and ability to make
|
56
|
+
them recursive.
|
57
|
+
test_files: []
|