| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Mock::Data::GeneratorSub; | 
| 2 | 9 |  |  | 9 |  | 66 | use strict; | 
|  | 9 |  |  |  |  | 20 |  | 
|  | 9 |  |  |  |  | 301 |  | 
| 3 | 9 |  |  | 9 |  | 49 | use warnings; | 
|  | 9 |  |  |  |  | 17 |  | 
|  | 9 |  |  |  |  | 4912 |  | 
| 4 |  |  |  |  |  |  | require Mock::Data::Generator; | 
| 5 |  |  |  |  |  |  | our @ISA= qw( Mock::Data::Generator ); | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | # ABSTRACT: Wrap a coderef to become a blessed Generator object | 
| 8 |  |  |  |  |  |  | our $VERSION = '0.01'; # VERSION | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | sub new { | 
| 12 | 13 |  |  | 13 | 1 | 32 | my ($class, $coderef, @params)= @_; | 
| 13 | 13 | 50 |  |  |  | 52 | if (ref $coderef eq 'HASH') { | 
| 14 | 0 | 0 |  |  |  | 0 | @params= @{ $coderef->{params} || [] }; | 
|  | 0 |  |  |  |  | 0 |  | 
| 15 | 0 |  |  |  |  | 0 | $coderef= $coderef->{coderef}; | 
| 16 |  |  |  |  |  |  | } | 
| 17 | 13 | 50 |  |  |  | 31 | if (ref $class) { | 
| 18 | 0 |  | 0 |  |  | 0 | $coderef ||= $class->{coderef}; | 
| 19 | 0 |  |  |  |  | 0 | @params= $class->_merge_params(@params); | 
| 20 | 0 |  |  |  |  | 0 | $class= ref $class; | 
| 21 |  |  |  |  |  |  | } | 
| 22 | 13 | 50 |  |  |  | 65 | Scalar::Util::reftype($coderef) eq 'CODE' or Carp::croak("Not a coderef"); | 
| 23 | 13 |  |  |  |  | 60 | bless { | 
| 24 |  |  |  |  |  |  | coderef => $coderef, | 
| 25 |  |  |  |  |  |  | params => \@params, | 
| 26 |  |  |  |  |  |  | }, $class; | 
| 27 |  |  |  |  |  |  | } | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | sub _merge_params { | 
| 30 | 10 |  |  | 10 |  | 18 | my $self= shift; | 
| 31 | 10 |  |  |  |  | 20 | my $p= $self->{params}; | 
| 32 | 10 | 50 |  |  |  | 23 | my $named_p= ref $p->[0] eq 'HASH'? $p->[0] : undef; | 
| 33 |  |  |  |  |  |  | # Merge any options-by-name newly supplied with options-by-name from @params | 
| 34 | 10 | 0 |  |  |  | 22 | unshift @_, (ref $_[0] eq 'HASH')? { %$named_p, %{shift @_} } : $named_p | 
|  | 0 | 50 |  |  |  | 0 |  | 
| 35 |  |  |  |  |  |  | if $named_p; | 
| 36 |  |  |  |  |  |  | # Append positional params if none provided | 
| 37 | 10 | 100 |  |  |  | 31 | push @_, @{$p}[1..$#$p] | 
|  | 9 |  |  |  |  | 17 |  | 
| 38 |  |  |  |  |  |  | unless @_ > 1; | 
| 39 | 10 |  |  |  |  | 32 | return @_; | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | sub generate { | 
| 44 | 11 |  |  | 11 | 1 | 28 | my ($self, $mock)= (shift, shift); | 
| 45 | 11 | 100 |  |  |  | 38 | $self->{coderef}->($mock, @_? $self->_merge_params(@_) : @{$self->{params}}); | 
|  | 1 |  |  |  |  | 25 |  | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | sub compile { | 
| 50 | 4 |  |  | 4 | 1 | 9 | my $self= shift; | 
| 51 | 4 |  |  |  |  | 12 | my $params= $self->{params}; | 
| 52 | 4 | 50 | 33 |  |  | 50 | return $self->{coderef} unless @_ || @$params; | 
| 53 | 0 |  |  |  |  |  | my @new_params= $self->_merge_params(@_); | 
| 54 | 0 |  |  |  |  |  | my $coderef= $self->{coderef}; | 
| 55 | 0 |  |  | 0 |  |  | return sub { $coderef->(shift, @new_params) }; | 
|  | 0 |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | 1; | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | __END__ |