File Coverage

blib/lib/Data/Rx/Type/Perl/Code.pm
Criterion Covered Total %
statement 29 31 93.5
branch 10 12 83.3
condition 4 5 80.0
subroutine 8 8 100.0
pod 0 3 0.0
total 51 59 86.4


line stmt bran cond sub pod time code
1 1     1   5 use strict;
  1         2  
  1         49  
2 1     1   5 use warnings;
  1         1  
  1         41  
3             package Data::Rx::Type::Perl::Code;
4             {
5             $Data::Rx::Type::Perl::Code::VERSION = '0.009';
6             }
7             # ABSTRACT: experimental / perl coderef type
8 1     1   4 use parent 'Data::Rx::CommonType::EasyNew';
  1         2  
  1         6  
9              
10              
11 1     1   5186 use Carp ();
  1         2  
  1         12  
12 1     1   4 use Scalar::Util ();
  1         2  
  1         237  
13              
14 1     1 0 22 sub type_uri { 'tag:codesimply.com,2008:rx/perl/code' }
15              
16             sub guts_from_arg {
17 5     5 0 2349 my ($class, $arg, $rx) = @_;
18 5   50     15 $arg ||= {};
19              
20 5         13 for my $key (keys %$arg) {
21 3 50       11 next if $key eq 'prototype';
22 0         0 Carp::croak(
23             "unknown argument $key in constructing " . $class->type_uri . " type",
24             );
25             }
26              
27 5 100       29 my $prototype_schema
    100          
28             = (! exists $arg->{prototype})
29             ? undef
30              
31             : (! defined $arg->{prototype})
32             ? $rx->make_schema('tag:codesimply.com,2008:rx/core/nil')
33              
34             : $rx->make_schema({
35             type => 'tag:codesimply.com,2008:rx/core/str',
36             value => $arg->{prototype}
37             });
38              
39 5         167 return { prototype_schema => $prototype_schema };
40             }
41              
42             sub assert_valid {
43 18     18 0 5317 my ($self, $value) = @_;
44              
45 18 100       40 unless (ref $value) {
46 5         42 $self->fail({
47             error => [ qw(type) ],
48             message => "found value is not a ref",
49             value => $value,
50             });
51             }
52              
53             # Should probably be checking _CALLABLE. -- rjbs, 2009-03-12
54 13 50       51 unless (Scalar::Util::reftype($value) eq 'CODE') {
55 0         0 $self->fail({
56             error => [ qw(type) ],
57             message => "found value is not a CODE ref",
58             value => $value,
59             });
60             }
61              
62 13 100 100     68 if (
63             defined $self->{prototype_schema}
64             && ! $self->{prototype_schema}->check(prototype $value)
65             ) {
66 6         413 $self->fail({
67             error => [ qw(prototype) ],
68             message => "subroutine prototype does not match requirement",
69             value => $value,
70             # data_path => [[ 'prototype', 'prototype', sub { "prototype($_[0])" } ]],
71             });
72             }
73              
74 7         53 return 1;
75             }
76              
77             1;
78              
79             __END__