annotate lib/IMPL/Code/DirectPropertyImplementor.pm @ 422:b0481c071bea ref20150831

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