File Coverage

blib/lib/Data/Hopen/Class.pm
Criterion Covered Total %
statement 29 29 100.0
branch 10 10 100.0
condition 6 6 100.0
subroutine 7 7 100.0
pod 1 1 100.0
total 53 53 100.0


line stmt bran cond sub pod time code
1             # Data::Hopen::Class - Class::Tiny base class supporting new(-arg => value)
2             package Data::Hopen::Class;
3              
4 1     1   4829 use 5.006;
  1         3  
5 1     1   11 use strict;
  1         2  
  1         25  
6 1     1   6 use warnings;
  1         2  
  1         45  
7 1     1   6 use Carp qw(croak);
  1         16  
  1         75  
8              
9             our $VERSION = '0.000018';
10              
11             # No parent, so Class::Tiny will become the parent
12 1     1   567 use Class::Tiny;
  1         1887  
  1         5  
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 except for a custom C method
23             that permits you to put hyphens in front of the argument names in the
24             constructor call. This provides consistency with L.
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 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 100   16   134 my $class = shift or croak 'Need a class';
47              
48 15 100 100     87 if(@_ == 1 && ref $_[0] eq 'HASH') {
    100 100        
    100          
49 3         5 @_ = ($class, %{$_[0]});
  3         13  
50 3         9 goto \&_data_hopen_class_builder_internal;
51             # No extra stack frame for the sake of croak()
52             } elsif(@_ == 1 && ref $_[0]) {
53 1         3 croak "$class\->new(arg) with @{[ref $_[0]]} instead of HASH ref";
  1         114  
54             } elsif(@_ % 2) {
55 1         258 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         30 my %args = @_;
60 10         28 for (keys %args) {
61 12 100       45 next unless /^-/;
62 6         18 $args{ substr $_, 1 } = $args{ $_ };
63 6         13 delete $args{ $_ };
64             }
65              
66 10         33 return \%args;
67             }; # $_builder()
68              
69             sub BUILDARGS {
70 13     13 1 13742 goto \&Data::Hopen::Class::_data_hopen_class_builder_internal;
71             } #BUILDARGS()
72              
73             1;
74             __END__