The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

DBIx::Class::InflateColumn::Math::Currency - Inflate and Deflate "decimal" columns into Math::Currency Objects

SYNOPSIS

    package HorseTrack::Database::Schema::Result::Bet;
    use base 'DBIx::Class::Core';

    use strict;
    use warnings;

    __PACKAGE__->load_components("InflateColumn::Math::Currency");

    __PACKAGE__->add_columns(
      id         => { data_type => 'integer' },
      gambler_id => { data_type => 'integer' },
      amount     => { data_type => 'decimal', size => [9,2], is_currency => 1 },
    );

DESCRIPTION

This module can be used to automagically inflate database columns of data type "decimal" that are flagged with "is_currency" into Math::Currency objects. It is used similiar to other InflateColumn DBIx modules.

Once your Result is properly defined you can now pass Math::Currency objects (and regular integers and floats for that matter, they need not be Math::Currency objects) into columns of data_type decimal and retrieve Math::Currency objects from these columns as well.

In the event anything other than a Math::Currency object, an integer, or a float, is provided this module will croak, stating as such.

Inflation

Inflation occurs whenever the data is being taken FROM the database. In this case the database is storing the value with data_type of decimal, upon inflation a Math::Currency object is returned from the resultset.

    package HorseTrack::Bet;

    use strict;
    use warnings;

    use Moose;
    use namespace::autoclean;

    use Math::Currency;

    has 'id'       => ( is => 'rw', isa => 'Int' );
    has 'gamber_id => ( is => 'rw', isa => 'Int' );
    has 'amount'   => ( is => 'rw', isa => 'Math::Currency', is_currency => 1 );

    sub retrieve {
        my $self = shift;
        my $result = $schema->resultset('...')->search({ id => 1 })->single;

        my $bet = $self->new({
            id        => $result->id,
            gamber_id => $result->gamber_id,
            amount    => $result->amount,
        });

        return $bet;
    }

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

Deflation

Deflation occurs whenever the data is being taken TO the database. In this case an object of type Math::Currency is being stored into the a database columns with a data_type of "decimal". Using the same object from the Inflation example:

    $schema->resultset('...')->create({
        id        => $self->id,
        gamber_id => $self->gambler_id,
        amount    => $self->amount,
    });

METHODS

Strictly speaking, you don't actually call any of these methods yourself. DBIx handles the magic provided you have included the InflateColumn::Math::Currency component in your Result.

Therefore, there are no public methods to be consumed.

AUTHORS

Robert Stone <drzigman AT cpan DOT org >

COPYRIGHT & LICENSE

Copyright 2013 Robert Stone

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU Lesser General Public License as published by the Free Software Foundation; or any compatible license.

See http://dev.perl.org/licenses/ for more information.