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 19 19 100.0
pod 2 2 100.0
total 100 102 98.0


line stmt bran cond sub pod time code
1 7     7   103 use 5.006;
  7         20  
2 7     7   35 use strict;
  7         9  
  7         107  
3 7     7   32 use warnings;
  7         28  
  7         178  
4              
5             =head1 NAME
6              
7             EJS::Template::JSAdapter::JE
8              
9             =cut
10              
11             package EJS::Template::JSAdapter::JE;
12 7     7   33 use base 'EJS::Template::JSAdapter';
  7         11  
  7         496  
13              
14 7     7   1586 use EJS::Template::Util qw(clean_text_ref);
  7         14  
  7         290  
15 7     7   39 use Scalar::Util qw(reftype);
  7         10  
  7         1919  
16              
17             our $ENCODE_UTF8 = 1;
18             our $SANITIZE_UTF8 = 0;
19             our $FORCE_UNTAINT = 0;
20              
21             =head1 Methods
22              
23             =head2 new
24              
25             Creates an adapter object.
26              
27             =cut
28              
29             sub new {
30 55     55 1 97 my ($class) = @_;
31 7     7   585 eval 'use JE';
  7     7   70746  
  7     7   100  
  7     7   47  
  7     5   13  
  7     5   84  
  7     4   42  
  7     1   13  
  7         81  
  7         42  
  7         13  
  7         92  
  5         80  
  5         11  
  5         50  
  5         77  
  5         10  
  5         54  
  4         27  
  4         8  
  4         85  
  55         3048  
32 55 50       153 die $@ if $@;
33 55         204 my $engine = JE->new;
34 55         121440 return bless {engine => $engine}, $class;
35             }
36              
37             =head2 bind
38              
39             Implements the bind method.
40              
41             =cut
42              
43             sub bind {
44 192     192 1 214 my ($self, $variables) = @_;
45 192         452 my $engine = $self->engine;
46            
47 192         178 my $assign_value;
48             my $assign_hash;
49 1         1 my $assign_array;
50            
51             $assign_value = sub {
52 1237     1237   8378 my ($target_ref, $source_ref) = @_;
53 1237         1967 my $reftype = reftype $$source_ref;
54            
55 1237 100       1744 if ($reftype) {
56 776 100       1788 if ($reftype eq 'HASH') {
    100          
    100          
    50          
57 138         350 $assign_hash->($$target_ref = {}, $$source_ref);
58             } elsif ($reftype eq 'ARRAY') {
59 80         203 $assign_array->($$target_ref = [], $$source_ref);
60             } elsif ($reftype eq 'CODE') {
61 556         1115 $$target_ref = $$source_ref;
62             } elsif ($reftype eq 'SCALAR') {
63 1         2 $assign_value->($target_ref, $$source_ref);
64             } else {
65             # ignore?
66             }
67             } else {
68 462         942 my $text_ref = clean_text_ref($source_ref, $ENCODE_UTF8, $SANITIZE_UTF8, $FORCE_UNTAINT);
69 461         1025 $$target_ref = $$text_ref;
70             }
71 192         737 };
72            
73             $assign_hash = sub {
74 328     329   11235 my ($target, $source) = @_;
75            
76 328         2266 for my $name (keys %$source) {
77 953         78980 $assign_value->(\$target->{$name}, \$source->{$name});
78             }
79 191         461 };
80            
81             $assign_array = sub {
82 79     80   53186 my ($target, $source) = @_;
83 79         901 my $len = scalar(@$source);
84            
85 79         229 for (my $i = 0; $i < $len; $i++) {
86 283         16409 $assign_value->(\$target->[$i], \$source->[$i]);
87             }
88 191         477 };
89            
90 191         288 $assign_hash->($engine, $variables);
91 191         24803 return $engine;
92             }
93              
94             =head1 SEE ALSO
95              
96             =over 4
97              
98             =item * L
99              
100             =item * L
101              
102             =item * L
103              
104             =back
105              
106             =cut
107              
108             1;