annotate Lib/IMPL/Web/View/Metadata/ObjectMeta.pm @ 369:7c784144d2f1

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