comparison lib/IMPL/Web/View/Metadata/ObjectMeta.pm @ 407:c6e90e02dd17 ref20150831

renamed Lib->lib
author cin
date Fri, 04 Sep 2015 19:40:23 +0300
parents
children
comparison
equal deleted inserted replaced
406:f23fcb19d3c1 407:c6e90e02dd17
1 package IMPL::Web::View::Metadata::ObjectMeta;
2 use strict;
3
4 use IMPL::lang;
5 use IMPL::Const qw(:prop :access);
6 use IMPL::declare {
7 require => {
8 Exception => 'IMPL::Exception',
9 ArgException => '-IMPL::InvalidArgumentException',
10 OpException => '-IMPL::InvalidOperationException',
11 PropertyInfo => 'IMPL::Class::PropertyInfo',
12 AbstractObject => '-IMPL::Object::Abstract'
13 },
14 base => [
15 'IMPL::Web::View::Metadata::BaseMeta' => sub {
16 my ($model,$type,$args) = @_;
17 $type ||= typeof($model);
18 return ($model,$type,$args);
19 }
20 ],
21 props => [
22 isMultiple => PROP_RO,
23 holdingType => PROP_RO
24 ]
25 };
26
27 use constant {
28 Meta => __PACKAGE__
29 };
30
31 sub CTOR {
32 my ($this,$model,$type,$args) = @_;
33
34 $type = $this->modelType;
35
36 $args->{isMultiple} ||= $type && $type eq 'ARRAY';
37
38 if ($args) {
39 $this->$_($args->{$_}) foreach grep $args->{$_}, qw(isMultiple holdingType);
40 }
41 }
42
43 sub PopulateProperties {
44 my ($this) = @_;
45
46 my %seen;
47 my @props;
48
49 my $modelType = $this->modelType;
50
51 if ( isclass($modelType,AbstractObject) ) {
52 foreach my $pi (
53 $this->modelType->GetMeta(
54 PropertyInfo,
55 sub { not($seen{$_}++) and $_->access == ACCESS_PUBLIC },
56 1
57 )
58 ) {
59 my $pv = $this->model && $pi->getter->($this->model);
60 my $pt;
61
62 my %args = (name => $pi->name);
63 if ($pi->isList) {
64 $pt = 'ARRAY';
65 $args{isMultiple} = 1;
66 $args{holdingType} = $pi->type;
67 } else {
68 $pt = $pi->type;
69 }
70
71 push @props, Meta->new($pv, $pt, \%args);
72 }
73 } elsif ( $modelType && $modelType eq 'HASH' ) {
74 while ( my ($k,$v) = each %{$this->model || {}} ) {
75 push @props, Meta->new($v,undef,{name => $k});
76 }
77 }
78
79 return \@props;
80 }
81
82 sub GetItems {
83 my ($this) = @_;
84
85 die OpException->new("The operation must be performed on the container")
86 unless $this->isMultiple;
87
88 my $i = 0;
89
90 return [
91 map $this->_GetItemMeta($_,$i++), @{$this->model || []}
92 ];
93 }
94
95 sub GetItem {
96 my ($this,$index) = @_;
97
98 die OpException->new("The operation must be performed on the container")
99 unless $this->isMultiple;
100
101 my $item = @{$this->model || []}[$index];
102
103 return $this->_GetItemMeta($item,$index);
104 }
105
106 sub _GetItemMeta {
107 my ($this,$item,$index) = @_;
108
109 return Meta->new(
110 $item,
111 $this->holdingType,
112 {
113 name => $index,
114 container => $this
115 }
116 );
117 }
118
119 sub GetMetadataForModel {
120 my ($self,$model,$args) = @_;
121
122 $args ||= {};
123
124 return $self->new(
125 $model,
126 delete $args->{modelType},
127 $args
128 )
129 }
130
131 1;
132
133 __END__