TPath/Attribute.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« TPath/Test/Not.pm ↑ Summary TPath/Concatenation.pm »
1 package TPath::Attribute;
2 $TPath::Attribute::VERSION = '1.007';
3 # ABSTRACT: handles evaluating an attribute for a particular node
4  
5  
6 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶no if $] >= 5.018, warnings => "experimental";
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 0 ▶use TPath::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use namespace::autoclean;
12  
13  
14 with qw(TPath::Test TPath::Numifiable);
15  
16  
17 has name => ( is => 'ro', isa => 'Str', required => 1 );
18  
19  
20 has args => ( is => 'ro', isa => 'ArrayRef', required => 1 );
21  
22 # caches results of reflective code for processing args
23 has _arg_subs => (
24 is => 'ro',
25 isa => 'ArrayRef[CodeRef]',
26 lazy => 1,
27 builder => '_build_arg_subs'
28 );
29  
30 has _expr => ( is => 'rw', isa => 'TPath::Expression', weak_ref => 1 )
31 ;
32  
33  
34 has code => ( is => 'ro', isa => 'CodeRef', required => 1 );
35  
36  
37 has autoloaded => ( is => 'ro', isa => 'Bool', default => 0 );
38  
39  
40 sub apply {
41 my ( $self, $ctx ) = @_;
42 $ctx->expression( $self->_expr );
43 my @args = ($ctx);
44  
45 # invoke all code to reify arguments
46 push @args, $_->($ctx) for @{ $self->_arg_subs };
47 $self->code->( $ctx->i->f, @args );
48 }
49  
50 sub _build_arg_subs {
51 my $self = shift;
52 my @codes;
53 for my $a ( @{ $self->args } ) {
54 my $type = ref $a;
55 my $sub;
56 if ( $type && $type !~ /ARRAY|HASH/ ) {
57 if ( $a->isa('TPath::Attribute') ) {
58 $sub = sub { $a->apply(shift) };
59 }
60 elsif ( $a->isa('TPath::Concatenation') ) {
61 $sub = sub { $a->concatenate(shift) };
62 }
63 elsif ( $a->isa('TPath::AttributeTest') ) {
64 $sub = sub { $a->test(shift) };
65 }
66 elsif ( $a->isa('TPath::Expression') ) {
67 $sub = sub {
68 [ map { $_->n } @{ $a->_select( shift, 0 ) } ];
69 };
70 }
71 elsif ( $a->does('TPath::Test') ) {
72 $sub = sub { $a->test(shift) };
73 }
74 else { confess "unknown argument type: $type" }
75 }
76 else {
77 $sub = sub { $a };
78 }
79 push @codes, $sub;
80 }
81 return \@codes;
82 }
83  
84  
85 sub to_num {
86 my ( $self, $ctx ) = @_;
87 my $val = $self->apply($ctx);
88 for ( ref $val ) {
89 when ('ARRAY') { return scalar @$val }
90 when ('HASH') { return scalar keys %$val }
91 default { return 0 + $val }
92 }
93 }
94  
95 # required by TPath::Test
96 sub test {
97 my ( $self, $ctx ) = @_;
98 defined $self->apply($ctx);
99 }
100  
101 sub to_string {
102 my $self = shift;
103 my $s = '@';
104 $s .= ':' if $self->autoloaded;
105 $s .= $self->_stringify_label( $self->name );
106 my @args = @{ $self->args };
107 if (@args) {
108 $s .= '(' . $self->_stringify( $args[0] );
109 for my $arg ( @args[ 1 .. $#args ] ) {
110 $s .= ', ' . $self->_stringify($arg);
111 }
112 $s .= ')';
113 }
114 return $s;
115 }
116  
117 __PACKAGE__->meta->make_immutable;
118  
119 1;
120  
121 __END__
« TPath/Test/Not.pm ↑ Summary TPath/Concatenation.pm »