| 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
| |||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||
| 48 | 15 | ▶ | if(@_ == 1 && ref $_[0] eq 'HASH') { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
@_ == 1 and ref $_[0] eq "HASH"
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"
| |||||||||||||||||||||||||||||||||||||
| 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]
MC/DC100%2/2
@_ == 1 and ref $_[0]
@_ == 1
ref $_[0]
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0]
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||