annotate Lib/IMPL/Web/View/Metadata/ObjectMeta.pm @ 370:cbf4febf0930

ObjectMeta, Tests, migrating to the new metadata model.
author sergey
date Tue, 10 Dec 2013 03:02:01 +0400
parents 7c784144d2f1
children d5c8b955bf8d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
370
cbf4febf0930 ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents: 369
diff changeset
1 package IMPL::Web::View::Metadata::ObjectMeta;
369
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
2 use strict;
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
3
370
cbf4febf0930 ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents: 369
diff changeset
4 use IMPL::lang;
369
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
5 use IMPL::Const qw(:prop);
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
6 use IMPL::declare {
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
7 require => {
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
8 Exception => 'IMPL::Exception',
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
9 ArgException => '-IMPL::InvalidArgumentException',
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
10 OpException => '-IMPL::InvalidOperationException',
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
11 PropertyInfo => 'IMPL::Class::PropertyInfo',
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
12 AbstractObject => '-IMPL::Object::Abstract'
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
13 },
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
14 base => [
370
cbf4febf0930 ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents: 369
diff changeset
15 'IMPL::Web::View::Metadata::BaseMeta' => sub {
cbf4febf0930 ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents: 369
diff changeset
16 my ($model,$type,$args) = @_;
cbf4febf0930 ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents: 369
diff changeset
17 $type ||= typeof($model);
cbf4febf0930 ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents: 369
diff changeset
18 return ($model,$type,$args);
cbf4febf0930 ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents: 369
diff changeset
19 }
369
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
20 ],
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
21 props => [
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
22 isMultiple => PROP_RO,
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
23 holdingType => PROP_RO
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
24 ]
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
25 };
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
26
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
27 use constant {
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
28 Meta => __PACKAGE__
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
29 };
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
30
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
31 sub CTOR {
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
32 my ($this,$model,$type,$args) = @_;
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
33
370
cbf4febf0930 ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents: 369
diff changeset
34 $type = $this->modelType;
cbf4febf0930 ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents: 369
diff changeset
35
cbf4febf0930 ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents: 369
diff changeset
36 $args->{isMultiple} ||= $type && $type eq 'ARRAY';
369
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
37
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
38 if ($args) {
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
39 $this->$_($args->{$_}) foreach grep $args->{$_}, qw(isMultiple holdingType);
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
40 }
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
41 }
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
42
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
43 sub PopulateProperties {
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
44 my ($this) = @_;
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
45
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
46 my %seen;
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
47 my @props;
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
48
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
49 my $modelType = $this->modelType;
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
50
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
51 if ( isclass($modelType,AbstractObject) ) {
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
52 foreach my $pi ($this->modelType->GetMeta(PropertyInfo, sub { not($seen{$_}++) }, 1)) {
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
53 my $pv = $pi->getter->($this->model);
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
54 my $pt;
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
55
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
56 my %args = (name => $pi->name);
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
57 if ($pi->isList) {
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
58 $pt = 'ARRAY';
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
59 $args{isMultiple} = 1;
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
60 $args{holdingType} = $pi->type;
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
61 } else {
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
62 $pt = $pi->type;
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
63 }
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
64
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
65 push @props, Meta->new($pv, $pt, \%args);
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
66 }
370
cbf4febf0930 ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents: 369
diff changeset
67 } elsif ( $modelType && $modelType eq 'HASH' ) {
369
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
68 while ( my ($k,$v) = each %{$this->model || {}} ) {
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
69 push @props, Meta->new($v,undef,{name => $k});
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
70 }
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
71 }
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
72
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
73 return \@props;
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
74 }
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
75
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
76 sub GetItems {
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
77 my ($this) = @_;
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
78
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
79 die OpException->new("The operation must be performed on the container")
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
80 unless $this->isMultiple;
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
81
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
82 my $i = 0;
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
83
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
84 return [
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
85 map $this->_GetItemMeta($_,$i++), @{$this->model || []}
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
86 ];
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
87 }
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
88
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
89 sub GetItem {
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
90 my ($this,$index) = @_;
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
91
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
92 die OpException->new("The operation must be performed on the container")
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
93 unless $this->isMultiple;
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
94
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
95 my $item = @{$this->model || []}[$index];
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
96
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
97 return $this->_GetItemMeta($item,$index);
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
98 }
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
99
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
100 sub _GetItemMeta {
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
101 my ($this,$item,$index) = @_;
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
102
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
103 return Meta->new(
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
104 $item,
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
105 $this->holdingType,
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
106 {
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
107 name => $index,
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
108 container => $this
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
109 }
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
110 );
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
111 }
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
112
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
113 1;
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
114
7c784144d2f1 Implemented object metadata class, cleanup
cin
parents:
diff changeset
115 __END__