| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Template::Pure::DataProxy; | ||
| 5 | |||
| 6 | use Scalar::Util; | ||
| 7 | use Data::Dumper; | ||
| 8 | |||
| 9 | sub new { | ||
| 10 | my ($proto, $data, %extra) = @_; | ||
| 11 | my $class = ref($proto) || $proto; | ||
| 12 | bless +{ | ||
| 13 | data => $data, | ||
| 14 | extra => \%extra, | ||
| 15 | }, $class; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub can { 1 } # Evil, sorry | ||
| 19 | |||
| 20 | sub AUTOLOAD { | ||
| 21 | return if our $AUTOLOAD =~ /DESTROY/; | ||
| 22 | my $self = shift; | ||
| 23 | ( my $method = $AUTOLOAD ) =~ s{.*::}{}; | ||
| 24 | if(Scalar::Util::blessed $self->{data}) { | ||
| 25 | if($self->{data}->can($method)) { | ||
| 26 | return $self->{data}->$method; | ||
| 27 | } elsif(exists $self->{extra}{$method}) { | ||
| 28 | return $self->{extra}{$method}; | ||
| 29 | } else { | ||
| 30 | die "No value at $method for $self"; | ||
| 31 | } | ||
| 32 | } else { | ||
| 33 | ## I think we can assume its a hashref then. | ||
| 34 | if(exists $self->{data}{$method}) { | ||
| 35 | return $self->{data}{$method}; | ||
| 36 | } elsif(exists $self->{extra}{$method}) { | ||
| 37 | return $self->{extra}{$method}; | ||
| 38 | } else { | ||
| 39 | die "No value at $method in: ".Dumper $self; | ||
| 40 | } | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | 1; | ||
| 45 |