Forum: Nitro Og and the 'Composite' pattern.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
62711fa2787e85b5f0c88e245ef69f54?d=identicon&s=25 Alexey Petrushin (axyd80)
on 2008-06-05 21:55
Hello, is there a way to implement the 'Composite' pattern in Og?

Probably it should looks like this:

    class Item
      property :name, String
      belongs_to :parent, Item
    end

    class Container < Item
      has_many :items, Item
    end

    class Image < Item; end
    class Post < Item; end
    class Folder < Container; end

But i failed to achieve this with Og :(

P.S.
Does ActiveRecords or DataMapper support this feature?

Thanks!
62711fa2787e85b5f0c88e245ef69f54?d=identicon&s=25 Alexey Petrushin (axyd80)
on 2008-06-05 22:03
Partially solution. It's possible to implement subject this way:

    class Item
      property :name, String
      belongs_to :parent, Object
    end

    class Container < Item
      has_many :items, Item
    end

    class Folder < Item
      has_many :items, Item
    end

But, there are major disadvantages:
- Code duplication, each 'Container' should explicitly include 'has_many
...'.
- Instead of 'Item' you forced to use 'Container::Item' and
'Folder::Item' and remember about it.
- Instead of one 'Item' table you got two distinct 'container_item' and
'folder_item' tables.

So, this is not the best solution ... :(
D3c9a4e663c77df1fe4888da3ba85bcc?d=identicon&s=25 * William (Guest)
on 2008-06-07 20:51
(Received via mailing list)
Thanks there Alexey

I love this post!  I have a design need for exactly thing like (both)
'Item'
and 'Container'.

As well, I'd like to make a "generic" 'Container' that cna accept
'Items"
sub-clases too.

I'd like though to do ...

            *class *Data_Table_stuff
                *property*     subclass-of-Item
                *property*     subclass-of-Container

            *end *# Data_Table_stuff

I'll admit that I'm sitting back on this.  My issuer -- Still some solid
Og
base is a precursor.

:-)


2008/6/6 Alexey Petrushin <lists@ruby-forum.com>:
This topic is locked and can not be replied to.