DBIx::Class::InflateColumn::Math::Currency - Inflate and Deflate "decimal" columns into Math::Currency Objects
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 }, );
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 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 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, });
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.
Robert Stone <drzigman AT cpan DOT org >
<drzigman AT cpan DOT org >
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.
To install DBIx::Class::InflateColumn::Math::Currency, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::Class::InflateColumn::Math::Currency
CPAN shell
perl -MCPAN -e shell install DBIx::Class::InflateColumn::Math::Currency
For more information on module installation, please visit the detailed CPAN module installation guide.