File Coverage

lib/App/GitFind/Class.pm
Criterion Covered Total %
statement 23 31 74.1
branch 5 10 50.0
condition 2 6 33.3
subroutine 7 7 100.0
pod 1 1 100.0
total 38 55 69.0


line stmt bran cond sub pod time code
1             # App::GitFind::Class - Class::Tiny base class supporting new(-arg => value)
2             package App::GitFind::Class;
3              
4 2     2   750 use 5.010;
  2         5  
5 2     2   8 use strict;
  2         3  
  2         30  
6 2     2   7 use warnings;
  2         3  
  2         34  
7 2     2   362 use App::GitFind::Base;
  2         3  
  2         228  
8              
9             our $VERSION = '0.000002';
10              
11             # No parent, so Class::Tiny will become the parent
12 2     2   847 use Class::Tiny;
  2         4056  
  2         7  
13              
14             # Docs {{{1
15              
16             =head1 NAME
17              
18             App::GitFind::Class - Class::Tiny base class supporting 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 _app_gitfind_class_builder_internal {
46 1 50   1   4 my $class = shift or require Carp, Carp::croak 'Need a class';
47              
48 1 50 33     9 if(@_ == 1 && ref $_[0] eq 'HASH') {
    50 33        
    50          
49 0         0 @_ = ($class, %{$_[0]});
  0         0  
50 0         0 goto \&_app_gitfind_class_builder_internal;
51             # No extra stack frame for the sake of croak()
52             } elsif(@_ == 1 && ref $_[0]) {
53 0         0 require Carp;
54 0         0 Carp::croak "$class\->new(arg) with @{[ref $_[0]]} instead of HASH ref";
  0         0  
55             } elsif(@_ % 2) {
56 0         0 require Carp;
57 0         0 Carp::croak "Odd number of arguments to $class\->new()";
58             }
59              
60             # Now we have key-value pairs. Trim leading hyphens on the keys.
61 1         4 my %args = @_;
62 1         3 for (keys %args) {
63 2 50       7 next unless /^-/;
64 2         5 $args{ substr $_, 1 } = $args{ $_ };
65 2         5 delete $args{ $_ };
66             }
67              
68 1         3 return \%args;
69             }; # $_builder()
70              
71             sub BUILDARGS {
72 1     1 1 131 goto \&App::GitFind::Class::_app_gitfind_class_builder_internal;
73             } #BUILDARGS()
74              
75             1;
76             __END__
77             # vi: set fdm=marker: #