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   114 use 5.006;
  7         18  
2 7     7   33 use strict;
  7         14  
  7         105  
3 7     7   29 use warnings;
  7         12  
  7         166  
4              
5             =head1 NAME
6              
7             EJS::Template::JSAdapter::JE
8              
9             =cut
10              
11             package EJS::Template::JSAdapter::JE;
12 7     7   31 use base 'EJS::Template::JSAdapter';
  7         13  
  7         504  
13              
14 7     7   1673 use EJS::Template::Util qw(clean_text_ref);
  7         17  
  7         293  
15 7     7   38 use Scalar::Util qw(reftype);
  7         13  
  7         2005  
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 83 my ($class) = @_;
32 7     7   633 eval 'use JE';
  7     7   73752  
  7     7   106  
  7     7   43  
  7     5   11  
  7     5   74  
  7     4   43  
  7     1   12  
  7     1   74  
  7         41  
  7         11  
  7         91  
  5         72  
  5         10  
  5         55  
  5         30  
  5         10  
  5         96  
  4         63  
  4         8  
  4         37  
  56         2793  
33 56 50       142 die $@ if $@;
34 56         180 my $engine = JE->new;
35 56         117580 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 202 my ($self, $variables) = @_;
46 195         432 my $engine = $self->engine;
47            
48 195         177 my $assign_value;
49             my $assign_hash;
50 1         1 my $assign_array;
51            
52             $assign_value = sub {
53 1275     1275   7862 my ($target_ref, $source_ref) = @_;
54 1275         2334 my $reftype = reftype $$source_ref;
55            
56 1275 100       1593 if ($reftype) {
57 796 100       1706 if ($reftype eq 'HASH') {
    100          
    100          
    50          
58 142         318 $assign_hash->($$target_ref = {}, $$source_ref);
59             } elsif ($reftype eq 'ARRAY') {
60 83         196 $assign_array->($$target_ref = [], $$source_ref);
61             } elsif ($reftype eq 'CODE') {
62 569         1133 $$target_ref = $$source_ref;
63             } elsif ($reftype eq 'SCALAR') {
64 1         1 $assign_value->($target_ref, $$source_ref);
65             } else {
66             # ignore?
67             }
68             } else {
69 480         905 my $text_ref = clean_text_ref($source_ref, $ENCODE_UTF8, $SANITIZE_UTF8, $FORCE_UNTAINT);
70 480         1276 $$target_ref = $$text_ref;
71             }
72 195         750 };
73            
74             $assign_hash = sub {
75 336     336   10814 my ($target, $source) = @_;
76            
77 335         2176 for my $name (keys %$source) {
78 980         82315 $assign_value->(\$target->{$name}, \$source->{$name});
79             }
80 195         450 };
81            
82             $assign_array = sub {
83 82     83   49052 my ($target, $source) = @_;
84 82         859 my $len = scalar(@$source);
85            
86 82         226 for (my $i = 0; $i < $len; $i++) {
87 294         12547 $assign_value->(\$target->[$i], \$source->[$i]);
88             }
89 194         424 };
90            
91 194         295 $assign_hash->($engine, $variables);
92 194         24535 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;