Thanks Nick,

It's actually the delegation of the helpers for CodeRef i'm unsure how to use 
since they are wrapped in the Hash, I would imagine I would have to use some 
API call similar to what 'handles' does to pull the execute() and 
execute_method() method modifiers in 
Moose::Meta::Attribute::Native::Trait::Code into my class each time a new item 
is added to the Hash.

sub my_set_actions {
        my ($self)=...@_;

        # allocate new CodeRef

        # add it to the Hash

        # do some api call to load the method modifiers in 
Moose::Meta::Attribute::Native::Trait::Code
        #  into $self (with unique method names of course)
                
        $self->set_actions('Baz', $coderef )
}



-----Original Message-----
From: Nick Perez [mailto:[email protected]] 
Sent: Montag, 2. August 2010 20:00
To: Brian ROONEY
Cc: [email protected]
Subject: Re: parameterized Helper traits

On Mon, 2 Aug 2010 19:50:12 +0200
Brian ROONEY <[email protected]> wrote:

> Hi,
> 
> How does one get the helper traits for a parameterized type?  Here I
> am using the helper: Moose::Meta::Attribute::Native::Trait::Hash
> For HashRef
> 
> The hash actually contains parameterized CodeRefs so I would like to
> use: Moose::Meta::Attribute::Native::Trait::Code
> For CodeRef
> 
> Is this possible?
> Thanks
> 
> 
> has 'actions' => (
>       traits                  => ['Hash'],
>       is                      => 'rw', 
>       isa                     => 'HashRef[CodeRef]',  
>       required                => 0,
>       handles   => {
>           set_actions         => 'set',
>           get_actions         => 'get',
>           delete_action       => 'delete',
>           keys_actions        => 'keys',
>           exists_action       => 'exists',
>           defined_action      => 'defined',
>           values_actions      => 'values',
>           pairs_actions       => 'kv',          
>           elements_actions    => 'elements',
>           clear_actions       => 'clear',
>           num_actions         => 'count',          
>           has_no_actions      => 'is_empty',
>           accessor_actions    => 'accessor',        
>       }, 
> );
> 

Did you try it and see?



package Foo;
use Moose;

has 'bar' => (
    traits      => ['Hash'],
    is          => 'ro', 
    isa         => 'HashRef[CodeRef]',
    default     => sub { +{} },
    handles     =>
    {
        set_actions => 'set',
        get_actions => 'get',
    }
);

no Moose;

package main;

my $foo = Foo->new();
$foo->set_actions('Baz', sub { print "Yahoo!\n"; });
$foo->set_actions('Flarg', sub { print "Zoom!\n"; });
$foo->get_actions('Baz')->();
$foo->get_actions('Flarg')->();
$foo->set_actions('Yarg', 'Some random string'); # errors here;

__END__
Value Some random string did not pass container type constraint
'CodeRef'
at 
/home/nicholas/installed/perl5/perls/default/lib/site_perl/5.10.1/x86_64-linux/Moose/Meta/Attribute/Native/MethodProvider/Hash.pm
line 84
Moose::Meta::Attribute::Native::MethodProvider::Hash::__ANON__('Foo=HASH(0x1ee5400)',
'Yarg', 'Some random string') called
at 
/home/nicholas/installed/perl5/perls/default/lib/site_perl/5.10.1/x86_64-linux/Moose/Meta/Attribute/Native/Trait.pm
line 124 Foo::set_actions('Foo=HASH(0x1ee5400)', 'Yarg', 'Some random
string') called at /tmp/example.pl line 25 

Yahoo! 
Zoom!



-- 

Nicholas Perez
XMPP/Email: [email protected]
http://search.cpan.org/~nperez/
http://github.com/nperez

Reply via email to