MouseX::AttributeHelpers::Collection::Array - MouseX::AttributeHelpers::Collection::Array documentation


MouseX-AttributeHelpers documentation Contained in the MouseX-AttributeHelpers distribution.

Index


Code Index:

NAME

Top

MouseX::AttributeHelpers::Collection::Array

SYNOPSIS

Top

    package MyClass;
    use Mouse;
    use MouseX::AttributeHelpers;

    has 'options' => (
        metaclass => 'Collection::Array',
        is        => 'rw',
        isa       => 'ArrayRef',
        default   => sub { [] },
        provides  => {
            count => 'num_options',
            empty => 'has_options',
            push  => 'add_options',
            pop   => 'remove_last_option',
        },
    );

DESCRIPTION

Top

This module provides an Array attribute which provides a number of array operations.

PROVIDERS

Top

This module also consumes the List method providers. See also MouseX::AttributeHelpers::Collection::List.

push

pop

unshift

shift

get

set

clear

delete

insert

splice

METHODS

Top

method_constructors

helper_type

AUTHOR

Top

NAKAGAWA Masaki <masaki@cpan.org>

LICENSE

Top

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

Top

MouseX::AttributeHelpers, MouseX::AttributeHelpers::Base, MouseX::AttributeHelpers::Collection::List


MouseX-AttributeHelpers documentation Contained in the MouseX-AttributeHelpers distribution.

package MouseX::AttributeHelpers::Collection::Array;
use Mouse;
use MouseX::AttributeHelpers::Collection::List;

extends 'MouseX::AttributeHelpers::Base';

has '+method_constructors' => (
    default => sub {
        my $attr = MouseX::AttributeHelpers::Collection::List->meta->get_attribute('method_constructors');
        return +{
            %{ $attr->default->() }, # apply MouseX::AttributeHelpers::Collection::List
            push => sub {
                my ($attr, $name) = @_;
                return sub { push @{ shift->$name() } => @_ };
            },
            pop => sub {
                my ($attr, $name) = @_;
                return sub { pop @{ $_[0]->$name() } };
            },
            unshift => sub {
                my ($attr, $name) = @_;
                return sub { unshift @{ shift->$name() } => @_ };
            },
            shift => sub {
                my ($attr, $name) = @_;
                return sub { shift @{ $_[0]->$name() } };
            },
            get => sub {
                my ($attr, $name) = @_;
                return sub { $_[0]->$name()->[$_[1]] };
            },
            set => sub {
                my ($attr, $name) = @_;
                return sub { $_[0]->$name()->[$_[1]] = $_[2] };
            },
            clear => sub {
                my ($attr, $name) = @_;
                return sub { @{ $_[0]->$name() } = () };
            },
            delete => sub {
                my ($attr, $name) = @_;
                return sub { splice @{ $_[0]->$name() }, $_[1], 1 };
            },
            insert => sub {
                my ($attr, $name) = @_;
                return sub { splice @{ $_[0]->$name() }, $_[1], 0, $_[2] };
            },
            splice => sub {
                my ($attr, $name) = @_;
                return sub {
                    my ($self, $offset, $length, @args) = @_;
                    splice @{ $self->$name() }, $offset, $length, @args;
                };
            },
        };
    },
);

sub helper_type { 'ArrayRef' }

no Mouse;
__PACKAGE__->meta->make_immutable(inline_constructor => 0);
__END__