Mercurial > pub > Impl
diff Lib/IMPL/Class/Meta.pm @ 0:03e58a454b20
Создан репозитарий
author | Sergey |
---|---|
date | Tue, 14 Jul 2009 12:54:37 +0400 |
parents | |
children | 16ada169ca75 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lib/IMPL/Class/Meta.pm Tue Jul 14 12:54:37 2009 +0400 @@ -0,0 +1,43 @@ +package IMPL::Class::Meta; +use strict; + +my %class_meta; + +sub set_meta { + my ($class,$meta_data) = @_; + $class = ref $class if ref $class; + + # òóò íåëüçÿ èñïîëüçîâàòü ñòàíäàðòíîå èñêëþ÷åíèå, ïîñêîëüêó äëÿ íåãî èñïîëüçóåòñÿ + # êëàññ IMPL::Object::Accessor, êîòîðûé íàñëåäóåòñÿ îò òåêóùåãî êëàññà + die "The meta_data parameter should be an object" if not ref $meta_data; + + push @{$class_meta{$class}{ref $meta_data}},$meta_data; +} + +sub get_meta { + my ($class,$meta_class,$predicate,$deep) = @_; + $class = ref $class if ref $class; + no strict 'refs'; + my @result; + + if ($deep) { + @result = map { $_->can('get_meta') ? $_->get_meta($meta_class,$predicate,$deep) : () } @{$class.'::ISA'}; + } + + if ($predicate) { + push @result,grep( &$predicate($_), map( @{$class_meta{$class}{$_}}, grep( $_->isa($meta_class), keys %{$class_meta{$class} || {}} ) ) ); + } else { + push @result, map( @{$class_meta{$class}{$_} || []}, grep( $_->isa($meta_class), keys %{$class_meta{$class} || {}} ) ); + } + wantarray ? @result : \@result; +} + +=pod +__PACKAGE_->set_meta($metaObject); +__PACKAGE_->get_meta('MyMetaClass',sub { + my ($item) = @_; + $item->Name eq 'Something' ? 1 : 0 +} ); +=cut + +1; \ No newline at end of file