b2ced0258b3198b6b83d252156aa1787d7a764e4
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor / Native / Array / natatime.pm
1 package Moose::Meta::Method::Accessor::Native::Array::natatime;
2
3 use strict;
4 use warnings;
5
6 use List::MoreUtils ();
7 use Params::Util ();
8
9 our $VERSION = '1.19';
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 use Moose::Role;
14
15 with 'Moose::Meta::Method::Accessor::Native::Reader' => {
16     -excludes => [
17         qw(
18             _minimum_arguments
19             _maximum_arguments
20             _inline_check_arguments
21             _inline_return_value
22             )
23     ]
24 };
25
26 sub _minimum_arguments { 1 }
27
28 sub _maximum_arguments { 2 }
29
30 sub _inline_check_arguments {
31     my $self = shift;
32
33     return (
34         'if (!defined($_[0]) || $_[0] !~ /^\d+$/) {',
35             $self->_inline_throw_error(
36                 '"The n value passed to natatime must be an integer"',
37             ) . ';',
38         '}',
39         'if (@_ == 2 && !Params::Util::_CODELIKE($_[1])) {',
40             $self->_inline_throw_error(
41                 '"The second argument passed to natatime must be a code '
42               . 'reference"',
43             ) . ';',
44         '}',
45     );
46 }
47
48 sub _inline_return_value {
49     my $self = shift;
50     my ($slot_access) = @_;
51
52     return (
53         'my $iter = List::MoreUtils::natatime($_[0], @{ (' . $slot_access . ') });',
54         'if ($_[1]) {',
55             'while (my @vals = $iter->()) {',
56                 '$_[1]->(@vals);',
57             '}',
58         '}',
59         'else {',
60             'return $iter;',
61         '}',
62     );
63 }
64
65 # Not called, but needed to satisfy the Reader role
66 sub _return_value { }
67
68 no Moose::Role;
69
70 1;