| 1 | package Catmandu::Fix::SimpleGetValue; | ||
| 2 | |||
| 3 | 35 | ▶ | use Catmandu::Sane; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 35 | our $VERSION = '1.0201_02'; | |
| 6 | |||
| 7 | 35 | ▶ | use Moo::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 35 | ▶ | use namespace::clean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 35 | with 'Catmandu::Fix::Base'; | |
| 11 | |||
| 12 | 35 | requires 'path'; | |
| 13 | 35 | requires 'emit_value'; | |
| 14 | |||
| 15 | sub emit { | ||
| 16 | 92 | ▶ | my ($self, $fixer) = @_; |
|
Subroutine
emit: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 17 | 92 | my $path = $fixer->split_path($self->path); | |
| 18 | 92 | my $key = pop @$path; | |
| 19 | |||
| 20 | $fixer->emit_walk_path( | ||
| 21 | $fixer->var, | ||
| 22 | $path, | ||
| 23 | sub { | ||
| 24 | 92 | ▶ | my $var = shift; |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 25 | $fixer->emit_get_key( | ||
| 26 | $var, $key, | ||
| 27 | sub { | ||
| 28 | 92 | ▶ | my $var = shift; |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 29 | 92 | $self->emit_value($var, $fixer); | |
| 30 | } | ||
| 31 | 92 | ); | |
| 32 | } | ||
| 33 | 92 | ); | |
| 34 | } | ||
| 35 | |||
| 36 | 35 | 1; | |
| 37 | |||
| 38 | __END__ | ||
| 39 | |||
| 40 | =pod | ||
| 41 | |||
| 42 | =head1 NAME | ||
| 43 | |||
| 44 | Catmandu::Fix::SimpleGetValue - helper class for creating emit Fix-es | ||
| 45 | |||
| 46 | =head1 SYNOPSIS | ||
| 47 | |||
| 48 | # Create a Rot13 encrypter | ||
| 49 | |||
| 50 | package Catmandu::Fix::rot13; | ||
| 51 | |||
| 52 | use Catmandu::Sane; | ||
| 53 | use Moo; | ||
| 54 | use Catmandu::Fix::Has; | ||
| 55 | |||
| 56 | has path => (fix_arg => 1); | ||
| 57 | |||
| 58 | with 'Catmandu::Fix::SimpleGetValue'; | ||
| 59 | |||
| 60 | sub emit_value { | ||
| 61 | my ($self, $var, $fixer) = @_; | ||
| 62 | "${var} =~ y/A-Za-z/N-ZA-Mn-za-m/ if is_string(${var});"; | ||
| 63 | } | ||
| 64 | |||
| 65 | # Now you can use this Fix in your scripts | ||
| 66 | rot13(my.deep.nested.path) | ||
| 67 | rot13(authors.*) | ||
| 68 | |||
| 69 | =head1 DESCRIPTION | ||
| 70 | |||
| 71 | Catmandu::Fix::SimpleGetValue eases the creation of emit Fixes that transform | ||
| 72 | values on a JSON path. A Fix package implementing Catmandu::Fix::SimpleGetValue | ||
| 73 | needs to implement a method C<emit_value> which accepts the variable name on | ||
| 74 | which the Fix operates and an instance of Catmandu::Fixer. The method should | ||
| 75 | return a string containing the Perl code to transform values on a JSON path. | ||
| 76 | |||
| 77 | It is not possible to inspect in an emit Fix the actual value on which this Fix | ||
| 78 | runs: $var contains a variable name not the actual value. The real values are | ||
| 79 | only available at run time. Emit Fixes are used to compile Perl code into Fix | ||
| 80 | modules which do the actual transformation. | ||
| 81 | |||
| 82 | For more examples look at the source code of: | ||
| 83 | |||
| 84 | =over 4 | ||
| 85 | |||
| 86 | =item Catmandu::Fix::append; | ||
| 87 | |||
| 88 | =item Catmandu::Fix::replace_all | ||
| 89 | |||
| 90 | =item Catmandu::Fix::upcase | ||
| 91 | |||
| 92 | =back | ||
| 93 | |||
| 94 | =head1 SEE ALSO | ||
| 95 | |||
| 96 | L<Catmandu::Fix> | ||
| 97 | |||
| 98 | =cut | ||