Hopen/Class.pm

filter: Statementstmt 100% 31 / 31 Branchbran 100% 10 / 10 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 100% 59 / 59 CC 9 SCAR 17.6
CC
9
Cov
100%
_data_hopen_class_builder_internal
9.0
BEGIN
1.0
BEGIN
1.0
CRAP 5.8
« Hopen/Util/Data.pm ↑ Summary Hopen/Scope.pm »
1 # Data::Hopen::Class - Class::Tiny base class supporting new(-arg => value)
2 package Data::Hopen::Class;
3  
4 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.000018';
10  
11 # No parent, so Class::Tiny will become the parent
12 1 ▶use Class::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # Docs {{{1
15  
16 =head1 NAME
17  
18 Data::Hopen::Class - Class::Tiny base class supporting C<< new(-arg => value) >>
19  
20 =head1 SYNOPSIS
21  
22 This is the same as L<Class::Tiny> except for a custom C<BUILDARGS()> method
23 that permits you to put hyphens in front of the argument names in the
24 constructor call. This provides consistency with L<Getargs::Mixed>.
25  
26 =cut
27  
28 # }}}1
29  
30 =head1 FUNCTIONS
31  
32 =head2 BUILDARGS
33  
34 Pre-process the constructor arguments to strip leading hyphens from
35 argument names.
36  
37 Note: L<Class::Tiny> prohibits attributes named C<-> (a single hyphen),
38 so we don't handle that case.
39  
40 =cut
41  
42 # The internal builder, so I don't have to worry about dispatch
43 # once our BUILDARGS is called.
44  
45 sub _data_hopen_class_builder_internal {
46 16 ▶ my $class = shift or croak 'Need a class';
Subroutine _data_hopen_class_builder_internal: called CC=9 SCAR=22.0
Branch: 1 branch
BranchTrueFalseTotal
my $class = shift() or croak("Need a class") 1 15 16
47  
48 15 ▶ if(@_ == 1 && ref $_[0] eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 3 12 15
elsif (@_ == 1 and ref $_[0]) { } 1 11 12
elsif (@_ % 2) { } 1 10 11
Condition100%3/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
1023
MC/DC100%2/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
49 3 @_ = ($class, %{$_[0]});
50 3 goto \&_data_hopen_class_builder_internal;
51 # No extra stack frame for the sake of croak()
52 ▶ } elsif(@_ == 1 && ref $_[0]) {
Condition100%3/3
@_ == 1 and ref $_[0]
!ll&&!rl&&r
1011
MC/DC100%2/2
@_ == 1 and ref $_[0]
@_ == 1 ref $_[0]
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0]
ABresult
0X0
100
111
53 1 croak "$class\->new(arg) with @{[ref $_[0]]} instead of HASH ref";
54 } elsif(@_ % 2) {
55 1 croak "Odd number of arguments to $class\->new()";
56 }
57  
58 # Now we have key-value pairs. Trim leading hyphens on the keys.
59 10 my %args = @_;
60 10 for (keys %args) {
61 12 ▶ next unless /^-/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^-/ 6 6 12
62 6 $args{ substr $_, 1 } = $args{ $_ };
63 6 delete $args{ $_ };
64 }
65  
66 10 return \%args;
67 }; # $_builder()
68  
69 sub BUILDARGS {
70 13 ▶ goto \&Data::Hopen::Class::_data_hopen_class_builder_internal;
Subroutine BUILDARGS: called CC=1 SCAR=0.0
Pod documented
71 } #BUILDARGS()
72  
73 1 1;
74 __END__
« Hopen/Util/Data.pm ↑ Summary Hopen/Scope.pm »