| 1 | # App::GitFind::Class - Class::Tiny base class supporting new(-arg => value) | ||
| 2 | package App::GitFind::Class; | ||
| 3 | |||
| 4 | 1 | ▶ | use 5.010; |
|
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 | our $VERSION = '0.000016'; # TRIAL | ||
| 10 | |||
| 11 | # No parent, so Class::Tiny will become the parent | ||
| 12 | 1 | ▶ | use Class::Tiny; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | |||
| 14 | # Docs {{{1 | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | App::GitFind::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 _app_gitfind_class_builder_internal { | ||
| 46 | my $class = shift or croak 'Need a class'; | ||
| 47 | |||
| 48 | if(@_ == 1 && ref $_[0] eq 'HASH') { | ||
| 49 | @_ = ($class, %{$_[0]}); | ||
| 50 | goto \&_app_gitfind_class_builder_internal; | ||
| 51 | # No extra stack frame for the sake of croak() | ||
| 52 | } elsif(@_ == 1 && ref $_[0]) { | ||
| 53 | croak "$class\->new(arg) with @{[ref $_[0]]} instead of HASH ref"; | ||
| 54 | } elsif(@_ % 2) { | ||
| 55 | croak "Odd number of arguments to $class\->new()"; | ||
| 56 | } | ||
| 57 | |||
| 58 | # Now we have key-value pairs. Trim leading hyphens on the keys. | ||
| 59 | my %args = @_; | ||
| 60 | for (keys %args) { | ||
| 61 | next unless /^-/; | ||
| 62 | $args{ substr $_, 1 } = $args{ $_ }; | ||
| 63 | delete $args{ $_ }; | ||
| 64 | } | ||
| 65 | |||
| 66 | return \%args; | ||
| 67 | }; # $_builder() | ||
| 68 | |||
| 69 | sub BUILDARGS { | ||
| 70 | goto \&App::GitFind::Class::_app_gitfind_class_builder_internal; | ||
| 71 | } #BUILDARGS() | ||
| 72 | |||
| 73 | 1; | ||
| 74 | __END__ | ||