On Monday, August 8, 2016 at 11:23:27 AM UTC-7, William Dias wrote:
>
> I have a abstract BaseModel class which includes some common code shared 
> among all models. Every model I have inherits it.
>
> BaseModel = Class.new(Sequel::Model) do
>  ... code ..
> end
>
> One of my models needs to set its backing table dynamically upon each 
> request on my Sinatra App. My first idea was to create a class method to 
> configure the dataset before actually using it, but I realised this 
> approach is not thread safe.
>
> class Foo < BaseModel
>   def self.config(table_name)
>     set_dataset(table_name)
>   end
>  ... code ...
> end
>
> The I thought about creating the model dynamically on each call and use 
> Foo.klass to access it.
>
> class Foo
>   def initialize(table_name)
>     super
>     @klass = Class.new(BaseModel) do
>       set_dataset table_name
>        ... code ...
>     end
>   end
> end
>
> Which is the recommended way to accomplish what I intend to do?
>

With the current version of Sequel, if you want to dynamically create the 
classes, I'd use:

@klass = BaseModel::Model(table_name)

You may want to turn off caching of such classes if you do that:

BaseModel.cache_anonymous_models = false

Creating classes per request is usually considered a code smell, though it 
is better than modifying classes per request.  You should consider whether 
there is a possible approach that will do what you need without 
creating/modifying classes per request.

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