| 1 | package DBIx::Class::InflateColumn::Math::Currency; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use base qw/DBIx::Class/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Math::Currency; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 3 | our $VERSION = '0.2.0'; # VERSION | |||||||||||||||||||||||||||
| 15 | # ABSTRACT: Automagically inflates decimal columns into Math::Currency objects | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =pod | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =encoding utf8 | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 NAME | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | DBIx::Class::InflateColumn::Math::Currency - Inflate and Deflate "decimal" columns into Math::Currency Objects | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | package HorseTrack::Database::Schema::Result::Bet; | ||||||||||||||||||||||||||||
| 28 | use base 'DBIx::Class::Core'; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | use strict; | ||||||||||||||||||||||||||||
| 31 | use warnings; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | __PACKAGE__->load_components("InflateColumn::Math::Currency"); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | __PACKAGE__->add_columns( | ||||||||||||||||||||||||||||
| 36 | id => { data_type => 'integer' }, | ||||||||||||||||||||||||||||
| 37 | gambler_id => { data_type => 'integer' }, | ||||||||||||||||||||||||||||
| 38 | amount => { data_type => 'decimal', size => [9,2], is_currency => 1 }, | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 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. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 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. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | In the event anything other than a Math::Currency object, an integer, or a float, is provided this module will croak, stating as such. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head2 Inflation | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 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. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | package HorseTrack::Bet; | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | use strict; | ||||||||||||||||||||||||||||
| 56 | use warnings; | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | use Moose; | ||||||||||||||||||||||||||||
| 59 | use namespace::autoclean; | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | use Math::Currency; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | has 'id' => ( is => 'rw', isa => 'Int' ); | ||||||||||||||||||||||||||||
| 64 | has 'gamber_id => ( is => 'rw', isa => 'Int' ); | ||||||||||||||||||||||||||||
| 65 | has 'amount' => ( is => 'rw', isa => 'Math::Currency', is_currency => 1 ); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | sub retrieve { | ||||||||||||||||||||||||||||
| 68 | my $self = shift; | ||||||||||||||||||||||||||||
| 69 | my $result = $schema->resultset('...')->search({ id => 1 })->single; | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | my $bet = $self->new({ | ||||||||||||||||||||||||||||
| 72 | id => $result->id, | ||||||||||||||||||||||||||||
| 73 | gamber_id => $result->gamber_id, | ||||||||||||||||||||||||||||
| 74 | amount => $result->amount, | ||||||||||||||||||||||||||||
| 75 | }); | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | return $bet; | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | __PACKAGE__->meta->make_immutable(); | ||||||||||||||||||||||||||||
| 81 | 1; | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head2 Deflation | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 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: | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | $schema->resultset('...')->create({ | ||||||||||||||||||||||||||||
| 88 | id => $self->id, | ||||||||||||||||||||||||||||
| 89 | gamber_id => $self->gambler_id, | ||||||||||||||||||||||||||||
| 90 | amount => $self->amount, | ||||||||||||||||||||||||||||
| 91 | }); | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head1 METHODS | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 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. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | Therefore, there are no public methods to be consumed. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =cut | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub register_column { | ||||||||||||||||||||||||||
|
Subroutine
register_column: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 102 | 0 | my ($self, $column, $info, @rest) = @_; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | $self->next::method($column, $info, @rest); | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if( $info->{data_type} eq 'decimal' && $info->{is_currency } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$info->{"data_type"} eq "decimal" and $info->{"is_currency"}
MC/DC0%0/2
$info->{"data_type"} eq "decimal" and $info->{"is_currency"}
$info->{"data_type"} eq "decimal"
$info->{"is_currency"}
Truth table0%0/3
A and B
A= $info->{"data_type"} eq "decimal"B= $info->{"is_currency"}
| |||||||||||||||||||||||||||||
| 107 | 0 | $self->inflate_column( | |||||||||||||||||||||||||||
| 108 | $column => { | ||||||||||||||||||||||||||||
| 109 | inflate => \&_inflate, | ||||||||||||||||||||||||||||
| 110 | deflate => \&_deflate, | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | ); | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | return; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 7 | ▶ | sub _inflate { | ||||||||||||||||||||||||||
|
Subroutine
_inflate: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 119 | 7 | my $value = shift; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 7 | ▶ | if(ref $value eq "Math::Currency") { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 122 | 4 | return $value; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | elsif(looks_like_number($value)) { | ||||||||||||||||||||||||||||
| 125 | 2 | return Math::Currency->new($value); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | else { | ||||||||||||||||||||||||||||
| 128 | 1 | croak "Failed to inflate " . $value | |||||||||||||||||||||||||||
| 129 | . ". This value is not a Math::Currency object nor does it look like a number"; | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 7 | ▶ | sub _deflate { | ||||||||||||||||||||||||||
|
Subroutine
_deflate: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 134 | 7 | my $value = shift; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 7 | ▶ | if(ref $value eq "Math::Currency") { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 137 | 2 | return $value->as_float; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | elsif(looks_like_number($value)) { | ||||||||||||||||||||||||||||
| 140 | 4 | return $value; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | else { | ||||||||||||||||||||||||||||
| 143 | 1 | croak "Failed to deflate " . $value | |||||||||||||||||||||||||||
| 144 | . ". This value is not a Math::Currency object nor does it look like a number"; | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 3 | 1; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | __END__ | ||||||||||||||||||||||||||||