| MouseX-AttributeHelpers documentation | Contained in the MouseX-AttributeHelpers distribution. |
MouseX::AttributeHelpers::Collection::List
package MyClass;
use Mouse;
use MouseX::AttributeHelpers;
has 'options' => (
metaclass => 'Collection::List',
is => 'rw',
isa => 'ArrayRef',
default => sub { [] },
provides => {
count => 'num_options',
empty => 'has_options',
map => 'map_options',
grep => 'filter_options',
elements => 'all_options',
},
);
This module provides an List attribute which provides a number of list operations.
NAKAGAWA Masaki <masaki@cpan.org>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| MouseX-AttributeHelpers documentation | Contained in the MouseX-AttributeHelpers distribution. |
package MouseX::AttributeHelpers::Collection::List; use Mouse; extends 'MouseX::AttributeHelpers::Base'; has '+method_constructors' => ( default => sub { return +{ count => sub { my ($attr, $name) = @_; return sub { scalar @{ $_[0]->$name() } }; }, empty => sub { my ($attr, $name) = @_; return sub { scalar @{ $_[0]->$name() } ? 1 : 0 }; }, find => sub { my ($attr, $name) = @_; return sub { for my $v (@{ $_[0]->$name() }) { return $v if $_[1]->($v); } return; }; }, map => sub { my ($attr, $name) = @_; return sub { map { $_[1]->($_) } @{ $_[0]->$name() } }; }, grep => sub { my ($attr, $name) = @_; return sub { grep { $_[1]->($_) } @{ $_[0]->$name() } }; }, elements => sub { my ($attr, $name) = @_; return sub { @{ $_[0]->$name() } }; }, join => sub { my ($attr, $name) = @_; return sub { join $_[1], @{ $_[0]->$name() } }; }, get => sub { my ($attr, $name) = @_; return sub { $_[0]->$name()->[$_[1]] }; }, first => sub { my ($attr, $name) = @_; return sub { $_[0]->$name()->[0] }; }, last => sub { my ($attr, $name) = @_; return sub { $_[0]->$name()->[-1] }; }, }; }, ); sub helper_type { 'ArrayRef' } no Mouse; __PACKAGE__->meta->make_immutable(inline_constructor => 0); __END__