annotate Lib/IMPL/Transform.pm @ 47:a9b70d836b28

Web::Application development (request controller) Security development
author Sergey
date Tue, 23 Feb 2010 22:57:16 +0300
parents 32d2350fccf9
children 16ada169ca75
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 {
38
d660fb38b7cc small fixes
Sergey
parents: 7
diff changeset
23 my ($this,$object,@args) = @_;
0
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};
38
d660fb38b7cc small fixes
Sergey
parents: 7
diff changeset
28 return $this->$template($object,@args);
0
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
38
d660fb38b7cc small fixes
Sergey
parents: 7
diff changeset
33 return $this->$template($object,@args);
0
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) = @_;
38
d660fb38b7cc small fixes
Sergey
parents: 7
diff changeset
39 my $class = $this->GetClassForObject( $object );
0
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
38
d660fb38b7cc small fixes
Sergey
parents: 7
diff changeset
46 sub GetClassForObject {
d660fb38b7cc small fixes
Sergey
parents: 7
diff changeset
47 my ($this,$object) = @_;
d660fb38b7cc small fixes
Sergey
parents: 7
diff changeset
48
d660fb38b7cc small fixes
Sergey
parents: 7
diff changeset
49 return ref $object;
d660fb38b7cc small fixes
Sergey
parents: 7
diff changeset
50 }
d660fb38b7cc small fixes
Sergey
parents: 7
diff changeset
51
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
52 package IMPL::Transform::NoTransformException;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
53 use base qw(IMPL::Exception);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
54
44
Sergey
parents: 38
diff changeset
55 our %CTOR = (
Sergey
parents: 38
diff changeset
56 'IMPL::Exception' => sub { 'No transformation', @_ }
Sergey
parents: 38
diff changeset
57 );
Sergey
parents: 38
diff changeset
58
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
59 1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
60
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
61 __END__
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
62
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
63 =pod
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
64 =head1 SYNOPSIS
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
65
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
66 my $obj = new AnyObject;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
67
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
68 my $t = new Transform (
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
69 AnyClass => sub {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
70 my ($this,$object) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
71 return new NewClass({ Name => $object->name, Document => $this->Transform($object->Data) })
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
72 },
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
73 DocClass => sub {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
74 my ($this,$object) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
75 return new DocPreview(Author => $object->Author, Text => $object->Data);
7
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
76 },
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
77 -default => sub {
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
78 my ($this,$object) = @_;
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
79 return $object;
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
80 },
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
81 -plain => sub {
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
82 my ($this,$object) = @_;
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
83 return $object;
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
84 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
85 );
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
86
7
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
87 my $result = $t->Transform($obj);
94d47b388442 Улучшены тесты
Sergey
parents: 0
diff changeset
88
38
d660fb38b7cc small fixes
Sergey
parents: 7
diff changeset
89 =head1 DESCRIPTION
d660fb38b7cc small fixes
Sergey
parents: 7
diff changeset
90
d660fb38b7cc small fixes
Sergey
parents: 7
diff changeset
91 , .
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
92
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
93 =cut