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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f6322a6bce710e4750c5b19e16dac50bc6825bb38d2b5bbebf16ca3b274589e
4
- data.tar.gz: 304958f8ad10f093939b6d1659b0412323675a1f841c964e61fa92ca86448e1c
3
+ metadata.gz: abd0405b2537b883cf128551077589f57e7222a0c768fc831c0a34c87d1f4c59
4
+ data.tar.gz: c7c16a9e52d80c23e5e139a415732ca11f21cbe1ec82f3566126fd7e4ae269e6
5
5
  SHA512:
6
- metadata.gz: 65c815ac49e03d228e59d917fbfe70f79c8a47c1d6c26f1f5f91045d848f8a0a8dcec8861aca077973b8fff467a6acac3f7c83ed7d84dde3203e59cee1992d1c
7
- data.tar.gz: 274bbdcfc14daa3f16a12faa0c64fd960080626219178783c00d2069f8f679f47aa0351911bb86bace66861f290d8d23bf109525c614466326fa1eafc5be95a9
6
+ metadata.gz: 02daed3abcb45213c7f7750ea9117b7962f628f7bcd367591b2485735c5e069da49a9a56301957d8972587a683d4d841dd3b6dae0681ef116611bf5755cee3a4
7
+ data.tar.gz: 540208b6ee76af2253f98adb7248bd1dadf797a30085b896ca9e3c73e79264073c743d0a8de7b34ee3359acff35afe9a5d8fa91853d2d00c2f7e8e0cb3bb286a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 1.0.1 (February 12, 2024)
2
+
3
+ - Simpler internal approach for creating the recursive hash that avoids a new class
4
+
1
5
  # 1.0.0 (February 4, 2024)
2
6
 
3
7
  ## Features:
data/README.md CHANGED
@@ -1,6 +1,25 @@
1
- # HashOf
1
+ # Hash.of
2
2
 
3
- Mostly syntactic sugar to quickly and tersely create hashes of arrays or hashes, and optionally, recursive hashes.
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
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HashOf
4
- VERSION = "1.0.0"
4
+ VERSION = "1.0.1"
5
5
  end
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 ? OfHash.new : Hash.new(&OF_HASH_LAMBDA)
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.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-04 00:00:00.000000000 Z
11
+ date: 2024-02-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: