blib/lib/DBIx/Class/InflateColumn/Math/Currency.pm

filter: Statementstmt 89.1% 33 / 37 Branchbran 80.0% 8 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.0% 9 / 10 Podpod 0.0% 0 / 1 totaltotal 86.2% 50 / 58 CC 6 SCAR 9.0
CC
6
Cov
87%
register_column
6.0
_inflate
3.0
_deflate
3.0
CRAP 2.5
« Build ↑ Summary
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.0.1'; # 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] },
39 );
40  
41 =head1 DESCRIPTION
42  
43 This module can be used to automagically inflate database columns of data type "decimal" into
44 Math::Currency objects. It is used similiar to other InflateColumn DBIx modules.
45  
46 Once your Result is properly defined you can now pass Math::Currency objects (and regular integers and
47 floats for that matter, they need not be Math::Currency objects) into columns of data_type decimal and
48 retrieve Math::Currency objects from these columns as well.
49  
50 In the event anything other than a Math::Currency object, an integer, or a float, is provided this module
51 will croak, stating as such.
52  
53 =head2 Inflation
54  
55 Inflation occurs whenever the data is being taken FROM the database. In this case the database
56 is storing the value with data_type of decimal, upon inflation a Math::Currency object is returned
57 from the resultset.
58  
59 package HorseTrack::Bet;
60  
61 use strict;
62 use warnings;
63  
64 use Moose;
65 use namespace::autoclean;
66  
67 use Math::Currency;
68  
69 has 'id' => ( is => 'rw', isa => 'Int' );
70 has 'gamber_id => ( is => 'rw', isa => 'Int' );
71 has 'amount' => ( is => 'rw', isa => 'Math::Currency' );
72  
73 sub retrieve {
74 my $self = shift;
75 my $result = $schema->resultset('...')->search({ id => 1 })->single;
76  
77 my $bet = $self->new({
78 id => $result->id,
79 gamber_id => $result->gamber_id,
80 amount => $result->amount,
81 });
82  
83 return $bet;
84 }
85  
86 __PACKAGE__->meta->make_immutable();
87 1;
88  
89 =head2 Deflation
90  
91 Deflation occurs whenever the data is being taken TO the database. In this case an object
92 of type Math::Currency is being stored into the a database columns with a data_type of "decimal".
93 Using the same object from the Inflation example:
94  
95 $schema->resultset('...')->create({
96 id => $self->id,
97 gamber_id => $self->gambler_id,
98 amount => $self->amount,
99 });
100  
101 =head1 METHODS
102  
103 Strictly speaking, you don't actually call any of these methods yourself. DBIx handles the magic
104 provided you have included the InflateColumn::Math::Currency component in your Result.
105  
106 Therefore, there are no public methods to be consumed.
107  
108 =cut
109  
110 sub register_column {
111 0 ▶ my ($self, $column, $info, @rest) = @_;
Subroutine register_column: not called CC=2 SCAR=17.9
Pod undocumented
112  
113 0 $self->next::method($column, $info, @rest);
114  
115 0 ▶ return unless $info->{data_type} eq 'decimal';
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"data_type"} eq "decimal" 0 0 0
116  
117 0 $self->inflate_column(
118 $column => {
119 inflate => \&_inflate,
120 deflate => \&_deflate,
121 }
122 );
123 }
124  
125 sub _inflate {
126 7 ▶ my $value = shift;
Subroutine _inflate: called CC=3 SCAR=11.0
127  
128 7 ▶ if(ref $value eq "Math::Currency") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'Math::Currency') { } 4 3 7
elsif (&looks_like_number($value)) { } 2 1 3
129 4 return $value;
130 }
131 elsif(looks_like_number($value)) {
132 2 return Math::Currency->new($value);
133 }
134 else {
135 1 croak "Failed to inflate " . $value
136 . ". This value is not a Math::Currency object nor does it look like a number";
137 }
138 }
139  
140 sub _deflate {
141 7 ▶ my $value = shift;
Subroutine _deflate: called CC=3 SCAR=11.0
142  
143 7 ▶ if(ref $value eq "Math::Currency") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'Math::Currency') { } 2 5 7
elsif (&looks_like_number($value)) { } 4 1 5
144 2 return $value->as_float;
145 }
146 elsif(looks_like_number($value)) {
147 4 return $value;
148 }
149 else {
150 1 croak "Failed to deflate " . $value
151 . ". This value is not a Math::Currency object nor does it look like a number";
152 }
153 }
154  
155 3 1;
156  
157 __END__
« Build ↑ Summary