On Thursday, September 13, 2018 at 10:17:22 AM UTC-7, Alexander Popov wrote:
>
> Hello!
>
> After your changes (optimizations) in 
> https://github.com/jeremyevans/sequel/commit/8d390da53d8c42be59366a1fd24c49e8cfc84712
>  
> and 
> https://github.com/jeremyevans/sequel/commit/f1bcb9521adb5d9fbb4283e89c40049e4f51603b
>  
> there is no way to make some hook for instance (initializing) after 
> `Model.all` method.
>

"there is no way"?  There is always a way. :)
 

> For example, I have `class Locale < Sequel::Model`, and I want to have 
> `Locale#i18n`, which find object from (another) global storage and assign 
> it to model instance.
>
> So, good approach is:
>
> ```ruby
> attr_reader :i18n
>
> def code=(value)
>   super
>
>   @i18n = I18n.storage.find { |obj| obj.iso_code == value }
> end
> ```
>
> Or maybe:
>
> ```ruby
> def initialize(*)
>   super
>
>   @i18n = I18n.storage.find { |obj| obj.iso_code == code }
> end
> ```
>
> But it doesn't work for `Locale.all`.
>

I'm not sure exactly what you the problem is with the code= method you 
have, and how it related to Locale.all.

If you want something similar to the initialize method:

def Locale.call(h)
  locale = super
  locale.instance_variable_set(:@i18n, I18n.storage.find { |obj| 
obj.iso_code == locale.code })
end

or use the after_initialize plugin:

class Locale
  plugin :after_initialize
  def after_initialize
    super
    @i18n = I18n.storage.find { |obj| obj.iso_code == code }
  end
end

If that still doesn't get you what you want, I'm probably not understanding 
the problem correctly, so please put together a minimal self-contained 
reproducible example showing the problem in that case.

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 post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to