/usr/share/perl5/MooseX/AttributeHelpers/MethodProvider/List.pm is in libmoosex-attributehelpers-perl 0.23-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | package MooseX::AttributeHelpers::MethodProvider::List;
use Moose::Role;
our $VERSION = '0.23';
$VERSION = eval $VERSION;
our $AUTHORITY = 'cpan:STEVAN';
sub count : method {
my ($attr, $reader, $writer) = @_;
return sub {
scalar @{$reader->($_[0])}
};
}
sub empty : method {
my ($attr, $reader, $writer) = @_;
return sub {
scalar @{$reader->($_[0])} ? 1 : 0
};
}
sub find : method {
my ($attr, $reader, $writer) = @_;
return sub {
my ($instance, $predicate) = @_;
foreach my $val (@{$reader->($instance)}) {
return $val if $predicate->($val);
}
return;
};
}
sub map : method {
my ($attr, $reader, $writer) = @_;
return sub {
my ($instance, $f) = @_;
CORE::map { $f->($_) } @{$reader->($instance)}
};
}
sub sort : method {
my ($attr, $reader, $writer) = @_;
return sub {
my ($instance, $predicate) = @_;
die "Argument must be a code reference"
if $predicate && ref $predicate ne 'CODE';
if ($predicate) {
CORE::sort { $predicate->($a, $b) } @{$reader->($instance)};
}
else {
CORE::sort @{$reader->($instance)};
}
};
}
sub grep : method {
my ($attr, $reader, $writer) = @_;
return sub {
my ($instance, $predicate) = @_;
CORE::grep { $predicate->($_) } @{$reader->($instance)}
};
}
sub elements : method {
my ($attr, $reader, $writer) = @_;
return sub {
my ($instance) = @_;
@{$reader->($instance)}
};
}
sub join : method {
my ($attr, $reader, $writer) = @_;
return sub {
my ($instance, $separator) = @_;
join $separator, @{$reader->($instance)}
};
}
sub get : method {
my ($attr, $reader, $writer) = @_;
return sub {
$reader->($_[0])->[$_[1]]
};
}
sub first : method {
my ($attr, $reader, $writer) = @_;
return sub {
$reader->($_[0])->[0]
};
}
sub last : method {
my ($attr, $reader, $writer) = @_;
return sub {
$reader->($_[0])->[-1]
};
}
1;
__END__
=pod
=head1 NAME
MooseX::AttributeHelpers::MethodProvider::List - method generator for MooseX::AttributeHelpers::Collection::List
=head1 SYNOPSIS
package Stuff;
use Moose;
use MooseX::AttributeHelpers;
has 'options' => (
metaclass => 'Collection::List',
is => 'rw',
isa => 'ArrayRef[Str]',
default => sub { [] },
auto_deref => 1,
provides => {
elements => 'all_options',
map => 'map_options',
grep => 'filter_options',
find => 'find_option',
first => 'first_option',
last => 'last_option',
get => 'get_option',
join => 'join_options',
count => 'count_options',
empty => 'do_i_have_options',
sort => 'sorted_options',
}
);
no Moose;
1;
=head1 DESCRIPTION
This is a role which provides the method generators for
L<MooseX::AttributeHelpers::Collection::List>.
=head1 METHODS
=over 4
=item B<meta>
=back
=head1 PROVIDED METHODS
=over 4
=item B<count>
Returns the number of elements in the list.
$stuff = Stuff->new;
$stuff->options(["foo", "bar", "baz", "boo"]);
my $count = $stuff->count_options;
print "$count\n"; # prints 4
=item B<empty>
If the list is populated, returns true. Otherwise, returns false.
$stuff->do_i_have_options ? print "Good boy.\n" : die "No options!\n" ;
=item B<find>
This method accepts a subroutine reference as its argument. That sub
will receive each element of the list in turn. If it returns true for
an element, that element will be returned by the C<find> method.
my $found = $stuff->find_option( sub { $_[0] =~ /^b/ } );
print "$found\n"; # prints "bar"
=item B<grep>
This method accepts a subroutine reference as its argument. This
method returns every element for which that subroutine reference
returns a true value.
my @found = $stuff->filter_options( sub { $_[0] =~ /^b/ } );
print "@found\n"; # prints "bar baz boo"
=item B<map>
This method accepts a subroutine reference as its argument. The
subroutine will be executed for each element of the list. It is
expected to return a modified version of that element. The return
value of the method is a list of the modified options.
my @mod_options = $stuff->map_options( sub { $_[0] . "-tag" } );
print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
=item B<sort>
Sorts and returns the elements of the list.
You can provide an optional subroutine reference to sort with (as you
can with the core C<sort> function). However, instead of using C<$a>
and C<$b>, you will need to use C<$_[0]> and C<$_[1]> instead.
# ascending ASCIIbetical
my @sorted = $stuff->sort_options();
# Descending alphabetical order
my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } );
print "@sorted_options\n"; # prints "foo boo baz bar"
=item B<elements>
Returns all of the elements of the list
my @option = $stuff->all_options;
print "@options\n"; # prints "foo bar baz boo"
=item B<join>
Joins every element of the list using the separator given as argument.
my $joined = $stuff->join_options( ':' );
print "$joined\n"; # prints "foo:bar:baz:boo"
=item B<get>
Returns an element of the list by its index.
my $option = $stuff->get_option(1);
print "$option\n"; # prints "bar"
=item B<first>
Returns the first element of the list.
my $first = $stuff->first_option;
print "$first\n"; # prints "foo"
=item B<last>
Returns the last element of the list.
my $last = $stuff->last_option;
print "$last\n"; # prints "boo"
=back
=head1 BUGS
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.
=head1 AUTHOR
Stevan Little E<lt>stevan@iinteractive.comE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright 2007-2009 by Infinity Interactive, Inc.
L<http://www.iinteractive.com>
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
|