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

Создан репозитарий
author Sergey
date Tue, 14 Jul 2009 12:54:37 +0400
parents
children 94d47b388442
comparison
equal deleted inserted replaced
-1:000000000000 0:03e58a454b20
1 package IMPL::Transform;
2 use base qw(IMPL::Object IMPL::Object::Autofill);
3
4 use IMPL::Class::Property;
5 use IMPL::Class::Property::Direct;
6
7 BEGIN {
8 protected _direct property Templates => prop_all;
9 protected _direct property Default => prop_all;
10 protected _direct property Plain => prop_all;
11 }
12
13 __PACKAGE__->PassThroughArgs;
14
15 sub Transform {
16 my ($this,$object) = @_;
17
18 if (not ref $object) {
19 die new IMPL::Exception("There is no the template for a plain value in the transform") unless $this->{$Plain};
20 my $template = $this->{$Plain};
21 return $this->$template($object);
22 } else {
23
24 my $template = $this->MatchTemplate($object) || $this->Default or die new IMPL::Transform::NoTransformException(ref $object);
25
26 return $this->$template($object);
27 }
28 }
29
30 sub MatchTemplate {
31 my ($this,$object) = @_;
32 my $class = ref $object;
33
34 foreach my $tClass ( keys %{$this->Templates || {}} ) {
35 return $this->Templates->{$tClass} if ($tClass eq $class);
36 }
37 }
38
39 package IMPL::Transform::NoTransformException;
40 use base qw(IMPL::Exception);
41
42 1;
43
44 __END__
45
46 =pod
47 =head1 SYNOPSIS
48
49 my $obj = new AnyObject;
50
51 my $t = new Transform (
52 AnyClass => sub {
53 my ($this,$object) = @_;
54 return new NewClass({ Name => $object->name, Document => $this->Transform($object->Data) })
55 },
56 DocClass => sub {
57 my ($this,$object) = @_;
58 return new DocPreview(Author => $object->Author, Text => $object->Data);
59 }
60 );
61
62 =head1 Summary
63 Ïðåîáðàçóåò äàííûå ñîäåðæàùèåñÿ â ôîðìå â ðåàëüíûå îáúåêòû èñïîëüçóÿ ñïåöèàëüíîå ïðåîáðàçîâàíèå.
64
65 =cut