| 1 | package Catmandu::Fix::Has; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 81 | ▶ | use Catmandu::Sane; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 81 | our $VERSION = '1.0201_02'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 81 | ▶ | use Class::Method::Modifiers qw(install_modifier); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | sub import { | ||||||||||||||||||||||||||||
| 10 | 133 | ▶ | my $target = caller; | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 81 | ▶ | my $around = do {no strict 'refs'; \&{"${target}::around"}}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 133 | my $fix_args = []; | |||||||||||||||||||||||||||
| 14 | 133 | my $fix_opts = []; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | install_modifier( | ||||||||||||||||||||||||||||
| 17 | $target, 'around', 'has', | ||||||||||||||||||||||||||||
| 18 | sub { | ||||||||||||||||||||||||||||
| 19 | 253 | ▶ | my ($orig, $attr, %opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | return $orig->($attr, %opts) | ||||||||||||||||||||||||||||
| 22 | 253 | ▶ | unless exists $opts{fix_arg} || exists $opts{fix_opt}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $opts{"fix_arg"} or exists $opts{"fix_opt"}
MC/DC100%2/2
exists $opts{"fix_arg"} or exists $opts{"fix_opt"}
exists $opts{"fix_arg"}
exists $opts{"fix_opt"}
Truth table100%3/3
A or B
A= exists $opts{"fix_arg"}B= exists $opts{"fix_opt"}
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 236 | ▶ | $opts{is} //= 'ro'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts{"is"} //= "ro"
MC/DC0%0/1
$opts{"is"} //= "ro"
$opts{"is"}
Truth table50%1/2
A
A= $opts{"is"}
| |||||||||||||||||||||||||||||
| 25 | 236 | ▶ | $opts{init_arg} //= $attr; | ||||||||||||||||||||||||||
|
Condition66%2/3
$opts{"init_arg"} //= $attr
MC/DC0%0/2
$opts{"init_arg"} //= $attr
$opts{"init_arg"}
$attr
Truth table66%2/3
A //= B
A= $opts{"init_arg"}B= $attr
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 236 | my $arg = {key => $opts{init_arg}}; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 236 | ▶ | if ($opts{fix_arg}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 203 | ▶ | $opts{required} //= 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts{"required"} //= 1
MC/DC0%0/1
$opts{"required"} //= 1
$opts{"required"}
Truth table50%1/2
A
A= $opts{"required"}
| |||||||||||||||||||||||||||||
| 31 | 203 | ▶ | $arg->{collect} = 1 if $opts{fix_arg} eq 'collect'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 203 | push @$fix_args, $arg; | |||||||||||||||||||||||||||
| 33 | 203 | delete $opts{fix_arg}; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 236 | ▶ | if ($opts{fix_opt}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 33 | ▶ | $arg->{collect} = 1 if $opts{fix_opt} eq 'collect'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 33 | push @$fix_opts, $arg; | |||||||||||||||||||||||||||
| 39 | 33 | delete $opts{fix_opt}; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 236 | $orig->($attr, %opts); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 133 | ); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | $around->( | ||||||||||||||||||||||||||||
| 47 | 'BUILDARGS', | ||||||||||||||||||||||||||||
| 48 | sub { | ||||||||||||||||||||||||||||
| 49 | 450 | ▶ | my $orig = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||
| 50 | 450 | my $self = shift; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 450 | ▶ | return $orig->($self, @_) unless @$fix_args || @$fix_opts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@$fix_args or @$fix_opts
MC/DC0%0/2
@$fix_args or @$fix_opts
@$fix_args
@$fix_opts
Truth table66%2/3
A or B
A= @$fix_argsB= @$fix_opts
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 450 | my $args = {}; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 450 | for my $arg (@$fix_args) { | |||||||||||||||||||||||||||
| 57 | 685 | ▶ | last unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 672 | my $key = $arg->{key}; | |||||||||||||||||||||||||||
| 59 | 672 | ▶ | if ($arg->{collect}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 13 | $args->{$key} = [splice @_, 0, @_]; | |||||||||||||||||||||||||||
| 61 | 13 | last; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 659 | $args->{$key} = shift; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 450 | my $orig_args = $self->$orig(@_); | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 450 | for my $arg (@$fix_opts) { | |||||||||||||||||||||||||||
| 69 | 214 | my $key = $arg->{key}; | |||||||||||||||||||||||||||
| 70 | 214 | ▶ | if ($arg->{collect}) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 71 | 32 | $args->{$key} = $orig_args; | |||||||||||||||||||||||||||
| 72 | 32 | last; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | elsif (exists $orig_args->{"-$key"}) { | ||||||||||||||||||||||||||||
| 75 | 1 | $args->{$key} = delete $orig_args->{"-$key"}; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | elsif (exists $orig_args->{$key}) { | ||||||||||||||||||||||||||||
| 78 | 36 | $args->{$key} = delete $orig_args->{$key}; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 450 | $args; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 133 | ); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 81 | 1; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | __END__ | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =pod | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head1 NAME | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | Catmandu::Fix::Has - helper class for creating Fix-es with (optional) parameters | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | package Catmandu::Fix::foo; | ||||||||||||||||||||||||||||
| 100 | use Moo; | ||||||||||||||||||||||||||||
| 101 | use Catmandu::Fix::Has; | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | has greeting => (fix_arg => 1); # required parameter 1 | ||||||||||||||||||||||||||||
| 104 | has message => (fix_arg => 1); # required parameter 2 | ||||||||||||||||||||||||||||
| 105 | has eol => (fix_opt => 1 , default => sub {'!'} ); # optional parameter 'eol' with default '!' | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | sub fix { | ||||||||||||||||||||||||||||
| 108 | my ($self,$data) = @_; | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | print STDERR $self->greeting . ", " . $self->message . $self->eol . "\n"; | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | $data; | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 1; | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =head1 PARAMETERS | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =over 4 | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =item fix_arg | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | Required argument when set to 1. The Fix containing the code fragment below needs | ||||||||||||||||||||||||||||
| 124 | two arguments. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | use Catmandu::Fix::Has; | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | has message => (fix_arg => 1); # required parameter 1 | ||||||||||||||||||||||||||||
| 129 | has number => (fix_arg => 1); # required parameter 2 | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | When the fix_arg is set to 'collect', then all arguments are read into an | ||||||||||||||||||||||||||||
| 132 | array. The Fix containing the code fragment below needs at least 1 or more | ||||||||||||||||||||||||||||
| 133 | arguments. All arguments will get collected into the C<messages> array: | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | use Catmandu::Fix::Has; | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | has messages => (fix_arg => 'collect'); # required parameter | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =item fix_opt | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Optional named argument when set to 1. The Fix containing the code fragment | ||||||||||||||||||||||||||||
| 142 | below can have two optional arguments C<message: ...>, C<number: ...>: | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | use Catmandu::Fix::Has; | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | has message => (fix_opt => 1); # optional parameter 1 | ||||||||||||||||||||||||||||
| 147 | has number => (fix_opt => 1); # optional parameter 2 | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | When the fix_opt is set to 'collect', then all optional argument are read into | ||||||||||||||||||||||||||||
| 150 | an array. The Fix containing the code fragment below needs at least 1 or more | ||||||||||||||||||||||||||||
| 151 | arguments. All arguments will get collected into the C<options> array: | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | use Catmandu::Fix::Has; | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | has options => (fix_opt => 'collect'); # optional parameter | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =back | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | L<Catmandu::Fix> | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =cut | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||