File Coverage

blib/lib/EJS/Template/JSAdapter/JE.pm
Criterion Covered Total %
statement 69 69 100.0
branch 10 12 83.3
condition n/a
subroutine 20 20 100.0
pod 2 2 100.0
total 101 103 98.0


line stmt bran cond sub pod time code
1 7     7   132 use 5.006;
  7         28  
2 7     7   60 use strict;
  7         24  
  7         163  
3 7     7   48 use warnings;
  7         18  
  7         245  
4              
5             =head1 NAME
6              
7             EJS::Template::JSAdapter::JE
8              
9             =cut
10              
11             package EJS::Template::JSAdapter::JE;
12 7     7   50 use base 'EJS::Template::JSAdapter';
  7         22  
  7         643  
13              
14 7     7   2031 use EJS::Template::Util qw(clean_text_ref);
  7         25  
  7         438  
15 7     7   76 use Scalar::Util qw(reftype);
  7         25  
  7         2846  
16              
17             our $ENCODE_UTF8 = 0;
18             our $SANITIZE_UTF8 = 0;
19             our $FORCE_UNTAINT = 0;
20             our $PRESERVE_UTF8 = 1;
21              
22             =head1 Methods
23              
24             =head2 new
25              
26             Creates an adapter object.
27              
28             =cut
29              
30             sub new {
31 56     56 1 181 my ($class) = @_;
32 7     7   1358 eval 'use JE';
  7     7   214684  
  7     7   142  
  7     7   113  
  7     5   24  
  7     5   144  
  7     4   66  
  7     1   21  
  7     1   104  
  7         62  
  7         21  
  7         106  
  5         47  
  5         17  
  5         66  
  5         50  
  5         17  
  5         65  
  4         44  
  4         15  
  4         52  
  56         3857  
33 56 50       251 die $@ if $@;
34 56         295 my $engine = JE->new;
35 56         221210 return bless {engine => $engine}, $class;
36             }
37              
38             =head2 bind
39              
40             Implements the bind method.
41              
42             =cut
43              
44             sub bind {
45 195     195 1 417 my ($self, $variables) = @_;
46 195         617 my $engine = $self->engine;
47            
48 195         509 my $assign_value;
49             my $assign_hash;
50 195         2 my $assign_array;
51            
52             $assign_value = sub {
53 1275     1275   14812 my ($target_ref, $source_ref) = @_;
54 1275         3353 my $reftype = reftype $$source_ref;
55            
56 1275 100       2579 if ($reftype) {
57 796 100       2344 if ($reftype eq 'HASH') {
    100          
    100          
    50          
58 142         504 $assign_hash->($$target_ref = {}, $$source_ref);
59             } elsif ($reftype eq 'ARRAY') {
60 83         295 $assign_array->($$target_ref = [], $$source_ref);
61             } elsif ($reftype eq 'CODE') {
62 569         1705 $$target_ref = $$source_ref;
63             } elsif ($reftype eq 'SCALAR') {
64 1         2 $assign_value->($target_ref, $$source_ref);
65             } else {
66             # ignore?
67             }
68             } else {
69 480         1407 my $text_ref = clean_text_ref($source_ref, $ENCODE_UTF8, $SANITIZE_UTF8, $FORCE_UNTAINT);
70 480         1761 $$target_ref = $$text_ref;
71             }
72 195         1115 };
73            
74             $assign_hash = sub {
75 336     336   19197 my ($target, $source) = @_;
76            
77 335         3937 for my $name (keys %$source) {
78 980         153762 $assign_value->(\$target->{$name}, \$source->{$name});
79             }
80 195         840 };
81            
82             $assign_array = sub {
83 82     83   97000 my ($target, $source) = @_;
84 82         1757 my $len = scalar(@$source);
85            
86 82         337 for (my $i = 0; $i < $len; $i++) {
87 294         23124 $assign_value->(\$target->[$i], \$source->[$i]);
88             }
89 194         834 };
90            
91 194         527 $assign_hash->($engine, $variables);
92 194         43956 return $engine;
93             }
94              
95             =head1 SEE ALSO
96              
97             =over 4
98              
99             =item * L
100              
101             =item * L
102              
103             =item * L
104              
105             =back
106              
107             =cut
108              
109             1;