Fey::Object::Mock::Table - Mock schema class subclass of Fey::Object::Table


Fey-ORM-Mock documentation Contained in the Fey-ORM-Mock distribution.

Index


Code Index:

NAME

Top

Fey::Object::Mock::Table - Mock schema class subclass of Fey::Object::Table

VERSION

Top

version 0.05

DESCRIPTION

Top

When you use Fey::ORM::Mock to mock a schema, this class will become the immediate parent for each of your table classes. It in turn inherits from Fey::Object::Table.

This class overrides various methods in order to record inserts, updates, and deletes. It also overrides _load_from_dbms() in order to use seeded values rather than fetching data from the DBMS.

METHODS

Top

This class provides the following methods:

$class->Seeder

Returns the Fey::ORM::Mock::Seeder object associated with the table.

$class->SetSeeder($seeder)

Sets the Fey::ORM::Mock::Recorder object associated with the table.

AUTHOR

Top

Dave Rolsky <autarch@urth.org>

COPYRIGHT AND LICENSE

Top


Fey-ORM-Mock documentation Contained in the Fey-ORM-Mock distribution.

package Fey::Object::Mock::Table;
BEGIN {
  $Fey::Object::Mock::Table::VERSION = '0.05';
}

use strict;
use warnings;

use Fey::Meta::Class::Schema;

use Moose;

extends 'Fey::Object::Table';

sub insert_many {
    my $class = shift;
    my @rows  = @_;

    $class->__record_insert($_) for @rows;

    return $class->SUPER::insert_many(@rows);
}

sub __record_insert {
    my $class = shift;
    my $vals  = shift;

    $class->__recorder->record_action(
        action => 'insert',
        class  => $class,
        values => $vals,
    );
}

sub update {
    my $self = shift;
    my %p    = @_;

    $self->__record_update( \%p );

    $self->SUPER::update(%p);
}

sub __record_update {
    my $self = shift;
    my $vals = shift;

    $self->__recorder->record_action(
        action => 'update',
        class  => ( ref $self ),
        values => $vals,
        pk     => { $self->pk_values_hash() },
    );
}

sub delete {
    my $self = shift;

    $self->__record_delete();

    $self->SUPER::delete(@_);
}

sub __record_delete {
    my $self = shift;

    $self->__recorder->record_action(
        action => 'delete',
        class  => ( ref $self ),
        pk     => { $self->pk_values_hash() },
    );
}

sub __recorder {
    my $self = shift;

    return Fey::Meta::Class::Schema->ClassForSchema( $self->Table->schema )
        ->Recorder();
}

sub _load_from_dbms {
    my $self = shift;

    if ( my $values = $self->Seeder()->next() ) {
        $self->_set_column_values_from_hashref($values);

        return;
    }

    return $self->SUPER::_load_from_dbms(@_);
}

{
    my %Seeder;

    sub Seeder {
        my $self = shift;

        return $Seeder{ ref $self || $self };
    }

    sub SetSeeder {
        my $self = shift;

        return $Seeder{ ref $self || $self } = shift;
    }
}

no Moose;

# inlining the constructor makes no sense, since we expect to be
# inherited from anyway, and those modules can inline their own
# constructor.
__PACKAGE__->meta()->make_immutable( inline_constructor => 0 );

1;

# ABSTRACT: Mock schema class subclass of Fey::Object::Table




__END__