annotate Lib/IMPL/template.pm @ 167:1f7a6d762394

SQL schema in progress
author sourcer
date Thu, 12 May 2011 08:57:19 +0400
parents 76515373dac0
children d1676be8afcc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
165
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
1 package IMPL::template;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
2 use strict;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
3 use warnings;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
4
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
5 use IMPL::Class::Template();
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
6
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
7 sub import {
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
8 shift;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
9 my %args = @_;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
10
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
11 my $class = caller;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
12
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
13 my @paramNames = grep m/\w+/, @{$args{parameters} || []};
165
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
14 my $declare = $args{declare};
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
15 my @isa = (@{$args{base} || []}, $class);
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
16 my %instances;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
17
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
18 no strict 'refs';
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
19
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
20 push @{"${class}::ISA"}, 'IMPL::Class::Template';
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
21
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
22 *{"${class}::$_"} = sub { die IMPL::InvalidOperationException("A template parameter isn't available here") }
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
23 foreach @paramNames;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
24
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
25 *{"${class}::spec"} = sub {
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
26 my ($self,@params) = @_;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
27
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
28 my $specClass = $self->makeName(@params);
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
29
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
30 return $specClass if $instances{$specClass};
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
31
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
32 $instances{$specClass} = 1;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
33
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
34 for (my $i=0; $i < @paramNames; $i++) {
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
35 my $param = $params[$i];
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
36 *{"${specClass}::$paramNames[$i]"} = sub { $param };
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
37 }
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
38
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
39 @{"${specClass}::ISA"} = @isa;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
40
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
41 &$declare($specClass) if $declare;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
42 return $specClass;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
43 };
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
44 }
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
45
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
46 1;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
47
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
48 __END__
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
49
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
50 =pod
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
51
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
52 =head1 NAME
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
53
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
54 C<IMPL::template> директива для объявления шаблона.
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
55
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
56 =head1 SYNPOSIS
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
57
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
58 =begin code
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
59
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
60 package KeyValuePair;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
61
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
62 use IMPL::Class::Property;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
63
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
64 use IMPL::template (
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
65 parameters => [qw(TKey TValue))],
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
66 base => [qw(IMPL::Object IMPL::Object::Autofill)],
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
67 declare => sub {
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
68 my ($class) = @_;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
69 public $class->CreateProperty(key => prop_get | owner_set, { type => $class->TKey } );
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
70 public $class->CreateProperty(value => prop_all, { type => $class->TValue} );
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
71
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
72 $class->PassThroughArgs;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
73 }
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
74 );
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
75
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
76 BEGIN {
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
77 public property id => prop_get | owner_set, { type => 'integer'};
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
78 }
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
79
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
80 __PACKAGE__->PassThroughArgs;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
81
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
82 package MyCollection;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
83
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
84 use IMPL::Class::Property;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
85
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
86 use IMPL::lang;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
87 use IMPL::template(
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
88 parameters => [qw(TKey TValue)],
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
89 base => [qw(IMPL::Object)],
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
90 declare => sub {
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
91 my ($class) = @_;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
92 my $item_t = spec KeyValuePair($class->TKey,$class->TValue);
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
93
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
94 public $class->CreateProperty(items => prop_get | prop_list, { type => $item_t } );
165
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
95
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
96 $class->static_accessor( ItemType => $item_t );
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
97 }
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
98 )
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
99
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
100 sub Add {
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
101 my ($this,$key,$value) = @_;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
102
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
103 die new IMPL::ArgumentException( key => "Invalid argument type" ) unless is $key, $this->TKey;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
104 die new IMPL::ArgumentException( value => "Invalid argument type" ) unless is $value, $this->TValue;
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
105
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
106 $this->items->AddLast( $this->ItemType->new( key => $key, value => $value ) );
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
107 }
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
108
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
109 =end code
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
110
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
111 =head1 DESCRIPTION
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
112
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
113 Шаблоны используются для динамической генерации классов. Процесс создания класса
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
114 по шаблону называется специализацией, при этом создается новый класс:
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
115
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
116 =over
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
117
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
118 =item 1
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
119
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
120 Обявляется новый пакет с именем, вычисленным из имени и параметров шаблона
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
121
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
122 =item 2
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
123
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
124 Формируется массив C<@ISA> для созаднного класса, в который добавляется имя шаблона
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
125
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
126 =item 3
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
127
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
128 Формируются методы с именами параметров шаблона, возвращающие реальные значения параметров
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
129
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
130 =item 4
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
131
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
132 Вызывается метод для конструирования специализиции
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
133
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
134 =back
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
135
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
136 =head1 MEMBERS
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
137
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
138 =over
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
139
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
140 =item C<spec(@params)>
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
141
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
142 Метод, создающий специализацию шаблона. Может быть вызван как оператор.
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
143
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
144 =back
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
145
76515373dac0 Added Class::Template,
wizard
parents:
diff changeset
146 =cut