IDL/Base.pm

filter: Statementstmt 89.5% 43 / 48 Branchbran 56.2% 9 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 83.7% 62 / 74 CC 11 SCAR 15.5
CC
11
Cov
81%
_setup
9.5
children_of_type
4.0
_overload_string
2.1
CRAP 4.7
« Parser/Type/list.pm ↑ Summary Parser/Method.pm »
1 package Thrift::IDL::Base;
2  
3 =head1 NAME
4  
5 Thrift::IDL::Base
6  
7 =head1 DESCRIPTION
8  
9 Base class for most L<Thrift::IDL> subclasses.
10  
11 =cut
12  
13 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 __PACKAGE__->mk_accessors(qw(parent comments));
17  
18 =head1 METHODS
19  
20 =head2 parent
21  
22 =head2 comments
23  
24 Accessors
25  
26 =cut
27  
28 use overload
29 4 ▶ '""' => \&_overload_string,
Subroutine BEGIN: called CC=1 SCAR=0.0
30 4 'eq' => \&_overload_string;
31  
32 20 ▶sub _overload_string {
Subroutine _overload_string: called CC=2 SCAR=7.2
33 20 my $self = shift;
34 20 ▶ return $self->can('to_str') ? $self->to_str : ref($self);
Branch: 1 branch
BranchTrueFalseTotal
$self->can('to_str') ? : 20 0 20
35 }
36  
37 =head2 children_of_type ($type)
38  
39 my $comments = $obj->children_of_type('Thrift::IDL::Comment');
40  
41 Returns an array ref of all child objects of the given class
42  
43 =cut
44  
45 6 ▶sub children_of_type {
Subroutine children_of_type: called CC=4 SCAR=13.9
Pod documented
46 6 my ($self, $type) = @_;
47  
48 6 my $cache_key = 'children.' . $type;
49 6 ▶ return $self->{$cache_key} if $self->{$cache_key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{$cache_key} 2 4 6
50  
51 4 $self->{$cache_key} = [];
52 4 foreach my $child (@{ $self->{children} }) {
53 25 ▶ push @{ $self->{$cache_key} }, $child if $child->isa($type);
Branch: 1 branch
BranchTrueFalseTotal
if $child->isa($type) 9 16 25
54 }
55 4 return $self->{$cache_key};
56 }
57  
58 =head2 array_search ($value, $array_method, $method)
59  
60 my $Calculator_service = $document->array_search('Calculator', 'services', 'name');
61  
62 Given a method $array_method which returns an array of objects on $self, return the object which has $value = $object->$method
63  
64 =cut
65  
66 10 ▶sub array_search {
Subroutine array_search: called CC=2 SCAR=6.9
Pod documented
67 10 my ($self, $value, $array_method, $method) = @_;
68  
69 10 my $cache_key = join '.', 'array_idx', $array_method, $method;
70 10 ▶ if (! $self->{$cache_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$cache_key}) 8 2 10
71 $self->{$cache_key} = {
72 23 map { $_->$method => $_ }
73 8 @{ $self->$array_method }
74 };
75 }
76 10 return $self->{$cache_key}{$value};
77 }
78  
79 =head2 setup
80  
81 A struct has children of type L<Thrift::IDL::Field> and L<Thrift::IDL::Comment>. Walk through all these children and associate the comments with the fields that preceeded them (if perl style) or with the field following.
82  
83 =cut
84  
85 17 ▶sub _setup {
Subroutine _setup: called CC=6 SCAR=22.5
86 17 my ($self, $key) = @_;
87 17 ▶ return if $self->{"_setup_called_$key"}++;
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"_setup_called_$key"}++ 0 17 17
88  
89 17 my (@fields, @comments, $last_field);
90 17 foreach my $child (@{ $self->$key }) {
91 21 ▶ if ($child->isa('Thrift::IDL::Field')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($child->isa('Thrift::IDL::Field')) { } 21 0 21
elsif ($child->isa('Thrift::IDL::Comment')) { } 0 0 0
92 21 $child->{comments} = [ @comments ];
93 21 push @fields, $child;
94 21 $last_field = $child;
95 21 @comments = ();
96 }
97 elsif ($child->isa('Thrift::IDL::Comment')) {
98 # Perl-style comments are postfix to the previous element
99 0 ▶ if ($child->style eq 'perl_single') {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->style eq 'perl_single') { } 0 0 0
100 0 push @{ $last_field->{comments} }, $child;
101 }
102 else {
103 0 push @comments, $child;
104 }
105 }
106 else {
107 0 die "Unrecognized child of ".ref($self)." (".ref($child)."\n";
108 }
109 }
110 17 $self->$key(\@fields);
111 }
112  
113 4 1;
« Parser/Type/list.pm ↑ Summary Parser/Method.pm »