view Lib/IMPL/Transform.pm @ 0:03e58a454b20

Создан репозитарий
author Sergey
date Tue, 14 Jul 2009 12:54:37 +0400
parents
children 94d47b388442
line wrap: on
line source

package IMPL::Transform;
use base qw(IMPL::Object IMPL::Object::Autofill);

use IMPL::Class::Property;
use IMPL::Class::Property::Direct;

BEGIN {
    protected _direct property Templates => prop_all;
    protected _direct property Default => prop_all;
    protected _direct property Plain => prop_all;
}

__PACKAGE__->PassThroughArgs;

sub Transform {
    my ($this,$object) = @_;
    
    if (not ref $object) {
        die new IMPL::Exception("There is no the template for a plain value in the transform") unless $this->{$Plain};
        my $template = $this->{$Plain};
        return $this->$template($object);
    } else {
    
        my $template = $this->MatchTemplate($object) || $this->Default or die new IMPL::Transform::NoTransformException(ref $object);
    
        return $this->$template($object);
    }
}

sub MatchTemplate {
    my ($this,$object) = @_;
    my $class = ref $object;
    
    foreach my $tClass ( keys %{$this->Templates || {}} ) {
        return $this->Templates->{$tClass} if ($tClass eq $class);
    }
}

package IMPL::Transform::NoTransformException;
use base qw(IMPL::Exception);

1;

__END__

=pod
=head1 SYNOPSIS

my $obj = new AnyObject;

my $t = new Transform (
    AnyClass => sub {
        my ($this,$object) = @_;
        return new NewClass({ Name => $object->name, Document => $this->Transform($object->Data) })
    },
    DocClass => sub {
        my ($this,$object) = @_;
        return new DocPreview(Author => $object->Author, Text => $object->Data);
    }
);

=head1 Summary
Ïðåîáðàçóåò äàííûå ñîäåðæàùèåñÿ â ôîðìå â ðåàëüíûå îáúåêòû èñïîëüçóÿ ñïåöèàëüíîå ïðåîáðàçîâàíèå.

=cut