annotate Lib/IMPL/Transform.pm @ 15:16795016e70b

Dom schema in progress
author Sergey
date Mon, 07 Sep 2009 17:30:55 +0400
parents 94d47b388442
children d660fb38b7cc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
1 package IMPL::Transform;
7
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
2 use base qw(IMPL::Object);
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
3
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
4 use IMPL::Class::Property;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
5 use IMPL::Class::Property::Direct;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
6
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
7 BEGIN {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
8 protected _direct property Templates => prop_all;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
9 protected _direct property Default => prop_all;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
10 protected _direct property Plain => prop_all;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
11 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
12
7
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
13 sub CTOR {
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
14 my ($this,%args) = @_;
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
15
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
16 $this->{$Plain} = delete $args{-plain};
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
17 $this->{$Default} = delete $args{-default};
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
18
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
19 $this->{$Templates} = \%args;
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
20 }
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
21
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
22 sub Transform {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
23 my ($this,$object) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
24
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
25 if (not ref $object) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
26 die new IMPL::Exception("There is no the template for a plain value in the transform") unless $this->{$Plain};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
27 my $template = $this->{$Plain};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
28 return $this->$template($object);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
29 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
30
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
31 my $template = $this->MatchTemplate($object) || $this->Default or die new IMPL::Transform::NoTransformException(ref $object);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
32
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
33 return $this->$template($object);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
34 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
35 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
36
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
37 sub MatchTemplate {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
38 my ($this,$object) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
39 my $class = ref $object;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
40
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
41 foreach my $tClass ( keys %{$this->Templates || {}} ) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
42 return $this->Templates->{$tClass} if ($tClass eq $class);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
43 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
44 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
45
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
46 package IMPL::Transform::NoTransformException;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
47 use base qw(IMPL::Exception);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
48
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
49 1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
50
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
51 __END__
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
52
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
53 =pod
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
54 =head1 SYNOPSIS
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
55
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
56 my $obj = new AnyObject;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
57
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
58 my $t = new Transform (
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
59 AnyClass => sub {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
60 my ($this,$object) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
61 return new NewClass({ Name => $object->name, Document => $this->Transform($object->Data) })
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
62 },
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
63 DocClass => sub {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
64 my ($this,$object) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
65 return new DocPreview(Author => $object->Author, Text => $object->Data);
7
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
66 },
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
67 -default => sub {
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
68 my ($this,$object) = @_;
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
69 return $object;
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
70 },
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
71 -plain => sub {
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
72 my ($this,$object) = @_;
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
73 return $object;
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
74 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
75 );
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
76
7
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
77 my $result = $t->Transform($obj);
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
78
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
79 =head1 Summary
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
80 .
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
81
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
82 =cut