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   38 use strict;
  6         12  
  6         173  
3 6     6   28 use warnings;
  6         12  
  6         154  
4 6     6   25 use Moose;
  6         13  
  6         36  
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   33510 use RapidApp::Util qw(:all);
  6         15  
  6         2003  
11 6     6   40 use RapidApp::JSON::MixedEncoder;
  6         25  
  6         1174  
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   11 my $self = shift;
20 6         22 return RapidApp::JSON::MixedEncoder->new;
21             }
22              
23             sub TO_JSON {
24 13     13 0 18 my $self = shift;
25 13 100       323 return $self->func if ($self->raw);
26 7         144 return $self->func . '(' . $self->json->encode($self->parm) . ')';
27             }
28              
29             sub TO_JSON_RAW {
30 13     13 0 38 return (shift)->TO_JSON;
31             }
32              
33              
34 6     6   38 no Moose;
  6         12  
  6         26  
35             __PACKAGE__->meta->make_immutable;
36             1;