| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Text::Xslate::Bridge; | 
| 2 | 2 |  |  | 2 |  | 2915 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 46 |  | 
| 3 | 2 |  |  | 2 |  | 10 | use warnings; | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 54 |  | 
| 4 | 2 |  |  | 2 |  | 10 | use Carp (); | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 36 |  | 
| 5 | 2 |  |  | 2 |  | 9 | use Text::Xslate::Util qw(p); | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 1170 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | my %storage; | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | sub bridge { | 
| 10 | 3 |  |  | 3 | 1 | 216 | my $class = shift; | 
| 11 | 3 |  |  |  |  | 20 | while(my($type, $table) = splice @_, 0, 2) { | 
| 12 | 9 |  |  |  |  | 39 | $storage{$class}{$type} = $table; | 
| 13 |  |  |  |  |  |  | } | 
| 14 | 3 |  |  |  |  | 10 | return; | 
| 15 |  |  |  |  |  |  | } | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | sub export_into_xslate { | 
| 18 | 6 |  |  | 6 | 0 | 21 | my($class, $funcs_ref, @args) = @_; | 
| 19 | 6 |  |  |  |  | 10 | push @{$funcs_ref}, $class->methods(@args); | 
|  | 6 |  |  |  |  | 30 |  | 
| 20 | 6 |  |  |  |  | 86 | return; | 
| 21 |  |  |  |  |  |  | } | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub methods { | 
| 24 | 6 |  |  | 6 | 0 | 20 | my($class, %args) = @_; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 6 | 50 |  |  |  | 23 | if(!exists $storage{$class}) { | 
| 27 | 0 |  |  |  |  | 0 | Carp::croak("$class has no methods (possibly not a bridge class)"); | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 6 | 100 |  |  |  | 26 | if(exists $args{-exclude}) { | 
| 31 | 1 |  |  |  |  | 4 | my $exclude = $args{-exclude}; | 
| 32 | 1 |  |  |  |  | 5 | my $methods = $class->_functions; | 
| 33 | 1 |  |  |  |  | 3 | my @export; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 1 | 50 |  |  |  | 6 | if(ref($exclude) eq 'ARRAY') { | 
| 36 | 1 |  |  |  |  | 3 | $exclude = { map { $_ => 1 } @{$exclude} }; | 
|  | 1 |  |  |  |  | 5 |  | 
|  | 1 |  |  |  |  | 3 |  | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 1 | 50 |  |  |  | 4 | if(ref($exclude) eq 'HASH') { | 
|  |  | 0 |  |  |  |  |  | 
| 40 | 1 |  |  |  |  | 3 | @export = grep { !$exclude->{$_} } keys %{$methods}; | 
|  | 5 |  |  |  |  | 13 |  | 
|  | 1 |  |  |  |  | 3 |  | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  | elsif(ref($exclude) eq 'Regexp'){ | 
| 43 | 0 |  |  |  |  | 0 | @export = grep { $_ !~ $exclude } keys %{$methods}; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  | else { | 
| 46 | 0 |  |  |  |  | 0 | @export = grep { $_ ne $exclude } keys %{$methods}; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 47 |  |  |  |  |  |  | } | 
| 48 | 1 |  |  |  |  | 3 | return map { $_ => $methods->{$_} } @export; | 
|  | 4 |  |  |  |  | 14 |  | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  | else { | 
| 51 | 5 |  |  |  |  | 9 | return %{ $class->_functions }; | 
|  | 5 |  |  |  |  | 25 |  | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | sub _functions { | 
| 56 | 6 |  |  | 6 |  | 10 | my($class) = @_; | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 6 |  | 50 |  |  | 22 | my $st    = $storage{$class} ||= {}; | 
| 59 | 6 |  | 100 |  |  | 30 | my $funcs = $st->{_funcs}    ||= {}; | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | # for methods | 
| 62 | 6 |  |  |  |  | 13 | foreach my $type (qw(scalar hash array)) { | 
| 63 | 18 |  | 100 |  |  | 56 | my $table = $st->{$type} || next; | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 16 |  |  |  |  | 22 | foreach my $name(keys %{$table}) { | 
|  | 16 |  |  |  |  | 39 |  | 
| 66 | 26 |  |  |  |  | 115 | $funcs->{$type . '::' . $name} = $table->{$name}; | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | # for functions | 
| 71 | 6 |  |  |  |  | 13 | my $table = $st->{function}; | 
| 72 | 6 |  |  |  |  | 10 | foreach my $name(keys %{$table}) { | 
|  | 6 |  |  |  |  | 17 |  | 
| 73 | 11 |  |  |  |  | 21 | $funcs->{$name} = $table->{$name}; | 
| 74 |  |  |  |  |  |  | } | 
| 75 | 6 |  |  |  |  | 40 | return $funcs; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | sub dump { | 
| 79 | 0 |  |  | 0 | 0 |  | p(\%storage); | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | 1; | 
| 83 |  |  |  |  |  |  | __END__ |