nested_hash_builder 0.1.2 → 0.2.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: 3ae91f6f35a7eb80ad36ce8aa4c828850dd664c4e974f8739efcbfdd431513ca
4
- data.tar.gz: 0ec36c55778d92787c705a98aebfd5ad6039937c7b3d80482819364a57a53a7a
3
+ metadata.gz: 40c02e41c1890f478577f138a24ac874f3448b21f048dda0311abd540889cafb
4
+ data.tar.gz: 03a659d9d9aed896a477f9d6fe33168f432e9762af1566cf529468d37e83e500
5
5
  SHA512:
6
- metadata.gz: 83535f7620d3828bcfd4920ba64e5eb6e3f1e357659efe2b434c34eedd7e21bc203096b91245792db905b5a87b02b68a75e463638c13df558dca7a18f7368e10
7
- data.tar.gz: aac7aa1b776fd43b616fd2810ac4121fa2a5960d0abd7f671324864b8696aea5dd0bce7c89f35a3df34710cc8812f0ee49ebb3c596d5bcd8f75220993e7797be
6
+ metadata.gz: 1e17961e499cd4ffdc6cb8a24fe94ffa908e7923e07a0993319f4cbe4e21fba4217296972717469345864b8a9ab3568e75947f8aedad5708519bd5b5781fac53
7
+ data.tar.gz: d7a2190a7b22e1377b569ae7708e59255ad942e3960ac4afd0ac93f1029a2bb4bd7ba8c6f860e87540a6c839c5310a530c31d27c0930996e9f206b630b43e105
data/CHANGELOG.md CHANGED
@@ -12,6 +12,21 @@ Add changes in new features here. Do not change the gem's version in pull/merge
12
12
  ### Changes
13
13
  -
14
14
 
15
+ ## [0.2.1] - 2026-10-15
16
+
17
+ [Diff](https://github.com/Verseth/ruby-nested_hash_builder/compare/v0.2.0...v0.2.1)
18
+
19
+ ### Changes
20
+ - Add MIT license
21
+
22
+ ## [0.2.0] - 2025-08-13
23
+
24
+ [Diff](https://github.com/Verseth/ruby-nested_hash_builder/compare/v0.1.0...v0.2.0)
25
+
26
+ ### Changes
27
+ - Added `NestedHashBuilder.build!` for strict typechecking
28
+ - Added `NestedHashBuilder::Proxy#[]=` and `NestedHashBuilder::Proxy#[]`
29
+
15
30
  ## [0.1.0] - 2025-08-13
16
31
 
17
32
  [Diff](https://github.com/Verseth/ruby-nested_hash_builder/compare/v0.0.0...v0.1.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2026 Mateusz Drewniak
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -32,7 +32,10 @@ hash = NestedHashBuilder.build do |h|
32
32
  h.city "Anytown"
33
33
  h.zip "12345"
34
34
  end
35
- h.key!("SOME:STRANGE:KEY", 2)
35
+ h["SOME:STRANGE:KEY"] = 2
36
+ h["another-strange-key"] do
37
+ h.foo "bar"
38
+ end
36
39
  h.array!(:contacts) do |c|
37
40
  c << h.entry! do |e|
38
41
  e.email "john@example.com"
@@ -56,6 +59,9 @@ end
56
59
  # zip: "12345"
57
60
  # },
58
61
  # "SOME:STRANGE:KEY": 2,
62
+ # "another-strange-key": {
63
+ # foo: "bar",
64
+ # },
59
65
  # contacts: [
60
66
  # {
61
67
  # email: "john@example.com",
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module NestedHashBuilder
5
- VERSION = '0.1.2'
5
+ VERSION = '0.2.1'
6
6
  end
@@ -22,13 +22,19 @@ require_relative 'nested_hash_builder/version'
22
22
  #
23
23
  module NestedHashBuilder
24
24
  class << self
25
- #: (?base: Hash[untyped, untyped], ?symbolize: bool) { (Proxy) -> void } -> Hash[untyped, untyped]
25
+ #: (?base: Hash[untyped, untyped], ?symbolize: bool) { (untyped) -> void } -> Hash[untyped, untyped]
26
26
  def call(base: {}, symbolize: true, &block)
27
27
  block.call(proxy = Proxy.new(base: base, symbolize: symbolize))
28
28
  proxy.to_h
29
29
  end
30
30
 
31
31
  alias build call
32
+
33
+ #: (?base: Hash[untyped, untyped], ?symbolize: bool) { (Proxy) -> void } -> Hash[untyped, untyped]
34
+ def build!(base: {}, symbolize: true, &block)
35
+ block.call(proxy = Proxy.new(base: base, symbolize: symbolize))
36
+ proxy.to_h
37
+ end
32
38
  end
33
39
 
34
40
  # Object that is the receiver of all
@@ -65,6 +71,22 @@ module NestedHashBuilder
65
71
  @current_parent[name] = value
66
72
  @hash
67
73
  end
74
+ alias set! key!
75
+ alias []= key!
76
+
77
+ # Add a key with the given name to the hash and create a nested hash.
78
+ #
79
+ #: (String | Symbol) { -> void } -> void
80
+ def [](name, &block)
81
+ name =
82
+ if @symbolize
83
+ name.to_sym
84
+ else
85
+ name.to_s
86
+ end
87
+
88
+ hash!(name, &block)
89
+ end
68
90
 
69
91
  #: { (Proxy) -> void } -> Hash[untyped, untyped]
70
92
  def entry!(&block)
@@ -77,6 +99,13 @@ module NestedHashBuilder
77
99
  #
78
100
  #: (String | Symbol) -> void
79
101
  def hash!(name)
102
+ name =
103
+ if @symbolize
104
+ name.to_sym
105
+ else
106
+ name.to_s
107
+ end
108
+
80
109
  previous_parent = @current_parent
81
110
  @current_parent = (@current_parent[name] ||= {})
82
111
  yield
@@ -165,7 +194,7 @@ module NestedHashBuilder
165
194
  private
166
195
 
167
196
  def builder_method?(name)
168
- !name.end_with?('?') && !name.end_with?('!')
197
+ name.match(/^[a-zA-Z]/) && !name.end_with?('?') && !name.end_with?('!')
169
198
  end
170
199
 
171
200
  def method_missing(method_name, *args, &block)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nested_hash_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mateusz Drewniak
@@ -33,6 +33,7 @@ files:
33
33
  - ".github/workflows/ci.yml"
34
34
  - ".ruby-version"
35
35
  - CHANGELOG.md
36
+ - LICENSE.txt
36
37
  - README.md
37
38
  - Rakefile
38
39
  - lib/nested_hash_builder.rb
@@ -59,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
60
  - !ruby/object:Gem::Version
60
61
  version: '0'
61
62
  requirements: []
62
- rubygems_version: 3.7.1
63
+ rubygems_version: 3.6.9
63
64
  specification_version: 4
64
65
  summary: A Ruby library for building nested Hashes
65
66
  test_files: []