@@ -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
195242end
196243
0 commit comments