| 1 | package Catmandu::Fix::Bind::list; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | ▶ | use Catmandu::Sane; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 1 | our $VERSION = '1.0201_02'; | |||||||||||||
| 6 | |||||||||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 1 | ▶ | use Clone (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 1 | ▶ | use Catmandu::Util; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 1 | ▶ | use namespace::clean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 1 | ▶ | use Catmandu::Fix::Has; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 1 | with 'Catmandu::Fix::Bind'; | |||||||||||||
| 14 | |||||||||||||||
| 15 | 1 | has path => (fix_opt => 1); | |||||||||||||
| 16 | 1 | has var => (fix_opt => 1); | |||||||||||||
| 17 | |||||||||||||||
| 18 | 1 | has _root_ => (is => 'rw'); | |||||||||||||
| 19 | 13 | ▶ | has flag => (is => 'rw', default => sub {0}); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | |||||||||||||||
| 21 | sub zero { | ||||||||||||||
| 22 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
zero: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 23 | 0 | []; | |||||||||||||
| 24 | } | ||||||||||||||
| 25 | |||||||||||||||
| 26 | sub unit { | ||||||||||||||
| 27 | 13 | ▶ | my ($self, $data) = @_; | ||||||||||||
|
Subroutine
unit: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 28 | |||||||||||||||
| 29 | 13 | $self->_root_($data); | |||||||||||||
| 30 | |||||||||||||||
| 31 | # Set a flag so that all the bind fixes are only run once... | ||||||||||||||
| 32 | 13 | $self->flag(0); | |||||||||||||
| 33 | |||||||||||||||
| 34 | 13 | ▶ | defined $self->path ? Catmandu::Util::data_at($self->path, $data) : $data; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 35 | } | ||||||||||||||
| 36 | |||||||||||||||
| 37 | sub bind { | ||||||||||||||
| 38 | 12 | ▶ | my ($self, $mvar, $func, $name, $fixer) = @_; | ||||||||||||
|
Subroutine
bind: called CC=10 SCAR=23.2
Pod
undocumented
| |||||||||||||||
| 39 | |||||||||||||||
| 40 | 12 | ▶ | if (Catmandu::Util::is_hash_ref($mvar)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 41 | |||||||||||||||
| 42 | # Ignore all specialized processing when not an array | ||||||||||||||
| 43 | 9 | $mvar = $func->($mvar); | |||||||||||||
| 44 | } | ||||||||||||||
| 45 | elsif (Catmandu::Util::is_array_ref($mvar)) { | ||||||||||||||
| 46 | 3 | ▶ | return $mvar if $self->flag; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 47 | |||||||||||||||
| 48 | # Run only these fixes once: no need for do identity() ... end | ||||||||||||||
| 49 | 3 | $self->flag(1); | |||||||||||||
| 50 | |||||||||||||||
| 51 | 3 | my $idx = 0; | |||||||||||||
| 52 | |||||||||||||||
| 53 | [ | ||||||||||||||
| 54 | map { | ||||||||||||||
| 55 | 3 | my $scope; | |||||||||||||
| 56 | 10 | my $has_default_context_variable = 0; | |||||||||||||
| 57 | |||||||||||||||
| 58 | # Switch context to the variable set by the user | ||||||||||||||
| 59 | 10 | ▶ | if ($self->var) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 60 | 4 | $scope = $self->_root_; | |||||||||||||
| 61 | 4 | $scope->{$self->var} = $_; | |||||||||||||
| 62 | } | ||||||||||||||
| 63 | elsif (!ref($_)) { | ||||||||||||||
| 64 | 4 | $scope = [$_]; | |||||||||||||
| 65 | 4 | $has_default_context_variable = 1; | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | else { | ||||||||||||||
| 68 | 2 | $scope = $_; | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | # Run /all/ the fixes on the scope | ||||||||||||||
| 72 | 10 | my $res = $fixer->fix($scope); | |||||||||||||
| 73 | |||||||||||||||
| 74 | # Check for rejects() | ||||||||||||||
| 75 | 10 | ▶ | if (defined $res) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 76 | 10 | ▶ | if ($self->var) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 77 | 4 | $mvar->[$idx] = $scope->{$self->var}; | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | elsif ($has_default_context_variable) { | ||||||||||||||
| 80 | 4 | $mvar->[$idx] = $res->[0]; | |||||||||||||
| 81 | } | ||||||||||||||
| 82 | 10 | $idx++; | |||||||||||||
| 83 | } | ||||||||||||||
| 84 | else { | ||||||||||||||
| 85 | 0 | splice(@$mvar, $idx, 1); | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | |||||||||||||||
| 88 | 10 | ▶ | delete $scope->{$self->var} if $self->var; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 89 | } @$mvar | ||||||||||||||
| 90 | ]; | ||||||||||||||
| 91 | } | ||||||||||||||
| 92 | else { | ||||||||||||||
| 93 | 0 | return $self->zero; | |||||||||||||
| 94 | } | ||||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | # Flatten an array: [ [A] , [A] , [A] ] -> [ A, A, A ] | ||||||||||||||
| 98 | sub plus { | ||||||||||||||
| 99 | 11 | ▶ | my ($self, $prev, $next) = @_; | ||||||||||||
|
Subroutine
plus: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 100 | |||||||||||||||
| 101 | 11 | ▶ | Catmandu::Util::is_array_ref($next) ? [$prev, @$next] : [$prev, $next]; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 102 | } | ||||||||||||||
| 103 | |||||||||||||||
| 104 | 1 | 1; | |||||||||||||
| 105 | |||||||||||||||
| 106 | __END__ | ||||||||||||||
| 107 | |||||||||||||||
| 108 | =pod | ||||||||||||||
| 109 | |||||||||||||||
| 110 | =head1 NAME | ||||||||||||||
| 111 | |||||||||||||||
| 112 | Catmandu::Fix::Bind::list - a binder that computes Fix-es for every element in a list | ||||||||||||||
| 113 | |||||||||||||||
| 114 | =head1 SYNOPSIS | ||||||||||||||
| 115 | |||||||||||||||
| 116 | # Create an array: | ||||||||||||||
| 117 | # demo: | ||||||||||||||
| 118 | # - red | ||||||||||||||
| 119 | # - green | ||||||||||||||
| 120 | # - yellow | ||||||||||||||
| 121 | |||||||||||||||
| 122 | # Add a foo field to every item in the demo list, by default all | ||||||||||||||
| 123 | # fixes will be in context of the iterated path. If the context | ||||||||||||||
| 124 | # is a list, then '.' will be the path of the temporary context | ||||||||||||||
| 125 | # variable | ||||||||||||||
| 126 | do list(path:demo) | ||||||||||||||
| 127 | if all_equal(.,green) | ||||||||||||||
| 128 | upcase(.) | ||||||||||||||
| 129 | end | ||||||||||||||
| 130 | end | ||||||||||||||
| 131 | |||||||||||||||
| 132 | # This will result: | ||||||||||||||
| 133 | # demo: | ||||||||||||||
| 134 | # - red | ||||||||||||||
| 135 | # - GREEN | ||||||||||||||
| 136 | # - yellow | ||||||||||||||
| 137 | |||||||||||||||
| 138 | # Loop over the list but store the values in a temporary 'c' variable | ||||||||||||||
| 139 | # Use this c variable to copy the list to the root 'xyz' path | ||||||||||||||
| 140 | do list(path:demo,var:c) | ||||||||||||||
| 141 | copy_field(c,xyz.$append) | ||||||||||||||
| 142 | end | ||||||||||||||
| 143 | |||||||||||||||
| 144 | # This will result: | ||||||||||||||
| 145 | # demo: | ||||||||||||||
| 146 | # - red | ||||||||||||||
| 147 | # - GREEN | ||||||||||||||
| 148 | # - yellow | ||||||||||||||
| 149 | # xyz: | ||||||||||||||
| 150 | # - red | ||||||||||||||
| 151 | # - GREEN | ||||||||||||||
| 152 | # - yellow | ||||||||||||||
| 153 | |||||||||||||||
| 154 | =head1 DESCRIPTION | ||||||||||||||
| 155 | |||||||||||||||
| 156 | The list binder will iterate over all the elements in a list and fixes the | ||||||||||||||
| 157 | values in context of that list. | ||||||||||||||
| 158 | |||||||||||||||
| 159 | =head1 CONFIGURATION | ||||||||||||||
| 160 | |||||||||||||||
| 161 | =head2 path | ||||||||||||||
| 162 | |||||||||||||||
| 163 | The path to a list in the data. | ||||||||||||||
| 164 | |||||||||||||||
| 165 | =head2 var | ||||||||||||||
| 166 | |||||||||||||||
| 167 | The loop variable to be iterated over. When used, a magic temporary field will | ||||||||||||||
| 168 | be available in the root of the record containing the iterated data. | ||||||||||||||
| 169 | |||||||||||||||
| 170 | =head1 SEE ALSO | ||||||||||||||
| 171 | |||||||||||||||
| 172 | L<Catmandu::Fix::Bind> | ||||||||||||||
| 173 | |||||||||||||||
| 174 | =cut | ||||||||||||||