annotate Lib/IMPL/Transform.pm @ 233:3cebcf6fdb9b

refactoring, cleaning code
author sergey
date Thu, 11 Oct 2012 04:53:08 +0400
parents 292226770180
children 2904da230022
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
1 package IMPL::Transform;
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
2 use strict;
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
3
166
4267a2ac3d46 Added Class::Template,
wizard
parents: 148
diff changeset
4 use parent qw(IMPL::Object);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
5
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
6 use IMPL::lang qw(:declare :constants);
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
7
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
8 use IMPL::Class::Property::Direct;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
9
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
10 BEGIN {
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
11 public _direct property templates => PROP_ALL;
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
12 public _direct property default => PROP_ALL;
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
13 public _direct property plain => PROP_ALL;
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
14 private _direct property _cache => PROP_ALL;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
15 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
16
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
17 sub CTOR {
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 197
diff changeset
18 my $this = shift;
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 197
diff changeset
19 my $args = @_ == 1 ? shift : { @_ };
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
20
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 197
diff changeset
21 $this->{$plain} = delete $args->{-plain};
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 197
diff changeset
22 $this->{$default} = delete $args->{-default};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
23
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 197
diff changeset
24 $this->{$templates} = $args;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
25 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
26
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
27 sub Transform {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
28 my ($this,$object,@args) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
29
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
30 if (not ref $object) {
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
31 die new IMPL::Exception("There is no the template for a plain value in the transform") unless $this->{$plain};
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
32 my $template = $this->{$plain};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
33 return $this->$template($object,@args);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
34 } else {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
35
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
36 my $template = $this->MatchTemplate($object) || $this->default or die new IMPL::Transform::NoTransformException(ref $object);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
37
199
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents: 198
diff changeset
38 return $this->ProcessTemplate($template,$object,@args);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
39 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
40 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
41
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
42 sub MatchTemplate {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
43 my ($this,$object) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
44 my $class = $this->GetClassForObject( $object );
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
45
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
46 if (my $t = $this->{$_cache}->{$class} ) {
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
47 return $t;
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
48 } else {
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
49 $t = $this->{$templates}->{$class};
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
50
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
51 return $this->{$_cache}->{$class} = $t if $t;
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
52
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
53 {
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
54 no strict 'refs';
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
55
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
56 my @isa = @{"${class}::ISA"};
212
292226770180 bugfixes
sergey
parents: 199
diff changeset
57
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
58 while (@isa) {
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
59 my $sclass = shift @isa;
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
60
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
61 $t = $this->{$templates}->{$sclass};
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
62
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 197
diff changeset
63 #cache and return
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
64 return $this->{$_cache}->{$class} = $t if $t;
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
65
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
66 push @isa, @{"${sclass}::ISA"};
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
67 }
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
68 };
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
69 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
70 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
71
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
72 sub ProcessTemplate {
199
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents: 198
diff changeset
73 my ($this,$t,$obj,@args) = @_;
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
74
199
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents: 198
diff changeset
75 return $this->$t($obj,@args);
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
76 }
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
77
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
78 sub GetClassForObject {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
79 my ($this,$object) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
80
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
81 return ref $object;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
82 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
83
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
84 package IMPL::Transform::NoTransformException;
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
85 use IMPL::declare {
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
86 base => {
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
87 'IMPL::Exception' => sub { 'No transformation', @_ }
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
88 }
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents: 180
diff changeset
89 };
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
90
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
91 1;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
92
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
93 __END__
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
94
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
95 =pod
96
4c55aed00ff2 Minor changes
wizard
parents: 49
diff changeset
96
4c55aed00ff2 Minor changes
wizard
parents: 49
diff changeset
97 =head1 NAME
4c55aed00ff2 Minor changes
wizard
parents: 49
diff changeset
98
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
99 C<IMPL::Transform> - преобразование объектной структуры
96
4c55aed00ff2 Minor changes
wizard
parents: 49
diff changeset
100
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
101 =head1 SYNOPSIS
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
102
96
4c55aed00ff2 Minor changes
wizard
parents: 49
diff changeset
103 =begin code
4c55aed00ff2 Minor changes
wizard
parents: 49
diff changeset
104
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
105 my $obj = new AnyObject;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
106
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
107 my $t = new Transform (
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents: 96
diff changeset
108 SomeClass => sub {
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
109 my ($this,$object) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
110 return new NewClass({ Name => $object->name, Document => $this->Transform($object->Data) })
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
111 },
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
112 DocClass => sub {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
113 my ($this,$object) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
114 return new DocPreview(Author => $object->Author, Text => $object->Data);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
115 },
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
116 -default => sub {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
117 my ($this,$object) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
118 return $object;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
119 },
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
120 -plain => sub {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
121 my ($this,$object) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
122 return $object;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
123 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
124 );
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
125
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
126 my $result = $t->Transform($obj);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
127
96
4c55aed00ff2 Minor changes
wizard
parents: 49
diff changeset
128 =end code
4c55aed00ff2 Minor changes
wizard
parents: 49
diff changeset
129
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
130 =head1 DESCRIPTION
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
131
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
132 Преобразование одного объекта к другому, например даных к их представлению.
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
133
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
134 =cut