Mercurial > pub > Impl
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 |
| 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 | 2 use strict; |
| 3 | |
|
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
4 use IMPL::lang; |
| 369 | 5 use IMPL::Const qw(:prop); |
| 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 => [ | |
|
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 | 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 | |
|
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 | 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 ($this->modelType->GetMeta(PropertyInfo, sub { not($seen{$_}++) }, 1)) { | |
| 53 my $pv = $pi->getter->($this->model); | |
| 54 my $pt; | |
| 55 | |
| 56 my %args = (name => $pi->name); | |
| 57 if ($pi->isList) { | |
| 58 $pt = 'ARRAY'; | |
| 59 $args{isMultiple} = 1; | |
| 60 $args{holdingType} = $pi->type; | |
| 61 } else { | |
| 62 $pt = $pi->type; | |
| 63 } | |
| 64 | |
| 65 push @props, Meta->new($pv, $pt, \%args); | |
| 66 } | |
|
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
67 } elsif ( $modelType && $modelType eq 'HASH' ) { |
| 369 | 68 while ( my ($k,$v) = each %{$this->model || {}} ) { |
| 69 push @props, Meta->new($v,undef,{name => $k}); | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 return \@props; | |
| 74 } | |
| 75 | |
| 76 sub GetItems { | |
| 77 my ($this) = @_; | |
| 78 | |
| 79 die OpException->new("The operation must be performed on the container") | |
| 80 unless $this->isMultiple; | |
| 81 | |
| 82 my $i = 0; | |
| 83 | |
| 84 return [ | |
| 85 map $this->_GetItemMeta($_,$i++), @{$this->model || []} | |
| 86 ]; | |
| 87 } | |
| 88 | |
| 89 sub GetItem { | |
| 90 my ($this,$index) = @_; | |
| 91 | |
| 92 die OpException->new("The operation must be performed on the container") | |
| 93 unless $this->isMultiple; | |
| 94 | |
| 95 my $item = @{$this->model || []}[$index]; | |
| 96 | |
| 97 return $this->_GetItemMeta($item,$index); | |
| 98 } | |
| 99 | |
| 100 sub _GetItemMeta { | |
| 101 my ($this,$item,$index) = @_; | |
| 102 | |
| 103 return Meta->new( | |
| 104 $item, | |
| 105 $this->holdingType, | |
| 106 { | |
| 107 name => $index, | |
| 108 container => $this | |
| 109 } | |
| 110 ); | |
| 111 } | |
| 112 | |
| 113 1; | |
| 114 | |
| 115 __END__ |
