hash_of 1.0.0 → 1.0.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 +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +21 -2
- data/lib/hash_of/version.rb +1 -1
- data/lib/hash_of.rb +2 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: abd0405b2537b883cf128551077589f57e7222a0c768fc831c0a34c87d1f4c59
|
4
|
+
data.tar.gz: c7c16a9e52d80c23e5e139a415732ca11f21cbe1ec82f3566126fd7e4ae269e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 02daed3abcb45213c7f7750ea9117b7962f628f7bcd367591b2485735c5e069da49a9a56301957d8972587a683d4d841dd3b6dae0681ef116611bf5755cee3a4
|
7
|
+
data.tar.gz: 540208b6ee76af2253f98adb7248bd1dadf797a30085b896ca9e3c73e79264073c743d0a8de7b34ee3359acff35afe9a5d8fa91853d2d00c2f7e8e0cb3bb286a
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,25 @@
|
|
1
|
-
#
|
1
|
+
# Hash.of
|
2
2
|
|
3
|
-
Mostly syntactic sugar to quickly and tersely create
|
3
|
+
Mostly syntactic sugar to quickly and tersely create a hash of arrays or hashes, and optionally, recursive hashes.
|
4
|
+
|
5
|
+
If you find yourself doing `Hash.new { |hash, key] hash[key] = {} }` a lot you can make this more concise with `Hash.of(:hash)`.
|
6
|
+
|
7
|
+
It helps readability when iterating with `#reduce` or `#each_with_object` to create efficient lookup tables. E.g.:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
# This is a contrived example for illustrative purposes only as Rails' `Enumerable#index_by` is a better tool for the specific case being depicted.
|
11
|
+
a_buncha_records.each_with_object(Hash.new { |hash, key| hash[key] = {} }) do |record, cache|
|
12
|
+
cache[record.id] = record
|
13
|
+
end
|
14
|
+
|
15
|
+
# vs.
|
16
|
+
a_buncha_records.each_with_object(Hash.of(:hash)) do |record, cache|
|
17
|
+
cache[record.id] = record
|
18
|
+
end
|
19
|
+
|
20
|
+
# or go nuts and inline it
|
21
|
+
a_buncha_records.each_with_object(Hash.of(:hash)) { |record, cache| cache[record.id] = record }
|
22
|
+
```
|
4
23
|
|
5
24
|
## Installation
|
6
25
|
|
data/lib/hash_of/version.rb
CHANGED
data/lib/hash_of.rb
CHANGED
@@ -6,18 +6,14 @@ require_relative "hash_of/version"
|
|
6
6
|
module HashOf
|
7
7
|
OF_ARRAY_LAMBDA = ->(hash, key) { hash[key] = [] }
|
8
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
|
9
|
+
OF_HASH_RECURSIVE_LAMBDA = ->(hash, key) { hash[key] = Hash.new(&hash.default_proc) }
|
14
10
|
|
15
11
|
def of(type, recursive: false)
|
16
12
|
case type
|
17
13
|
when :array
|
18
14
|
Hash.new(&OF_ARRAY_LAMBDA)
|
19
15
|
when :hash
|
20
|
-
recursive ?
|
16
|
+
recursive ? Hash.new(&OF_HASH_RECURSIVE_LAMBDA) : Hash.new(&OF_HASH_LAMBDA)
|
21
17
|
end
|
22
18
|
end
|
23
19
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_of
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Rosenberg
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-02-
|
11
|
+
date: 2024-02-12 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|