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