I have some troubles with this approach for multi-inherited abstract
classes:
AbstractModelClass = Class.new(Sequel::Model) do
def self.inherited(model)
super
begin
model.one_to_many :associated_model
rescue Sequel::Error => error # because there is no `dataset?` method,
and `dataset` getter raises error
raise error unless error.message.start_with?('No dataset associated')
end
end
end
AbstractModelClassWithAdditionalFeature = Class.new(AbstractModelClass) do
def self.inherited(model)
super
model.many_to_one :additional_association
end
end
class Child < AbstractModelClass
end
On Wednesday, May 30, 2012 at 9:49:08 PM UTC+3, Jeremy Evans wrote:
>
> On Wednesday, May 30, 2012 10:20:38 AM UTC-7, Michael Granger wrote:
>>
>> > On Saturday, May 26, 2012 7:01:48 PM UTC-7, Jeremy Evans wrote:
>> > > On Friday, May 25, 2012 1:28:43 PM UTC-7, Michael Granger wrote:
>> > > Even though it's an unsupported use, subclassing Sequel::Model works
>> quite well if you're careful. I do it in several projects in which I want
>> to use Sequel::Model, but I can't know where the database is until runtime
>> (e.g., connect info is in a config file, etc.).
>> >
>> > This is probably different from what the original poster is asking. In
>> your case, your model has a backing table, it just isn't known until
>> runtime. He appears to be requesting a way to use a model without a
>> backing table at all.
>>
>> Looking back at what I wrote, I didn't explain myself well, but my
>> case is the same as his *in addition* to runtime loading: both of the
>> above cases subclass Sequel::Model, but don't have a backing table,
>> and then all other model classes inherit from it instead of
>> Sequel::Model directly. Mongrel2::Config is the abstract class in the
>> first case, and WordNet::Model in the second. So instead of:
>>
>> ModelClass < (anonymous model class) < Sequel::Model
>>
>> the inheritance ends up being:
>>
>> ConcreteModelClass < (anonymous model class) < AbstractModelClass <
>> Sequel::Model
>>
>
> Ah. Apparently I misinterpreted the original post. Having an abstract
> model class without a backing table is completely supported, as long as you
> only instantiate subclasses that have a backing table. The recommended way
> to do this in Sequel is to use Class.new:
>
> AbstractModelClass = Class.new(Sequel::Model)
> class AbstractModelClass
> # ...
> end
>
> The problem with the regular form:
>
> class AbstractModelClass < Sequel::Model
> end
>
> Is that it is going to assume that AbstractModelClass has a dataset
> associated with it, when it should not.
>
> Sorry for the misunderstanding.
>
> 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.