view Lib/Form/Filter.pm @ 59:0f3e369553bd

Rewritten property implementation (probably become slower but more flexible) Configuration infrastructure in progress (in the aspect of the lazy activation) Initial concept for the code generator
author wizard
date Tue, 09 Mar 2010 02:50:45 +0300
parents 16ada169ca75
children
line wrap: on
line source

package Form::Filter;
use strict;
use Common;
our @ISA = qw(Object);

use constant {
    CTX_SINGLE      => 1,   # значение поля
    CTX_SET         => 2,   # множество значений
    CTX_EXISTENT    => 4    # только существующие значения
};

BEGIN {
    DeclareProperty Name => ACCESS_READ;
    DeclareProperty Message => ACCESS_READ;
}

sub CTOR {
    my ($this,$name,$message) = @_;
    $this->{$Name} = $name or die new Exception('A filter name is required');
    $this->{$Message} = $message;
}

sub FormatMessage {
    my ($this,$object) = @_;
    
    (my $message = $object->Attributes->{$this->{$Name}} || $this->{$Message} || ($Common::Debug ? "$this->{$Name}: %name%" : '')) =~ s{%(\w+(?:\.\w+)*)%}{
        my $value = $object->Attributes->{$1} || ($Common::Debug ? $object->Name.'.'.$1 : '');
    }ge;
    
    return $message;
}

package Form::FilterResult;
use Common;
our @ISA = qw(Object);

use constant {
    STATE_ERROR => 0,           # ошибочное значение
    STATE_SUCCESS => 1,         # значение корректное, можно продолжать выполнение
    STATE_SUCCESS_STOP => 2,    # значение корректное, выполнение остальных фильтров не требуется
    STATE_SUCCESS_STAY => 3     # значение корректное, выполнение данного фильтра более не требуется
};

BEGIN {
    DeclareProperty State => ACCESS_READ;
    DeclareProperty Message => ACCESS_READ;
    DeclareProperty Target => ACCESS_READ;
    DeclareProperty Container => ACCESS_READ;
}

sub CTOR {
    my $this = shift;
    $this->SUPER::CTOR(@_);
    
    UNIVERSAL::isa($this->{$Target},'Form::Item') or UNIVERSAL::isa($this->{$Container},'Form::Container') or die new Exception("Invalid Target or Container property") if $this->{$State} == STATE_ERROR;
}

sub Item {
    my $this = shift;
    
    return ref $this->{$Target} ?
        ($this->{$Target}->isa('Form::Item') ? $this->{$Target} : $this->{$Container}->Item( $this->{$Target}->isMulti ? $this->{$Target}->Name . '0' : $this->{$Target}->Name ) )
        :
        ($this->{$Target});
}

1;