Data-Morph

 view release on metacpan or  search on metacpan

lib/Data/Morph/Backend/DBIC.pm  view on Meta::CPAN

package Data::Morph::Backend::DBIC;
$Data::Morph::Backend::DBIC::VERSION = '1.140400';
#ABSTRACT: Provides a Data::Morph backend for DBIx::Class

use Moose;
use Moose::Util::TypeConstraints;
use MooseX::Types::Moose(':all');
use MooseX::Params::Validate;
use Devel::PartialDump('dump');
use namespace::autoclean;
use DBIx::Class;
use Scalar::Util('weaken');


has result_set =>
(
    is => 'ro',
    isa => class_type('DBIx::Class::ResultSet'),
    required => 1,
);


has auto_insert =>
(
    is => 'ro',
    isa => Bool,
    default => 0,
);


has new_instance =>
(
    is => 'ro',
    isa => CodeRef,
    lazy => 1,
    builder => '_build_new_instance',
);


sub _build_new_instance
{
    my ($self) = @_;
    weaken($self);
    return sub { $self->result_set->new_result({}) };
}


sub epilogue
{
    my ($self, $obj) = @_;

    $obj->update_or_insert
        if $self->auto_insert;
}

with 'Data::Morph::Role::Backend' =>
{
    input_type => class_type('DBIx::Class::Row'),
    get_val => sub
    {
        my ($obj, $key) = @_;
        if($obj->can($key))
        {
            return $obj->$key;
        }
        elsif($obj->has_column($key))
        {
            return +{$obj->get_inflated_columns}->{$key};
        }
        else
        {
            die "Can't find '$key' column in: " . dump($obj);
        }
    },
    set_val => sub
    {
        my ($obj, $key, $value) = @_;
        if($obj->can($key))
        {
            $obj->$key($value);
        }
        elsif($obj->has_column($key))
        {
            return $obj->set_inflated_columns({$key => $value});
        }
        else
        {
            die "Can't find '$key' column in: " . dump($obj);
        }
    }
};

__PACKAGE__->meta->make_immutable();
1;

__END__

=pod

=head1 NAME

Data::Morph::Backend::DBIC - Provides a Data::Morph backend for DBIx::Class



( run in 1.252 second using v1.01-cache-2.11-cpan-39bf76dae61 )