annotate lib/IMPL/Code/AccessorPropertyImplementor.pm @ 413:af8d359ee4cc ref20150831

working on di container
author cin
date Thu, 24 Sep 2015 12:19:30 +0300
parents c6e90e02dd17
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::Code::AccessorPropertyImplementor;
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 IMPL::lang qw(:hash);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use IMPL::require {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 Exception => '-IMPL::Exception',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 ArgException => '-IMPL::InvalidArgumentException',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 AccessorPropertyInfo => '-IMPL::Class::AccessorPropertyInfo'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 require IMPL::Class::AccessorPropertyInfo;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 require IMPL::Object::List;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 use parent qw(IMPL::Code::BasePropertyImplementor);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 use constant {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 CodeGetAccessor => 'return $this->get($field);',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 CodeSetAccessor => 'return $this->set($field,@_);',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 CodeSetListAccessor =>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 'my $val = IMPL::Object::List->new( (@_ == 1 and UNIVERSAL::isa($_[0], \'ARRAY\') ) ? $_[0] : [@_] );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 $this->set($field,$val);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 return( wantarray ? @{ $val } : $val );',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 CodeGetListAccessor =>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 'my $val = $this->get($field);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 $this->set($field,$val = IMPL::Object::List->new()) unless $val;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 return( wantarray ? @{ $val } : $val );'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 sub factoryParams { qw($class $name $get $set $validator $field) };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 my %cache;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 sub Implement {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 my $self = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 my $spec = {};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 map hashApply($spec,$self->NormalizeSpecification($_)), @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 my $name = $spec->{name}
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 or ArgException->new(name => "The name of the property is required");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 my $class = $spec->{class}
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 or ArgException->new(name => "The onwer class must be specified");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 my $id = $self->CreateFactoryId($spec);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 my $factory = $cache{$id};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 unless($factory) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 $factory = $self->CreateFactory($spec);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 $cache{$id} = $factory;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 my $field = $name;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 my $accessor = $factory->($class, $name, $spec->{get}, $spec->{set}, $spec->{validator}, $field);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 my $args = {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 getter => $spec->{get} ? $accessor : undef,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 setter => $spec->{set} ? $accessor : undef,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 ownetSet => $spec->{ownerSet} ? 1 : 0,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 isList => $spec->{isList} ? 1 : 0,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61 name => $spec->{name},
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 class => $spec->{class},
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 type => $spec->{type},
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 access => $spec->{access},
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 fieldName => $field
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 delete @$spec{qw(get set ownerSet isList name class type access field direct)};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 $args->{attributes} = $spec;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 my $propInfo = AccessorPropertyInfo->new($args);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 no strict 'refs';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 *{"${class}::$name"} = $accessor;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 $class->SetMeta($propInfo);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 return $propInfo;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 1;