annotate lib/IMPL/declare.pm @ 421:7798345304bc ref20150831

working on IMPL::Config, removed old stuff
author cin
date Sun, 16 Jul 2017 22:59:39 +0300
parents 3ed0c58e9da3
children b0481c071bea
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::declare;
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);
417
3ed0c58e9da3 working on di container, tests
cin
parents: 411
diff changeset
5 use IMPL::lang qw( :base );
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 use IMPL::Class::PropertyInfo();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 use IMPL::Const qw(:access);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 use IMPL::require();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 BEGIN {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 *_require = *IMPL::require::_require;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 *_trace = *IMPL::require::_trace;
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 sub import {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 my ( $self, $args ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 return unless $args;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19
417
3ed0c58e9da3 working on di container, tests
cin
parents: 411
diff changeset
20 die "A hash reference is required" unless ishash($args);
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 no strict 'refs';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 no warnings 'once';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 my $caller = caller;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 my $aliases = $args->{require} || {};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 $IMPL::require::PENDING{$caller} = 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 _trace("declare $caller");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 $IMPL::require::level++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 *{"${caller}::SELF"} = sub () {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 $caller;
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 while ( my ( $alias, $class ) = each %$aliases ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 _trace("$alias => $class");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 $IMPL::require::level++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 my $c = _require($class);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41
411
cin
parents: 407
diff changeset
42 *{"${caller}::$alias"} = sub () {
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 $c;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 $IMPL::require::level--;
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 my $base = $args->{base} || {};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 my %ctor;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 my @isa;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52
417
3ed0c58e9da3 working on di container, tests
cin
parents: 411
diff changeset
53 if ( isarray($base) ) {
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 carp "Odd elements number in require"
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 unless scalar(@$base) % 2 == 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 while ( my ( $class, $mapper ) = splice @$base, 0, 2 ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 _trace("parent $class");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 $IMPL::require::level++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 $class = $aliases->{$class} || _require($class);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 $IMPL::require::level--;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 push @isa, $class;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 $ctor{$class} = $mapper;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 }
417
3ed0c58e9da3 working on di container, tests
cin
parents: 411
diff changeset
66 elsif ( ishash($base) ) {
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 while ( my ( $class, $mapper ) = each %$base ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 _trace("parent $class");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 $IMPL::require::level++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 $class = $aliases->{$class} || _require($class);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 $IMPL::require::level--;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 push @isa, $class;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 $ctor{$class} = $mapper;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 }
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 %{"${caller}::CTOR"} = %ctor;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 push @{"${caller}::ISA"}, @isa;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80
417
3ed0c58e9da3 working on di container, tests
cin
parents: 411
diff changeset
81 if ( isarray( $args->{meta} ) ) {
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 $caller->SetMeta($_) foreach @{ $args->{meta} };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85 my $props = $args->{props} || [];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86
417
3ed0c58e9da3 working on di container, tests
cin
parents: 411
diff changeset
87 if ( ishash($props) ) {
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 $props = [%$props];
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 die "A hash or an array reference is required in the properties list"
417
3ed0c58e9da3 working on di container, tests
cin
parents: 411
diff changeset
92 unless isarray($props);
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 carp "Odd elements number in properties declaration of $caller"
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 unless scalar(@$props) % 2 == 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 if (@$props) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 $self->_implementProps( $props, $caller );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 if ( $args->{_implement} ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 $self->_implementProps( $caller->abstractProps, $caller );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103 $caller->abstractProps( [] );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106 $IMPL::require::level--;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107 delete $IMPL::require::PENDING{$caller};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 sub _implementProps {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 my ( $self, $props, $caller ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 for ( my $i = 0 ; $i < @$props - 1 ; $i = $i + 2 ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 my ( $prop, $spec ) = @{$props}[ $i, $i + 1 ];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 $caller->ClassPropertyImplementor->Implement(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 $spec,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 name => $prop,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 class => $caller,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 access => $prop =~ /^_/
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122 ? ACCESS_PRIVATE
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 : ACCESS_PUBLIC
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 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137 C<IMPL::declare> - описывает класс
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139 =head1 SYNOPSIS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141 =begin code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143 package My::Bar;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
145 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
146 require => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
147 TFoo => 'My::Foo',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
148 TBox => 'My::Box'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
149 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
150 base => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
151 TFoo => '@_',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
152 'IMPL::Object' => undef,
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
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
156 sub CreateBox {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
157 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
158 return TBox->new($this);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
159 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
160
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
161 =end code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
162
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
163 Специальная ситрока C<@_> означает передачу параметров конструктора текущего класса конструктору
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
164 базового класса без изменений.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
165
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
166 =head1 DESCRIPTION
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 в которой храняться метаданные для объявления класса.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
170
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
171 =head1 METADATA
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
172
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
173 =head2 C<require>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
174
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
175 Содержит ссылку на хеш с синонимами модулей, которые будут доступны в текушем модуле,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
176 аналогично использованию C<IMPL::require>. Однако, если модуль не требует загрузки при
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
177 помощи C<require> нужно использовать префикс C<'-'> в его имени
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
178
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
179 =begin code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
180
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
181 {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
182 require => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
183 TObject => 'IMPL::Object', # will be loaded with require
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
184 TFoo => '-My:App::Data::Foo' # will not use 'require' to load module
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
185 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
186 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
187
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
188 =end code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
189
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
190 =head2 C<base>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
191
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
192 Обисывает базове классы для текущего класса. Если данный параметр - ссылка массив, то
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
193 этот массив будет превращен в массив C<@ISA>. Если данный параметр - ссылка на хеш, то
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
194 его ключи опичавют список базовых классов, а значения - преобразование параметров для
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
195 вызова базовых конструкторов.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
196
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
197 В качестве имен базовых классов могут быть как полные имена модулей, так и назначенные
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
198 ранее псевдонимы. Использование префикса C<'-'> перед B<полным именем модуля> означает,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
199 что модуль не требуется загружать, в случае с псевдонимами, префикс C<'-'> уже был указан
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
200 при их объявлении.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
201
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
202 =begin code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
203
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
204 {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
205 require => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
206 TFoo => '-My:App::Data::Foo' # will not use 'require' to load module
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
207 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
208 base => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
209 TFoo => '@_', # pass parameters unchanged
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
210 'My::Base::Class' => sub { name => $_[0], data => $_[1] }, # remap parameters
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
211 '-My::Extentions' => undef, # do not pass any parameters
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
212 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
213 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
214
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
215 =end code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
216
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
217 =cut