view Lib/IMPL/Object/List.pm @ 148:e6447ad85cb4

DOM objects now have a schema and schemaSource properties RegExp now can launder data Improved post to DOM transformation (multiple values a now supported) Added new axes to navigation queries: ancestor and descendant minor changes and bug fixes
author wizard
date Mon, 16 Aug 2010 08:26:44 +0400
parents 76b878ad6596
children e36ffd8c29db
line wrap: on
line source

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

use base qw(IMPL::Object::ArrayObject);
use IMPL::Exception;

sub as_list {
    return wantarray ? @{$_[0]} : $_[0];
}

sub CTOR {
    my ($this,$data) = @_;
    
    if ($data) {
        die new IMPL::InvalidArgumentException("The parameter should be a reference to an array") unless UNIVERSAL::isa($data,"ARRAY");
        @$this = @$data;
    }
}

sub Append {
    push @{$_[0]}, @_[1 .. $#_];
}

sub RemoveLast {
    return pop @{$_[0]};
}

sub AddFirst {
    return unshift @{$_[0]}, $_[1];
}

sub RemoveFirst {
    return shift @{$_[0]};
}

sub Count {
    return scalar @{$_[0]};
}

sub InsertAt {
    my ($this,$index,@val) = @_;
    
    splice @$this,($index||0),0,@val;
}

sub RemoveAt {
    my ($this,$index,$count) = @_;
    
    $count ||= 1;
    
    return splice @$this,$index,$count;
}

sub RemoveItem {
    my ($this,$item) = @_;
    
    @$this = grep $_ != $item, @$this;
    
    return $this;
}

sub RemoveItemStr {
    my ($this,$item) = @_;
    
    @$this = grep $_ ne $item, @$this;
    
    return $this;
}

sub save {
	my ($this,$ctx) = @_;
	
	$ctx->AddVar( item => $_ ) foreach @$this;
}

sub restore {
	my ($class,$data,$surrogate) = @_;
	
	my $i = 0;
	
	if ($surrogate) {
		@$surrogate = grep { ($i++)%2 } @$data;
	} else {
		$surrogate = $class->new([grep { ($i++)%2 } @$data]);
	}
	
	return $surrogate;
}

1;