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

Sorry, I mean there is now _elegant_ way, which I know. :) Ruby-way, 
Ruby-native way, something like that.

> def Locale.call(h)

I don't like `instance_variable_set` much, but yeah, it's an option. 

> or use the after_initialize plugin

OK, this looks better. But… doesn't work with `static_cache` plugin and 
`Model.first` method.

> 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.

I have (static cached) locales in DB and in R18n library (from YAML files). 
I want to bind them.

So… instead of something like:

```ruby
def r18n
  return @r18n if defined? @r18n
  @r18n = R18n.locales.find { |locale| locale.code == code }
end
```

I want to make this `find` in initialization (or in `code` setter), once, 
also because `Locale` has `static_cache` plugin.

On Thursday, September 13, 2018 at 8:40:56 PM UTC+3, Jeremy Evans wrote:
>
> 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