nested_hash_builder 0.1.2 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3ae91f6f35a7eb80ad36ce8aa4c828850dd664c4e974f8739efcbfdd431513ca
4
- data.tar.gz: 0ec36c55778d92787c705a98aebfd5ad6039937c7b3d80482819364a57a53a7a
3
+ metadata.gz: d844d52dedf8429bf59e34377cf96f315d60509200e4dec84693fe5cb0f4aee9
4
+ data.tar.gz: eabeb83b29827acafd98cb56fb763233752c3b783654b70a9ecb8ad60a997187
5
5
  SHA512:
6
- metadata.gz: 83535f7620d3828bcfd4920ba64e5eb6e3f1e357659efe2b434c34eedd7e21bc203096b91245792db905b5a87b02b68a75e463638c13df558dca7a18f7368e10
7
- data.tar.gz: aac7aa1b776fd43b616fd2810ac4121fa2a5960d0abd7f671324864b8696aea5dd0bce7c89f35a3df34710cc8812f0ee49ebb3c596d5bcd8f75220993e7797be
6
+ metadata.gz: e9e28cb47e066a86a63c8842e20797be1298371458b55c9787393880e9ed8ab892332e8ec7946ce582fabcb1d3b9a40da1dba845edc96b3e4824f0d5583c6f58
7
+ data.tar.gz: 0fe0f96c9642ec43b9707419332b06bd22a1bcbc5e3d5c5362f7d76eaae3876fdcc71ec0fe028c5d71b2375df23ee354774e43a9882df6f00a76b1e21ec5a170
data/CHANGELOG.md CHANGED
@@ -12,6 +12,14 @@ 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.0] - 2025-08-13
16
+
17
+ [Diff](https://github.com/Verseth/ruby-nested_hash_builder/compare/v0.1.0...v0.2.0)
18
+
19
+ ### Changes
20
+ - Added `NestedHashBuilder.build!` for strict typechecking
21
+ - Added `NestedHashBuilder::Proxy#[]=` and `NestedHashBuilder::Proxy#[]`
22
+
15
23
  ## [0.1.0] - 2025-08-13
16
24
 
17
25
  [Diff](https://github.com/Verseth/ruby-nested_hash_builder/compare/v0.0.0...v0.1.0)
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.0'
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.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mateusz Drewniak