|
From: Chris W. <ch...@cw...> - 2003-12-20 16:46:52
|
On Dec 19, 2003, at 5:25 PM, Vsevolod (Simon) Ilyushchenko wrote:
> ...
> So far this worked out well until I started using 'links_to'. Assume
> that MyPublisher and MyBook are my classes, SPOPS_Publisher and
> SPOPS_Book are SPOPS classes. Instances of MyPublisher refer to
> instances of SPOPS_Publisher, instances of MyBooks refer to instances
> of SPOPS_Books.
Right -- I think it's easier (now) to make MyBooks a subclass of
SPOPS_Books (more below)
> The expected object behavior is this:
>
> $aMyPublisher->books produces an array of MyBooks.
>
> In actuality,
>
> $aMyPublisher->books produces an array of SPOPS_Books.
>
> Is it possible to generate the desired result without straining the
> framework too much? Looks like I can achieve this by mucking around in
> SPOPS::ClassFactory::DBI and providing some kind of callbacks within
> the get/add/remove functions, but I'd like to get an official word
> first
Coincidentally, I made a change in 0.80 that allows you to do:
SPOPS class: SPOPS_Books
Your class: MyBooks isa SPOPS_Books
SPOPS class: SPOPS_Publisher
Your class: MyPublisher isa SPOPS_Publisher
Then declare in your publisher configuration:
links_to => { MyBooks => 'link_table' }
So you can use your subclass ('MyBooks') rather than the
SPOPS-generated class as the class to link to. I know this goes against
the GoF declaration to 'Prefer composition over inheritance', but it is
a much cleaner solution that using the 'code_class' configuration item
(crazy idea that). I've modified all the code in OpenInteract2 to use
this pattern, naming the SPOPS-generated classes 'FooPersist' for
clarity.
The only caveat is that you cannot 'use base' to declare the parent
relationship; you need to use @ISA explicitly:
package MyBooks;
@MyBooks::ISA = qw( SPOPS_Books );
sub my_custom_sub {
...
}
The change in SPOPS 0.80 to enable this was just to do a 'require' on
the class specified in has_a and links_to before using it in the code
generation process.
Hope that makes sense.
Chris
--
Chris Winters
Creating enterprise-capable snack systems since 1988
|