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