File Coverage

blib/lib/Sub/Talisman/Struct.pm
Criterion Covered Total %
statement 51 51 100.0
branch 6 6 100.0
condition 4 5 80.0
subroutine 12 12 100.0
pod 1 1 100.0
total 74 75 98.6


line stmt bran cond sub pod time code
1             package Sub::Talisman::Struct;
2              
3 2     2   74166 use 5.012;
  2         8  
  2         77  
4 2     2   12 use strict;
  2         4  
  2         63  
5 2     2   12 use warnings;
  2         9  
  2         125  
6              
7             BEGIN {
8 2     2   4 $Sub::Talisman::Struct::AUTHORITY = 'cpan:TOBYINK';
9 2         37 $Sub::Talisman::Struct::VERSION = '0.004';
10             }
11              
12 2     2   13 use base qw( Sub::Talisman );
  2         3  
  2         1913  
13 2     2   309414 use MooX::Struct ();
  2         117702  
  2         59  
14 2     2   20 use Carp qw( confess );
  2         4  
  2         123  
15 2     2   13 use Data::OptList ();
  2         4  
  2         48  
16 2     2   12 use namespace::clean;
  2         4  
  2         16  
17            
18             sub import
19             {
20 4     4   333 my $class = shift;
21 4         10 my $caller = caller;
22            
23 4         4 foreach my $arg (@{ Data::OptList::mkopt(\@_) })
  4         20  
24             {
25 7         171 my ($atr, $str) = @$arg;
26 7         33 $class->setup_for($caller => {
27             attribute => $atr,
28             struct => $str,
29             });
30             }
31             }
32              
33             my %PROCESSORS;
34             my %STRUCTS;
35             sub setup_for
36             {
37 7     7 1 21 my ($class, $caller, $opts) = @_;
38 7         40 $class->SUPER::setup_for($caller, $opts);
39 7   66     2793 my $proc = $PROCESSORS{$caller} //= 'MooX::Struct::Processor'->new;
40 7   100     6621 my $struct = $proc->make_sub(
41             $opts->{attribute},
42             $opts->{struct} || [],
43             );
44 7         437 $STRUCTS{"$caller\::$opts->{attribute}"} = $struct;
45             }
46              
47             sub _process_params
48             {
49 15     15   15053 my ($class, $attr, $params) = @_;
50            
51 15         20 my %new;
52 15 100       16 my @p = @{ $params || [] };
  15         66  
53 15         50 my @f = $STRUCTS{$attr}->()->FIELDS;
54 15 100       6731 confess "Too many parameters for attribute $attr" if @p > @f;
55 14         35 for my $i ( 0 .. $#p )
56             {
57 8         23 $new{ $f[$i] } = $p[$i];
58             }
59 14         20 my $obj = eval { $STRUCTS{$attr}->()->new(%new) };
  14         33  
60 14 100       5993 return $obj if $obj;
61 3         8 chomp(my $msg = $@);
62 3         7 $msg =~ s{ at \(.+?\) line \d+\.?}{};
63 3         603 confess $msg;
64             }
65              
66             1;
67              
68             __END__