Mercurial > pub > Impl
annotate Lib/IMPL/Class/Meta.pm @ 229:47f77e6409f7
heavily reworked the resource model of the web application:
*some ResourcesContraact functionality moved to Resource
+Added CustomResource
*Corrected action handlers
author | sergey |
---|---|
date | Sat, 29 Sep 2012 02:34:47 +0400 |
parents | a8db61d0ed33 |
children | 6d8092d8ce1b |
rev | line source |
---|---|
49 | 1 package IMPL::Class::Meta; |
2 use strict; | |
3 | |
209 | 4 use Carp qw(carp); |
173 | 5 use IMPL::clone qw(clone); |
90 | 6 |
49 | 7 my %class_meta; |
90 | 8 my %class_data; |
49 | 9 |
209 | 10 sub SetMeta { |
49 | 11 my ($class,$meta_data) = @_; |
12 $class = ref $class if ref $class; | |
13 | |
180 | 14 # тут нельзя использовать стандартное исключение, поскольку для него используется |
15 # класс IMPL::Object::Accessor, который наследуется от текущего класса | |
49 | 16 die "The meta_data parameter should be an object" if not ref $meta_data; |
17 | |
18 push @{$class_meta{$class}{ref $meta_data}},$meta_data; | |
19 } | |
20 | |
209 | 21 sub set_meta { |
22 goto &SetMeta; | |
23 } | |
24 | |
25 sub GetMeta { | |
49 | 26 my ($class,$meta_class,$predicate,$deep) = @_; |
27 $class = ref $class if ref $class; | |
28 no strict 'refs'; | |
29 my @result; | |
30 | |
31 if ($deep) { | |
32 @result = map { $_->can('get_meta') ? $_->get_meta($meta_class,$predicate,$deep) : () } @{$class.'::ISA'}; | |
33 } | |
34 | |
35 if ($predicate) { | |
36 push @result,grep( &$predicate($_), map( @{$class_meta{$class}{$_}}, grep( $_->isa($meta_class), keys %{$class_meta{$class} || {}} ) ) ); | |
37 } else { | |
38 push @result, map( @{$class_meta{$class}{$_} || []}, grep( $_->isa($meta_class), keys %{$class_meta{$class} || {}} ) ); | |
39 } | |
40 wantarray ? @result : \@result; | |
41 } | |
42 | |
209 | 43 sub get_meta { |
44 goto &GetMeta; | |
45 } | |
46 | |
90 | 47 sub class_data { |
194 | 48 my $class = shift; |
49 $class = ref $class || $class; | |
50 | |
209 | 51 carp 'The method is obsolete, use static_accessor($name,$value,\'clone\') instead'; |
52 | |
194 | 53 if (@_ > 1) { |
54 my ($name,$value) = @_; | |
55 return $class_data{$class}{$name} = $value; | |
56 } else { | |
57 my ($name) = @_; | |
58 | |
59 if( exists $class_data{$class}{$name} ) { | |
60 $class_data{$class}{$name}; | |
61 } else { | |
62 if ( my $value = $class->_find_class_data($name) ) { | |
63 $class_data{$class}{$name} = clone($value); | |
64 } else { | |
65 undef; | |
66 } | |
67 } | |
68 } | |
90 | 69 } |
70 | |
163 | 71 sub static_accessor { |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
72 my ($class,$name,$value,$inherit) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
73 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
74 $inherit ||= 'inherit'; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
75 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
76 my $method = "static_accessor_$inherit"; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
77 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
78 return $class->$method($name,$value); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
79 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
80 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
81 sub static_accessor_clone { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
82 my ($class,$name,$value) = @_; |
194 | 83 $class = ref $class || $class; |
84 | |
85 no strict 'refs'; | |
86 | |
87 *{"${class}::${name}"} = sub { | |
209 | 88 my $self = shift; |
89 | |
90 if (@_ > 0) { | |
91 $self = ref $self || $self; | |
194 | 92 |
93 if ($class ne $self) { | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
94 $self->static_accessor_clone( $name => $_[0] ); # define own class data |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
95 } else { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
96 $value = $_[0]; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
97 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
98 } else { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
99 $self->static_accessor_clone($name => clone($value)); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
100 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
101 }; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
102 $value |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
103 }; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
104 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
105 sub static_accessor_inherit { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
106 my ($class,$name,$value) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
107 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
108 no strict 'refs'; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
109 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
110 *{"${class}::$name"} = sub { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
111 my $self = shift; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
112 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
113 if (@_ > 0) { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
114 $self = ref $self || $self; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
115 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
116 if ($class ne $self) { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
117 $self->static_accessor_inherit( $name => $_[0] ); # define own class data |
194 | 118 } else { |
119 $value = $_[0]; | |
120 } | |
121 } else { | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
122 $value ; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
123 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
124 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
125 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
126 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
127 sub static_accessor_own { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
128 my ($class,$name,$value) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
129 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
130 no strict 'refs'; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
131 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
132 *{"${class}::$name"} = sub { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
133 my $self = shift; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
134 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
135 if ($class ne $self) { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
136 if (@_ > 0) { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
137 $self->static_accessor_own( $name => $_[0] ); # define own class data |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
138 } else { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
139 return; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
140 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
141 } else { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
142 if ( @_ > 0 ) { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
143 $value = $_[0]; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
144 } else { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
145 return $value; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
146 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
147 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
148 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
149 } |
163 | 150 |
90 | 151 sub _find_class_data { |
194 | 152 my ($class,$name) = @_; |
153 | |
154 no strict 'refs'; | |
155 | |
156 exists $class_data{$_}{$name} and return $class_data{$_}{$name} foreach @{"${class}::ISA"}; | |
157 | |
158 my $val; | |
159 $val = $_->can('_find_class_data') ? $_->_find_class_data($name) : undef and return $val foreach @{"${class}::ISA"}; | |
90 | 160 } |
49 | 161 |
162 1; | |
90 | 163 |
164 __END__ | |
165 | |
166 =pod | |
167 | |
168 =head1 NAME | |
169 | |
180 | 170 C<IMPL::Class::Meta> - информация хранимая на уровне класса. |
90 | 171 |
172 =head1 SYNOPSIS | |
173 | |
174 =begin code | |
175 | |
176 package InfoMeta; | |
177 | |
165 | 178 use parent qw(IMPL::Object IMPL::Object::Autofill); |
90 | 179 use IMPL::Class::Property; |
180 | |
181 __PACKAGE__->PassThroughArgs; | |
182 | |
183 BEGIN { | |
194 | 184 public property name => prop_get | owner_set; |
90 | 185 } |
186 | |
187 package InfoExMeta; | |
165 | 188 use parent qw(InfoMeta); |
90 | 189 |
190 __PACKAGE__->PassThroughArgs; | |
191 | |
192 BEGIN { | |
194 | 193 public property description => prop_all; |
90 | 194 } |
195 | |
196 package Foo; | |
197 | |
198 __PACKAGE__->set_meta(new InfoMeta(name => 'info')); | |
199 __PACKAGE__->set_meta(new InfoExMeta(name => 'infoEx', description => 'extended info' )); | |
200 | |
201 package main; | |
202 | |
203 # get all InfoMeta, InfoExMeta will be included, becouse it's derived from InfoMeta | |
204 my @info = Foo->get_meta('InfoMeta'); # will get two objects, 'info' and 'infoEx'; | |
205 | |
206 # get all InfoExMeta meta | |
207 @info = Foo->get_meta('InfoExMeta'); # will get only 'infoEx' | |
208 | |
209 # get filtered meta | |
210 @info = Foo->get_meta('InfoMeta', sub { $_->name eq 'info'} ); # will get only 'info' | |
211 | |
212 =end code | |
213 | |
214 =head1 DESCRIPTION | |
215 | |
180 | 216 Позвоялет расширять информацию о типах (класса) при помощи метаданных, метаданными являются любые объекты, |
217 притом выборка метаданных приоизводится по их типу (классу), что позволяет выбрать все однотипные метаданные. | |
90 | 218 |
180 | 219 Существует возможность выборки метаданных с учетом унаследованных от базовых классов |
90 | 220 |
221 =head1 MEMBERS | |
222 | |
209 | 223 =head2 C<set_meta($meta_data)> |
90 | 224 |
180 | 225 Добавляет метаданные C<$meta_data> к классу. |
90 | 226 |
209 | 227 =head2 C<get_meta($meta_class,$predicate,$deep)> |
90 | 228 |
180 | 229 Выбирает метаданные типа C<$meta_class> и его наследников, с возможностью фильтрации и получения |
230 метаданных базовых классов. | |
90 | 231 |
232 =over | |
233 | |
234 =item C<$meta_class> | |
235 | |
180 | 236 Тип метаданных |
90 | 237 |
238 =item C<$predicate> | |
239 | |
180 | 240 Подпрограмма, которая будет вызываться для каждых найденных метаданных и на основе результата |
241 ее выполнения метаданные будут включены в результат или нет. Получеат в качестве параметра | |
242 объект с метаданными, возвращает C<true> - включить метаданные в результа, C<false> - пропустить | |
243 метаданные как не подходящие. Также переменная C<$_> ссылается на текущий объект с метаданными. | |
90 | 244 |
245 =begin code | |
246 | |
247 my @info = Foo->get_meta( | |
194 | 248 'InfoMeta', |
249 sub { ref $_ eq 'InfoMeta'}, # exclude subclasses ('InfoExMeta') | |
250 1 # deep search | |
90 | 251 ); |
252 | |
253 my @info = Foo->get_meta( | |
194 | 254 'InfoMeta', |
255 sub { | |
256 my $item = shift; | |
257 ref $item eq 'InfoMeta' # exclude subclasses ('InfoExMeta') | |
258 }, | |
259 1 # deep search | |
90 | 260 ); |
261 | |
262 =end code | |
263 | |
264 =item C<$deep> | |
265 | |
180 | 266 Осуществлять поиск по базовым классам. |
90 | 267 |
268 =back | |
269 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
270 =head2 C<static_accessor($name[,$value[,$inherit]])> |
163 | 271 |
180 | 272 Создает статическое свойство с именем C<$name> и начальным значением C<$value>. |
163 | 273 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
274 Параметр C<$inherit> контролирует то, как наследуются значения. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
275 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
276 =over |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
277 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
278 =item * C<inherit> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
279 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
280 По умолчанию. Означает, что если для класса не определено значение, оно будет |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
281 получено от родителя. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
282 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
283 =item * C<clone> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
284 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
285 Если для класса не определено значение, то оно будет клонировано из |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
286 родительского значения при первом обращении. Полезно, когда родитель задает |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
287 значение по-умолчанию, которое разделяется между несколькими потомками, |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
288 которые модифицирю само значение (например значением является ссылка на хеш, |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
289 а потомки добавляют или меняют значения в этом хеше). |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
290 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
291 =item * C<own> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
292 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
293 Каждый класс имеет свое собственное значение не зависящее от того, что было |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
294 у предка. Начальное значение для этого статического свойства C<undef>. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
295 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
296 =back |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
297 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
298 Данный метод является заглушкой, он передает управление |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
299 C<static_accessor_inherit>, C<static_accessor_clone>, C<static_accessor_own> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
300 соответственно. Эти методы можно вызывать явно |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
301 C<static_accessor_*($name[,$value])>. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
302 |
163 | 303 |
304 =begin code | |
305 | |
306 package Foo; | |
165 | 307 use parent qw(IMPL::Class::Meta); |
163 | 308 |
309 __PACKAGE__->static_accessor( info => { version => 1 } ); | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
310 __PACKAGE__->static_accessor( mappings => { toString => \&ToString }, 'clone' ); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
311 __PACKAGE__->static_accessor( _instance => undef, 'own' ); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
312 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
313 sub ToString { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
314 "[object Foo]"; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
315 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
316 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
317 sub default { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
318 my ($self) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
319 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
320 $self = ref $self || $self; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
321 return $self->_instance ? $self->_instance : $self->_instance($self->new()); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
322 } |
163 | 323 |
324 package Bar; | |
165 | 325 use parent qw(Foo); |
163 | 326 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
327 __PACKAGE__->info({language => 'English', version => 2}); # will define own 'info' but will loose original data. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
328 __PACKAGE__->mappings->{sayHello} = \&SayHello; # will not affect Foo->mappings; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
329 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
330 package main; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
331 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
332 my $foo = Foo->default; # will be a Foo object |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
333 my $bar = Bar->default; # will be a Bar object |
163 | 334 |
335 =end code | |
90 | 336 |
337 =cut |