Collection::Utl::ActiveRecord - Tools for track changes in HASHes.


Collection documentation Contained in the Collection distribution.

Index


Code Index:

NAME

Top

 Collection::Utl::ActiveRecord -  Tools for track changes in HASHes.

SYNOPSIS

Top

 use Collection::Utl::ActiveRecord;

 sub _prepare_record {
    my ( $self, $key, $ref ) = @_;
    my %hash;
    tie %hash, 'Collection::Utl::ActiveRecord', hash => $ref;
    return \%hash;
 }

DESCRIPTION

Top

 Tools for track changes in HASHes.

SEE ALSO

Top

Tie::StdHash

AUTHOR

Top

Zahatski Aliaksandr, <zag@cpan.org>

COPYRIGHT AND LICENSE

Top


Collection documentation Contained in the Collection distribution.
package Collection::Utl::ActiveRecord;

use strict;
use warnings;
use strict;
use Carp;
use Data::Dumper;
require Tie::Hash;
use Collection::Utl::Base;
@Collection::Utl::ActiveRecord::ISA = qw(Tie::StdHash Collection::Utl::Base);
$Collection::Utl::ActiveRecord::VERSION = '0.01';

attributes qw( _changed _orig_record __temp_array);

sub _init {
    my $self = shift;
    return $self->Init(@_);
}

sub DELETE {
    my ( $self, $key ) = @_;
    $self->_changed(1);
    delete $self->_orig_record->{$key};

}

sub STORE {
    my ( $self, $key, $val ) = @_;
    my $hash = $self->_orig_record;
    $self->_changed(1);
    $hash->{$key} = $val;
}

sub FETCH {
    my ( $self, $key ) = @_;
    if ( $key eq '_changed' ) {
        $self->_changed();
    }
    else {
        $self->_orig_record->{$key};
    }
}

sub Init {
    my ( $self, %arg ) = @_;
    $self->_orig_record( $arg{hash} );
    unless ( $arg{hash} ) {
        carp "Not inited param hash"
    }
    $self->_changed(0);
    return 1;
}

sub GetKeys {
    my $self = shift;
    my $hash = $self->_orig_record;
    return [ keys %$hash ];
}


sub TIEHASH {return Collection::Utl::Base::new(@_) }

sub FIRSTKEY {
    my ($self) = @_;
    $self->{__temp_array} =  [ sort { $a cmp $b } @{ $self->GetKeys() } ] ;
    shift( @{ $self->{__temp_array} } );
}

sub NEXTKEY {
    my ( $self, $key ) = @_;
    shift( @{ $self->{__temp_array} } );
}

sub EXISTS {
    my ( $self, $key ) = @_;
    my $hash = $self->_orig_record;
    return exists $hash->{$key};
}

sub CLEAR {
    my $self = shift;
    %{ $self->_orig_record } = ();
    $self->_changed(1);
}

1;
__END__