On Friday, August 28, 2020 at 1:35:03 AM UTC-7, Michael Monerau wrote:
>
> Hi,
>
> I solved an issue I had but I'm not sure if it's the expected behaviour or
> not. Let me explain.
>
> I realized that model values in plugins are not inherited by default when
> subclassing a model.
> That is:
>
> class MyModel < Sequel::Model
>> plugin MyPlugin, foo: { a: 1 }
>> end
>
> class MyInherited < MyModel
>> end
>
>
> If MyPlugin stores the foo value in a @foo variable, it is nil in
> MyInherited.
>
> That's where the doc kicks in
> http://sequel.jeremyevans.net/rdoc/files/doc/model_plugins_rdoc.html#label-Handling+Subclasses,
>
> and adding
>
> Sequel::Plugins.inherited_instance_variables(self, :@foo => nil)
>
>
> in the plugin's ClassMethods fixes the issue.
>
> But I wanted to clarify if it's expected. The doc states "*instead of
> model subclasses asking their parent class for a value if they don't have
> it defined, the value is automatically copied from the parent class to the
> subclass when the subclass is created*".
>
> So I would expect that I don't have to call inherited_instance_variables
> myself: the default would be the `nil` option.
>
> I suspect it's expected behaviour though, and the doc could be updated to
> "automatically copied *if you specify an inheritance policy through
> inherited_instance_variables*".
>
> Let me know if my understanding is correct and I can submit this small doc
> PR.
>
If you read the documentation directly below the line you are mentioning,
it explains this:
While you can do this by overriding the inherited class method, there is a
available shortcut that handles most cases:
module Sequel::Plugins::Foo
module ClassMethods
Sequel::Plugins.inherited_instance_variables(self, :@foo_states => :dup)
endend
Inside the ClassMethods submodule, you call the
Sequel::Plugins.inherited_instance_variables
<http://sequel.jeremyevans.net/rdoc/classes/Sequel/Plugins.html#method-c-inherited_instance_variables>
method
with the first argument being self. The second argument should be a hash
describing how to copy the value from the parent class into the subclass.
The keys of this hash are instance variable names, including the @ symbol
(e.g. :@foo_state). The values of this hash describe how to copy it:
nil
Use the value directly.
:dup
Call dup on the value.
:hash_dup
Create a new hash with the same keys, but a dup of all the values.
Proc
An arbitrary proc that is called with the parent class value and should
return the value to set into the subclass.
So I don't think any changes are needed.
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sequel-talk/d9405bdb-f5be-4f7a-9920-5edc97e67564o%40googlegroups.com.