| 1 | package Catmandu::Fix::Bind; | ||||||||||
| 2 | |||||||||||
| 3 | 12 | ▶ | use Catmandu::Sane; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 12 | our $VERSION = '1.2025'; | |||||||||
| 6 | |||||||||||
| 7 | 12 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 12 | ▶ | use Package::Stash; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 12 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 12 | with 'Catmandu::Logger'; | |||||||||
| 12 | |||||||||||
| 13 | 12 | requires 'unit'; | |||||||||
| 14 | 12 | requires 'bind'; | |||||||||
| 15 | |||||||||||
| 16 | 118 | ▶ | has __return__ => (is => 'rw', default => sub {[0]}); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 118 | ▶ | has __fixes__ => (is => 'rw', default => sub {[]}); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 174 | ▶ | around bind => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.4
| |||||||||||
| 20 | 174 | my ($orig, $self, $prev, @args) = @_; | |||||||||
| 21 | 174 | my $next = $orig->($self, $prev, @args); | |||||||||
| 22 | |||||||||||
| 23 | 163 | ▶ | if ($self->can('plus')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 0 | return $self->plus($prev, $next); | |||||||||
| 25 | } | ||||||||||
| 26 | else { | ||||||||||
| 27 | 163 | return $next; | |||||||||
| 28 | } | ||||||||||
| 29 | 12 | }; | |||||||||
| 30 | |||||||||||
| 31 | 56 | ▶ | sub unit { | ||||||||
|
Subroutine
unit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 32 | 56 | my ($self, $data) = @_; | |||||||||
| 33 | 56 | return $data; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 19 | ▶ | sub bind { | ||||||||
|
Subroutine
bind: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 37 | 19 | my ($self, $data, $code) = @_; | |||||||||
| 38 | 19 | return $code->($data); | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 116 | ▶ | sub emit { | ||||||||
|
Subroutine
emit: called CC=10 SCAR=23.0
Pod
undocumented
| |||||||||||
| 42 | 116 | my ($self, $fixer, $label) = @_; | |||||||||
| 43 | 116 | my $perl = ""; | |||||||||
| 44 | |||||||||||
| 45 | 116 | my $var = $fixer->var; | |||||||||
| 46 | 116 | my $bind_var = $fixer->capture($self); | |||||||||
| 47 | 116 | my $unit = $fixer->generate_var; | |||||||||
| 48 | |||||||||||
| 49 | #---The subfixer is only provided for backwards compatibility | ||||||||||
| 50 | # with older Bind implementations and is deprecated | ||||||||||
| 51 | 116 | my $sub_fixer = Catmandu::Fix->new(fixes => $self->__fixes__); | |||||||||
| 52 | 116 | my $sub_fixer_var = $fixer->capture($sub_fixer); | |||||||||
| 53 | |||||||||||
| 54 | #--- | ||||||||||
| 55 | |||||||||||
| 56 | 116 | my $fix_stash = Package::Stash->new('Catmandu::Fix'); | |||||||||
| 57 | 116 | my $fix_emit_reject; | |||||||||
| 58 | my $fix_emit_fixes; | ||||||||||
| 59 | |||||||||||
| 60 | # Allow Bind-s to overwrite the default reject behavior | ||||||||||
| 61 | 116 | ▶ | if ($self->can('reject')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 12 | $fix_emit_reject = $fix_stash->get_symbol('&emit_reject'); | |||||||||
| 63 | $fix_stash->add_symbol( | ||||||||||
| 64 | 12 | ▶ | '&emit_reject' => sub {"return ${bind_var}->reject(${var});";}); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | # Allow Bind-s to bind to all fixes in if-unless-else statements | ||||||||||
| 68 | 116 | ▶ | unless ($self->does('Catmandu::Fix::Bind::Group')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 33 | $fix_emit_fixes = $fix_stash->get_symbol('&emit_fixes'); | |||||||||
| 70 | $fix_stash->add_symbol( | ||||||||||
| 71 | 24 | ▶ | '&emit_fixes' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||
| 72 | 24 | my ($this, $fixes) = @_; | |||||||||
| 73 | 24 | my $perl = ''; | |||||||||
| 74 | |||||||||||
| 75 | 24 | $perl .= "my ${unit} = ${bind_var}->unit(${var});"; | |||||||||
| 76 | |||||||||||
| 77 | 24 | ▶ | for (my $i = 0; $i < @{$fixes}; $i++) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 12 | my $fix = $fixes->[$i]; | |||||||||
| 79 | 12 | my $name = ref($fix); | |||||||||
| 80 | 12 | my $var = $this->var; | |||||||||
| 81 | 12 | my $original_code = $this->emit_fix($fix); | |||||||||
| 82 | 12 | my $generated_code | |||||||||
| 83 | = "sub { my ${var} = shift; $original_code ; ${var} }"; | ||||||||||
| 84 | 12 | $perl | |||||||||
| 85 | .= "${unit} = ${bind_var}->bind(${unit}, $generated_code, '$name',${sub_fixer_var});"; | ||||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 24 | ▶ | if ($self->can('result')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 16 | $perl .= "${unit} = ${bind_var}->result(${unit});"; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 24 | ▶ | if ($self->__return__) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 0 | $perl .= "${var} = ${unit};"; | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | 24 | $perl; | |||||||||
| 97 | } | ||||||||||
| 98 | 33 | ); | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 116 | $perl .= "my ${unit} = ${bind_var}->unit(${var});"; | |||||||||
| 102 | |||||||||||
| 103 | # If this is a Bind::Group, then all fixes are executed as one block in a bind | ||||||||||
| 104 | 116 | ▶ | if ($self->does("Catmandu::Fix::Bind::Group")) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | 83 | my $generated_code = "sub { my ${var} = shift;"; | |||||||||
| 106 | |||||||||||
| 107 | 83 | for my $fix (@{$self->__fixes__}) { | |||||||||
| 108 | 79 | my $original_code = $fixer->emit_fix($fix); | |||||||||
| 109 | 79 | $generated_code .= "$original_code ;"; | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | 83 | $generated_code .= "${var} }"; | |||||||||
| 113 | |||||||||||
| 114 | 83 | $perl | |||||||||
| 115 | .= "${unit} = ${bind_var}->bind(${unit}, $generated_code,'::group::',${sub_fixer_var});"; | ||||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | # If this isn't a Bind::Group, then bind will be executed for each seperate fix | ||||||||||
| 119 | else { | ||||||||||
| 120 | 33 | for my $fix (@{$self->__fixes__}) { | |||||||||
| 121 | 34 | my $name = ref($fix); | |||||||||
| 122 | 34 | my $original_code = $fixer->emit_fix($fix); | |||||||||
| 123 | 34 | my $generated_code | |||||||||
| 124 | = "sub { my ${var} = shift; $original_code ; ${var} }"; | ||||||||||
| 125 | |||||||||||
| 126 | 34 | $perl | |||||||||
| 127 | .= "${unit} = ${bind_var}->bind(${unit}, $generated_code,'$name',${sub_fixer_var});"; | ||||||||||
| 128 | } | ||||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | 116 | ▶ | if ($self->can('result')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 24 | $perl .= "${unit} = ${bind_var}->result(${unit});"; | |||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | 116 | ▶ | if ($self->__return__) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 0 | $perl .= "${var} = ${unit};"; | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | 116 | ▶ | $fix_stash->add_symbol('&emit_reject' => $fix_emit_reject) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | if $fix_emit_reject; | ||||||||||
| 141 | 116 | ▶ | $fix_stash->add_symbol('&emit_fixes' => $fix_emit_fixes) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | if $fix_emit_fixes; | ||||||||||
| 143 | |||||||||||
| 144 | 116 | $perl; | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | 12 | 1; | |||||||||
| 148 | |||||||||||
| 149 | __END__ | ||||||||||