annotate lib/IMPL/Web/View/Metadata/ObjectMeta.pm @ 417:3ed0c58e9da3 ref20150831

working on di container, tests
author cin
date Mon, 02 Nov 2015 01:56:53 +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::Web::View::Metadata::ObjectMeta;
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;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use IMPL::Const qw(:prop :access);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 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 OpException => '-IMPL::InvalidOperationException',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 PropertyInfo => 'IMPL::Class::PropertyInfo',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 AbstractObject => '-IMPL::Object::Abstract'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 base => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 'IMPL::Web::View::Metadata::BaseMeta' => sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 my ($model,$type,$args) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 $type ||= typeof($model);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 return ($model,$type,$args);
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 props => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 isMultiple => PROP_RO,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 holdingType => PROP_RO
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 use constant {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 Meta => __PACKAGE__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 my ($this,$model,$type,$args) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 $type = $this->modelType;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 $args->{isMultiple} ||= $type && $type eq 'ARRAY';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 if ($args) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 $this->$_($args->{$_}) foreach grep $args->{$_}, qw(isMultiple holdingType);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 sub PopulateProperties {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 my %seen;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 my @props;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 my $modelType = $this->modelType;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 if ( isclass($modelType,AbstractObject) ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 foreach my $pi (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 $this->modelType->GetMeta(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 PropertyInfo,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 sub { not($seen{$_}++) and $_->access == ACCESS_PUBLIC },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 1
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 my $pv = $this->model && $pi->getter->($this->model);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 my $pt;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 my %args = (name => $pi->name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 if ($pi->isList) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 $pt = 'ARRAY';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 $args{isMultiple} = 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 $args{holdingType} = $pi->type;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 $pt = $pi->type;
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 push @props, Meta->new($pv, $pt, \%args);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 } elsif ( $modelType && $modelType eq 'HASH' ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 while ( my ($k,$v) = each %{$this->model || {}} ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 push @props, Meta->new($v,undef,{name => $k});
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
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 return \@props;
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 GetItems {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85 die OpException->new("The operation must be performed on the container")
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 unless $this->isMultiple;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 my $i = 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 return [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91 map $this->_GetItemMeta($_,$i++), @{$this->model || []}
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 ];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 sub GetItem {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 my ($this,$index) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 die OpException->new("The operation must be performed on the container")
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 unless $this->isMultiple;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 my $item = @{$this->model || []}[$index];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103 return $this->_GetItemMeta($item,$index);
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 sub _GetItemMeta {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107 my ($this,$item,$index) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 return Meta->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 $item,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 $this->holdingType,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 name => $index,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 container => $this
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 sub GetMetadataForModel {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 my ($self,$model,$args) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122 $args ||= {};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 return $self->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125 $model,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126 delete $args->{modelType},
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 $args
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128 )
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 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 __END__