MooseX::DeepAccessors - Delegate methods to member objects, curried with more methods!


MooseX-DeepAccessors documentation Contained in the MooseX-DeepAccessors distribution.

Index


Code Index:

NAME

Top

MooseX::DeepAccessors - Delegate methods to member objects, curried with more methods!

VERSION

Top

Version 0.02

SYNOPSIS

Top

    package MyClass;

    use Moose;
    use MooseX::DeepAccessors;

    has foo => (
        isa => 'Str',
        is => 'ro',
        required => 0,
    );

    has delegate => (
        isa => 'Foo',
        metaclass => 'MooseX::DeepAccessors',
        is => 'ro',
        default => sub { Foo->new },
        required => 0,
        lazy => 1,
        deep_accessors => {
            'bar' => { 'blah' => [ sub { $_[0]->foo }, ], },
        },
    );

INTERFACE

Top

The deep_accessors attribute takes parameters in the form:

    deep_accessors => { 
        'LOCALMETHOD' => { 'DELEGATEMETHOD' => [ sub { $_[0]->OTHERLOCALMETHOD } ] }
    }

Where LOCALMETHOD is the method on this class to create, DELEGATEMETHOD is the method on the object whose accessor is being described, and OTHERLOCALMETHOD is a method on this class, which will be called with the object passed to LOCALMETHOD and whose return value will be passed to DELEGATEMETHOD.

To put it another way, it allows you to write: $object->LOCALMETHOD;

Rather than: $object->DELEGATE->DELEGATEMETHOD( $object->OTHERLOCALMETHOD );

And thus can be thought of as providing another kind of currying for Moose methods.

AUTHOR

Top

Joel Bernstein, <rataxis at cpan.org>

BUGS

Top

Please report any bugs or feature requests to bug-moosex-deepaccessors at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-DeepAccessors. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc MooseX::DeepAccessors




You can also look for information at:

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-DeepAccessors

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/MooseX-DeepAccessors

* CPAN Ratings

http://cpanratings.perl.org/d/MooseX-DeepAccessors

* Search CPAN

http://search.cpan.org/dist/MooseX-DeepAccessors

ACKNOWLEDGEMENTS

Top

This module was written to scratch an itch I had, but the actual code idea comes from t0m and the impetus to release it from nothingmuch. So thankyou, #moose.

Really, this shouldn't be necessary, and hopefully the next Moose release will integrate this functionality making this module redundant.

COPYRIGHT & LICENSE

Top


MooseX-DeepAccessors documentation Contained in the MooseX-DeepAccessors distribution.
package MooseX::DeepAccessors;
use Moose;
use Scalar::Util qw(blessed);
our $VERSION = '0.02';
extends 'Moose::Meta::Attribute';

has deep_accessors => ( is => 'ro', isa => 'HashRef', default => sub { {} } );

after 'attach_to_class' => sub {
    my ($attr, $class) = @_;

    foreach my $method_name (keys %{ $attr->deep_accessors }) {
        $class->add_method($method_name, 
            $attr->generate_deep_accessor($attr->deep_accessors->{$method_name})
        );
    }
};

sub generate_deep_accessor {
    my ($attr, $spec) = @_;
    my $attrname = $attr->name;
    my ($delegate_method, $callbacks) = %$spec;
    
    sub { 
        my $self = shift; 
        my $value = $self->$attrname;
        my @method_params = map { $self->$_ } @$callbacks;
        return $value->$delegate_method( @method_params );
    };
}

1;

package Moose::Meta::Attribute::Custom::MyDeepAccessors;

sub register_implementation {
    'MooseX::DeepAccessors'
}

1;


__END__
1; # End of MooseX::DeepAccessors