| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  |  | 
| 2 |  |  |  |  |  |  | use Catmandu::Sane; | 
| 3 | 114 |  |  | 114 |  | 140995 |  | 
|  | 114 |  |  |  |  | 311 |  | 
|  | 114 |  |  |  |  | 675 |  | 
| 4 |  |  |  |  |  |  | our $VERSION = '1.2018'; | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | use Clone qw(clone); | 
| 7 | 114 |  |  | 114 |  | 770 | use Moo::Role; | 
|  | 114 |  |  |  |  | 220 |  | 
|  | 114 |  |  |  |  | 4724 |  | 
| 8 | 114 |  |  | 114 |  | 648 | use namespace::clean; | 
|  | 114 |  |  |  |  | 227 |  | 
|  | 114 |  |  |  |  | 672 |  | 
| 9 | 114 |  |  | 114 |  | 38539 |  | 
|  | 114 |  |  |  |  | 244 |  | 
|  | 114 |  |  |  |  | 752 |  | 
| 10 |  |  |  |  |  |  | requires 'fix'; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | my $target = caller; | 
| 13 |  |  |  |  |  |  | my ($pkg, %opts) = @_; | 
| 14 | 99 |  |  | 99 |  | 5386 |  | 
| 15 | 99 |  |  |  |  | 1578 | if (my $sym = $opts{as}) { | 
| 16 |  |  |  |  |  |  | $opts{cache} //= 1; | 
| 17 | 99 | 100 |  |  |  | 4510 |  | 
| 18 | 2 |  | 50 |  |  | 55 | my $sub = sub { | 
| 19 |  |  |  |  |  |  | my $data = shift; | 
| 20 |  |  |  |  |  |  | my $fixer; | 
| 21 | 2 |  |  | 2 |  | 4826 |  | 
| 22 | 2 |  |  |  |  | 5 | state $cache = {}; | 
| 23 |  |  |  |  |  |  | if ($opts{cache}) { | 
| 24 | 2 |  |  |  |  | 3 | my $key = join('--', @_); | 
| 25 | 2 | 50 |  |  |  | 8 | $fixer = $cache->{$key} ||= $pkg->new(@_); | 
| 26 | 2 |  |  |  |  | 6 | } | 
| 27 | 2 |  | 33 |  |  | 39 |  | 
| 28 |  |  |  |  |  |  | $fixer ||= $pkg->new(@_); | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 2 |  | 33 |  |  | 39 | if ($opts{clone}) { | 
| 31 |  |  |  |  |  |  | $data = clone($data); | 
| 32 | 2 | 50 |  |  |  | 30 | } | 
| 33 | 0 |  |  |  |  | 0 |  | 
| 34 |  |  |  |  |  |  | $fixer->fix($data); | 
| 35 |  |  |  |  |  |  | }; | 
| 36 | 2 |  |  |  |  | 9 | no strict 'refs'; | 
| 37 | 2 |  |  |  |  | 12 | *{"${target}::$sym"} = $sub; | 
| 38 | 114 |  |  | 114 |  | 56849 | } | 
|  | 114 |  |  |  |  | 228 |  | 
|  | 114 |  |  |  |  | 10267 |  | 
| 39 | 2 |  |  |  |  | 5 | } | 
|  | 2 |  |  |  |  | 14 |  | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | 1; | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | =pod | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | =head1 NAME | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | Catmandu::Fix::Inlineable - Role for all Catmandu fixes that can be inlined | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | package Catmandu::Fix::my_fix1; | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | use Catmandu::Sane; | 
| 55 |  |  |  |  |  |  | use Moo; | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | with 'Catmandu::Fix::Inlineable'; | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | sub fix { | 
| 60 |  |  |  |  |  |  | my ($self, $data) = @_; | 
| 61 |  |  |  |  |  |  | ....FIXER GENERATING CODE.... | 
| 62 |  |  |  |  |  |  | $data | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | package Catmandu::Fix::my_fix2; | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | use Catmandu::Sane; | 
| 68 |  |  |  |  |  |  | use Moo; | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | # Catmandu::Fix::Base automatically is Inlineable | 
| 71 |  |  |  |  |  |  | with 'Catmandu::Fix::Base'; | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | sub emit { | 
| 74 |  |  |  |  |  |  | my ($self, $fixer) = @_; | 
| 75 |  |  |  |  |  |  | ....FIXER GENERATING CODE.... | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | package main; | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | use Catmandu::Fix::my_fix1 as => 'my_fix1'; | 
| 81 |  |  |  |  |  |  | # disabling caching may be desirable with fixes that have side effects like | 
| 82 |  |  |  |  |  |  | # writing to a file, the downside is that a new instance of the fix will be | 
| 83 |  |  |  |  |  |  | # created with each invocation | 
| 84 |  |  |  |  |  |  | use Catmandu::Fix::my_fix2 as => 'my_fix2', cache => 0; | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | my $data = {}; | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | $data = my_fix1($data); | 
| 89 |  |  |  |  |  |  | $data = my_fix2($data); | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | For more information how to create fixes read the following two blog posts: | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | http://librecat.org/catmandu/2014/03/14/create-a-fixer.html | 
| 96 |  |  |  |  |  |  | http://librecat.org/catmandu/2014/03/26/creating-a-fixer-2.html | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | =cut |