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