annotate lib/IMPL/Transform.pm @ 418:3f38dabaf5cc ref20150831

sync
author cin
date Mon, 28 Dec 2015 15:11:35 +0300
parents c6e90e02dd17
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::Transform;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4 use parent qw(IMPL::Object);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 use IMPL::lang qw(:declare);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 BEGIN {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 public _direct property templates => PROP_ALL;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 public _direct property default => PROP_ALL;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 public _direct property plain => PROP_ALL;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 private _direct property _cache => PROP_ALL;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 my $this = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 my $args = @_ == 1 ? shift : { @_ };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 $this->{$plain} = delete $args->{-plain};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 $this->{$default} = delete $args->{-default};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 $this->{$templates} = $args;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 sub Transform {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 my ($this,$object,@args) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 if (not ref $object) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 die new IMPL::Exception("There is no the template for a plain value in the transform") unless $this->{$plain};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 my $template = $this->{$plain};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 return $this->$template($object,@args);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 my $template = $this->MatchTemplate($object) || $this->default or die new IMPL::Transform::NoTransformException(ref $object);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 return $this->ProcessTemplate($template,$object,@args);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 sub MatchTemplate {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 my ($this,$object) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 my $class = $this->GetClassForObject( $object );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 if (my $t = $this->{$_cache}->{$class} ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 return $t;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 $t = $this->{$templates}->{$class};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 return $this->{$_cache}->{$class} = $t if $t;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 no strict 'refs';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 my @isa = @{"${class}::ISA"};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 while (@isa) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 my $sclass = shift @isa;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 $t = $this->{$templates}->{$sclass};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 #cache and return
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 return $this->{$_cache}->{$class} = $t if $t;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 push @isa, @{"${sclass}::ISA"};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 ;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 sub ProcessTemplate {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 my ($this,$t,$obj,@args) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 return $this->$t($obj,@args);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 sub GetClassForObject {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 my ($this,$object) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 return ref $object;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 package IMPL::Transform::NoTransformException;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 base => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87 'IMPL::Exception' => sub { 'No transformation', @_ }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 C<IMPL::Transform> - преобразование объектной структуры
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 =head1 SYNOPSIS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103 =begin code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105 my $obj = new AnyObject;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107 my $t = new Transform (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108 SomeClass => sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 my ($this,$object) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 return new NewClass({ Name => $object->name, Document => $this->Transform($object->Data) })
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 DocClass => sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 my ($this,$object) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 return new DocPreview(Author => $object->Author, Text => $object->Data);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 -default => sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 my ($this,$object) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 return $object;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 -plain => sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 my ($this,$object) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122 return $object;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126 my $result = $t->Transform($obj);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128 =end code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132 Преобразование одного объекта к другому, например даных к их представлению.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134 =cut