view Lib/IMPL/Object/Serializable.pm @ 144:b56ebc31bf18

Empty nodes no more created while transforming a post request to the DOM document minor speed improvements to the object CTOR caching Added support for a secure processing (and 'laundering' ) a CGI parameters Many minor fixes
author wizard
date Tue, 13 Jul 2010 02:05:38 +0400
parents 16ada169ca75
children c8fe3f84feba
line wrap: on
line source

package IMPL::Object::Serializable;
use strict;
use warnings;

require IMPL::Exception;
use IMPL::Class::Property;

sub restore {
    my ($class,$data,$refSurrogate) = @_;
    
    if ($refSurrogate) {
        $refSurrogate->callCTOR(@$data);
        return $refSurrogate;
    } else {
        return $class->new(@$data);
    }
}

sub save {
    my ($this,$ctx,$predicate) = @_;
    
    ($this->_get_save_method)->($this,$ctx);
}

sub _get_save_method {
    my ($class) = @_;
    
    $class = ref $class || $class;
    
    no strict 'refs';
    if (my $method = *{"${class}::_impl_auto_save"}{CODE}) {
        return $method;
    } else {
        my $code = <<SAVE_METHOD;
package $class;
sub _impl_auto_save {
    my (\$this,\$ctx) = \@_;
SAVE_METHOD
    
        $code .=
        join "\n", map "\t".'$ctx->AddVar('.$_->Name.' => ' .
            ((not ref $_->Mutators and $_->Mutators & prop_list) ? ('[$this->'.$_->Class.'::'.$_->Name.'()]') : ('$this->'.$_->Class.'::'.$_->Name.'()')) .
        ') if defined ' . '$this->'.$_->Class.'::'.$_->Name.'()' . ';', grep $_->canGet, $class->get_meta('IMPL::Class::PropertyInfo',undef,1);
        $code .= <<SAVE_METHOD;

}
\\\&_impl_auto_save;
SAVE_METHOD

        return (eval $code || die new IMPL::Exception("Failed to generate serialization method",$class,$@));
    }
}

1;