File Coverage

blib/lib/RapidApp/JSONFunc.pm
Criterion Covered Total %
statement 24 24 100.0
branch 2 2 100.0
condition n/a
subroutine 9 9 100.0
pod 0 2 0.0
total 35 37 94.5


line stmt bran cond sub pod time code
1             package RapidApp::JSONFunc;
2 6     6   37 use strict;
  6         13  
  6         196  
3 6     6   32 use warnings;
  6         10  
  6         145  
4 6     6   29 use Moose;
  6         7  
  6         34  
5              
6             # This object allows returning functions within JSON. To prevent the function from being
7             # quoted (i.e. turned into a string), this object must be encoded with RapidApp::JSON::MixedEncoder
8             # which extends JSON::PP and modifies the behavior to return TO_JSON_RAW as-is
9              
10 6     6   33018 use RapidApp::Util qw(:all);
  6         13  
  6         1915  
11 6     6   42 use RapidApp::JSON::MixedEncoder;
  6         16  
  6         1172  
12              
13             has 'func' => ( is => 'ro', required => 1, isa => 'Str' );
14             has 'parm' => ( is => 'ro', required => 0 );
15             has 'raw' => ( is => 'ro', default => 0 );
16              
17             has 'json' => ( is => 'ro', lazy_build => 1 );
18             sub _build_json {
19 6     6   12 my $self = shift;
20 6         26 return RapidApp::JSON::MixedEncoder->new;
21             }
22              
23             sub TO_JSON {
24 13     13 0 20 my $self = shift;
25 13 100       342 return $self->func if ($self->raw);
26 7         151 return $self->func . '(' . $self->json->encode($self->parm) . ')';
27             }
28              
29             sub TO_JSON_RAW {
30 13     13 0 45 return (shift)->TO_JSON;
31             }
32              
33              
34 6     6   40 no Moose;
  6         15  
  6         28  
35             __PACKAGE__->meta->make_immutable;
36             1;