MooseX::IOC::Meta::Attribute - MooseX::IOC::Meta::Attribute documentation


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

Index


Code Index:

NAME

Top

MooseX::IOC::Meta::Attribute

DESCRIPTION

Top

No real user serviceable parts in here ... see MooseX::IOC docs.

METHODS

Top

meta

This returns the role meta object.

BUGS

Top

All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT.

AUTHOR

Top

Stevan Little <stevan@iinteractive.com>

COPYRIGHT AND LICENSE

Top


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

package MooseX::IOC::Meta::Attribute;
use Moose;

use IOC::Registry;

our $VERSION = '0.03';

extends 'Moose::Meta::Attribute';

# this never changes, so we can
# just store it once - SL
my $REGISTRY = IOC::Registry->new;

around '_process_options' => sub {
    my $next = shift;
    my ($self, $name, $options) = @_;
    if (exists $options->{service}) {

        my $service = $self->_process_service($options->{service});

        if (exists $options->{default}) {
            my $default = $options->{default};
            $options->{default} = sub {
                local $@;
                eval { $self->_locate_ioc_service($service, @_) } || $default;
            }
        } else {
            $options->{default} = sub { $self->_locate_ioc_service($service, @_) };
        }

        delete $options->{service};
    }
    $next->($self, $name, $options);
};

sub _locate_ioc_service {
    my ( $self, $service, @args ) = @_;
    $REGISTRY->locateService(@{$service->(@args)})
}

sub _process_service {
    my ($self, $service) = @_;
    return $service
        if ref $service eq 'CODE';
    # otherwise ...
    $service = [ $service ] if ref $service ne 'ARRAY';
    return sub { $service };
}

# register this as a metaclass alias ...
package Moose::Meta::Attribute::Custom::IOC;
sub register_implementation { 'MooseX::IOC::Meta::Attribute' }

1;

__END__