Skip to content

Commit 0bbd93c

Browse files
committed
v0.6.0
1 parent 35f1128 commit 0bbd93c

6 files changed

Lines changed: 80 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.6.0] - 2025-10-16
9+
10+
[Diff](https://github.com/Verseth/ruby-shale-builder/compare/v0.5.1...v0.6.0)
11+
12+
### Changes
13+
- Add `Shale::Builder::mapper_attributes`, `Shale::Builder::mapper_attributes!`, `Shale::Builder::builder_attributes`, `Shale::Builder::builder_attributes!`
14+
- Add `Shale::Builder#inject_context`
15+
816
## [0.5.1] - 2025-10-15
917

1018
[Diff](https://github.com/Verseth/ruby-shale-builder/compare/v0.5.0...v0.5.1)

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
shale-builder (0.5.2)
4+
shale-builder (0.6.0)
55
booleans (>= 0.1)
66
shale (< 2.0)
77
sorbet-runtime (> 0.5)

lib/shale/attribute.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,12 @@ def all_names
3838
def mapper?
3939
type.is_a?(Class) && type < Shale::Mapper
4040
end
41+
42+
# Returns `true` if the attribute is handled by a shale builder.
43+
#
44+
#: -> bool
45+
def builder?
46+
type.is_a?(Class) && type < Shale::Builder
47+
end
4148
end
4249
end

lib/shale/builder.rb

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,44 @@ def #{new_name}=(val)
169169
RUBY
170170
end
171171

172-
end
173-
mixes_in_class_methods(ClassMethods)
172+
# Returns a hash of shale attributes that are handled by a shale mapper.
173+
# The result gets memoized.
174+
#
175+
#: -> Hash[Symbol, Shale::Attribute]
176+
def mapper_attributes
177+
@mapper_attributes ||= mapper_attributes!
178+
end
174179

175-
def initialize(*args, **kwargs, &block)
176-
super
177-
@__initialized = true
178-
end
180+
# Returns a hash of shale attributes that are handled by a shale mapper.
181+
# Always constructs a new hash.
182+
#
183+
#: -> Hash[Symbol, Shale::Attribute]
184+
def mapper_attributes!
185+
attributes.select do |_, attr|
186+
attr.mapper?
187+
end
188+
end
179189

180-
#: bool?
181-
attr_reader :__initialized
190+
# Returns a hash of shale attributes that are handled by a shale builder.
191+
# The result gets memoized.
192+
#
193+
#: -> Hash[Symbol, Shale::Attribute]
194+
def builder_attributes
195+
@builder_attributes ||= builder_attributes!
196+
end
197+
198+
# Returns a hash of shale attributes that are handled by a shale builder.
199+
# Always constructs a new hash.
200+
#
201+
#: -> Hash[Symbol, Shale::Attribute]
202+
def builder_attributes!
203+
attributes.select do |_, attr|
204+
attr.builder?
205+
end
206+
end
207+
208+
end
209+
mixes_in_class_methods(ClassMethods)
182210

183211
# Returns an array of shale values
184212
# that have been assigned.
@@ -191,6 +219,25 @@ def attribute_values
191219
end
192220
end
193221

222+
# Attempts to set the given attributes and values
223+
# within this shale builder object and all of its sub-builders.
224+
# Attributes that aren't defined are ignored.
225+
#
226+
#: (**untyped) -> void
227+
def inject_context(**context)
228+
context.each do |name, val|
229+
try(:"#{name}=", val)
230+
end
231+
232+
klass = self.class #: as untyped
233+
klass.builder_attributes.each_key do |name|
234+
val = public_send(name)
235+
next unless val
236+
237+
val.inject_context(context)
238+
end
239+
end
240+
194241
end
195242
end
196243

lib/shale/builder/assigned_attributes.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ def #{name}=(val)
5959
end
6060
mixes_in_class_methods ClassMethods
6161

62+
def initialize(*args, **kwargs, &block)
63+
super
64+
@__initialized = true
65+
end
66+
67+
#: bool?
68+
attr_reader :__initialized
69+
6270
# Returns a set of names of assigned shale attributes.
6371
#
6472
#: -> Set[Symbol]

lib/shale/builder/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module Shale
44
module Builder
5-
VERSION = '0.5.2'
5+
VERSION = '0.6.0'
66
end
77
end

0 commit comments

Comments
 (0)