| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | #!/usr/bin/perl | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 89171 | use 5.008001; | 
|  | 1 |  |  |  |  | 3 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | package # | 
| 6 |  |  |  |  |  |  | Template::Plugin::JSON; | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | our $AUTHORITY = 'cpan:JWRIGHT'; | 
| 9 |  |  |  |  |  |  | our $ALT = 'Moo'; | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 1 |  |  | 1 |  | 475 | use Types::Standard (); | 
|  | 1 |  |  |  |  | 62792 |  | 
|  | 1 |  |  |  |  | 38 |  | 
| 12 | 1 |  |  | 1 |  | 9 | use Carp (); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 14 |  | 
| 13 | 1 |  |  | 1 |  | 13 | use JSON (); | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 19 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 1 |  |  | 1 |  | 459 | use Alt::Template::Plugin::JSON::Moo; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 29 |  | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  |  | 
| 18 | 1 |  |  | 1 |  | 496 | use Moo; | 
|  | 1 |  |  |  |  | 8948 |  | 
|  | 1 |  |  |  |  | 4 |  | 
| 19 | 1 |  |  | 1 |  | 1655 | use namespace::clean; | 
|  | 1 |  |  |  |  | 9445 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | extends qw(Template::Plugin); | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | has context => ( | 
| 24 |  |  |  |  |  |  | isa => Types::Standard::Object, | 
| 25 |  |  |  |  |  |  | is  => "ro", | 
| 26 |  |  |  |  |  |  | weak_ref => 1, | 
| 27 |  |  |  |  |  |  | ); | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | has json_converter => ( | 
| 30 |  |  |  |  |  |  | isa => Types::Standard::Object, | 
| 31 |  |  |  |  |  |  | is  => "lazy", | 
| 32 |  |  |  |  |  |  | lazy_build => 1, | 
| 33 |  |  |  |  |  |  | ); | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | has json_args => ( | 
| 36 |  |  |  |  |  |  | isa => Types::Standard::HashRef, | 
| 37 |  |  |  |  |  |  | is  => "ro", | 
| 38 |  |  |  |  |  |  | default => sub { {} }, | 
| 39 |  |  |  |  |  |  | ); | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | sub BUILDARGS { | 
| 42 | 3 |  |  | 3 | 0 | 62942 | my ( $class, $c, @args ) = @_; | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 3 | 50 | 66 |  |  | 22 | if ( @args == 1 and not ref $args[0] ) { | 
| 46 | 0 |  |  |  |  | 0 | warn "Single argument form is deprecated, this module always uses JSON/JSON::XS now"; | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 3 | 100 |  |  |  | 15 | my $args = ref $args[0] ? $args[0] : {}; | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 3 |  |  |  |  | 54 | return { %$args, context => $c, json_args => $args }; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | sub _build_json_converter { | 
| 56 | 3 |  |  | 3 |  | 37 | my $self = shift; | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 3 |  |  |  |  | 42 | my $json = JSON->new->allow_nonref(1); | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 3 |  |  |  |  | 13 | my $args = $self->json_args; | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 3 |  |  |  |  | 18 | for my $method (keys %$args) { | 
| 63 | 1 | 50 |  |  |  | 10 | if ( $json->can($method) ) { | 
| 64 | 1 |  |  |  |  | 4 | $json->$method( $args->{$method} ); | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 3 |  |  |  |  | 47 | return $json; | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | sub json { | 
| 72 | 4 |  |  | 4 | 0 | 10 | my ( $self, $value ) = @_; | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 4 |  |  |  |  | 67 | $self->json_converter->encode($value); | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | sub json_decode { | 
| 78 | 1 |  |  | 1 | 0 | 8131 | my ( $self, $value ) = @_; | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 1 |  |  |  |  | 40 | $self->json_converter->decode($value); | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | sub BUILD { | 
| 84 | 3 |  |  | 3 | 0 | 231 | my $self = shift; | 
| 85 | 3 |  |  | 4 |  | 49 | $self->context->define_vmethod( $_ => json => sub { $self->json(@_) } ) for qw(hash list scalar); | 
|  | 4 |  |  |  |  | 282 |  | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 1 |  |  | 1 |  | 572 | no Moo; | 
|  | 1 |  |  |  |  | 7 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | __PACKAGE__; | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | __END__ |