Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

You don't want to define MyCollection as a class. Define it as a parameterised role instead. IIRC, there is some MooseX::Declare syntactic sugar for parameterised roles, but I can't remember exactly how it goes, and it's undocumented.

package MyCollection; use MooseX::Role::Parameterized; parameter type => ( isa => 'ClassName', required => 1, ); parameter attr => ( isa => 'Str', required => 1, ); role { my $p = shift; my ($type, $attr) = ($p->type, $p->attr); has $attr => ( is => 'rw', isa => "HashRef[$type]", traits => ['Hash'], defaults => sub { {} } ); method "summon_$attr" => sub { my $self = shift; foreach my $item (values %{ $self->$attr }) { $item->summon if $item->does('Summonable'); } }; }; # and we'll use MooseX::Declare syntax for the rest... role Summonable { requires 'name'; method summon { say "Come here, ", $self->name; } } class Child with Summonable { has name => ( is => 'ro', isa => 'Str' ); } class Parent { with MyCollection => { type => 'Child', attr => 'children' }; } my $me = Parent->new; $me->children->{isabel} = Child->new(name => 'Isabel'); $me->children->{elliott} = Child->new(name => 'Elliott'); $me->summon_children;

The above is untested, and probably has some syntax errors lurking somewhere, but it should give you an idea of how it can be done.


In reply to Re: Moose(X) create my own Collection by tobyink
in thread Moose(X) create my own Collection by ragnaev

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (12)
    As of 2015-07-31 10:14 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (276 votes), past polls