annotate lib/IMPL/Class/Meta.pm @ 417:3ed0c58e9da3 ref20150831

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