blib/lib/Interchange6/Currency.pm

filter: Statementstmt 98.0% 99 / 101 Branchbran 96.1% 24 / 26 (1 uncoverable) Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 96.4% 27 / 28 Podpod 100% 12 / 12 totaltotal 95.8% 162 / 170 (1 uncoverable) CC 15 SCAR 9.4
CC
15
Cov
96%
convert
7.1
_clean_arg
4.0
__ANON__
2.0
CRAP 2.6
↑ Summary
1 package Interchange6::Currency;
2  
3 =head1 NAME
4  
5 Interchange6::Currency - Currency objects for Interchange 6
6  
7 =head1 VERSION
8  
9 0.101
10  
11 =for stopwords CLDR eCommerce Mottram Shutterstock SysPete
12  
13 =cut
14  
15 3 our $VERSION = '0.101';
16  
17 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 extends 'CLDR::Number::Format::Currency';
19  
20 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use Class::Load qw/load_class/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use Math::BigFloat;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use Sub::Quote qw/quote_sub/;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 use overload
27 0 ▶ '0+' => sub { shift->value },
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 52 ▶ '""' => sub { shift->as_string },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 3 '+' => \&_add,
30 '-' => \&_subtract,
31 '*' => \&_multiply,
32 '/' => \&_divide,
33 '%' => \&modulo,
34 '<=>' => \&cmp_value,
35 'cmp' => \&cmp,
36 '=' => \&clone,
37 3 ;
38  
39 =head1 DESCRIPTION
40  
41 Currency objects for Interchange6 Open Source eCommerce with Unicode
42 CLDR localization and rate conversion.
43  
44 Extends L<CLDR::Number::Format::Currency> with accurate calculation functions
45 using L<Math::BigFloat>.
46  
47 Many useful standard operators are overloaded and return currency objects
48 if appropriate.
49  
50 Although this class was written to satisfy the specific requirements of
51 Interchange6 it is suitable for use as a standalone module. See L</SEE ALSO>
52 for other currency modules.
53  
54 =head1 ATTRIBUTES
55  
56 =head2 value
57  
58 Value as simple decimal, e.g.: 3.45
59  
60 All values are coerced into L<Math::BigFloat>.
61  
62 =cut
63  
64 3 has value => (
65 is => 'rwp',
66 required => 1,
67 coerce => quote_sub(q{ $_[0]->$_isa("Math::BigFloat") ? $_[0] : Math::BigFloat->new( $_[0] ) }),
68 );
69  
70 # check for currency objects with different currency codes and if arg
71 # is a currency object return its value
72 78 ▶sub _clean_arg {
Subroutine _clean_arg: called CC=4 SCAR=13.9
73 78 my ( $self, $arg ) = @_;
74  
75 # uncoverable branch true
76 78 ▶ croak "_clean_arg is a class method" unless $self->$_isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$Interchange6::Currency::_isa(__PACKAGE__) 0 78 78
77  
78 78 ▶ if ( $arg->$_isa(__PACKAGE__) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->$Interchange6::Currency::_isa(__PACKAGE__)) 19 59 78
79 19 ▶ croak "Cannot perform calculation when currencies do not match"
Branch: 1 branch
BranchTrueFalseTotal
if $self->currency_code ne $arg->currency_code 7 12 19
80 if $self->currency_code ne $arg->currency_code;
81 12 return $arg->value;
82 }
83 59 return $arg;
84 }
85  
86 =head2 converter_class
87  
88 Defaults to L<Finance::Currency::Convert::WebserviceX>.
89  
90 The class name which handles conversion to a new C<currency_code>.
91  
92 The converter class can be any class that supports the following method
93 signature:
94  
95 sub convert {
96 my ($self, $price, $from, $to) = @_;
97
98 return $converted_price;
99 };
100  
101 In addition this module supports the following converters:
102  
103 =over
104  
105 =item * L<Finance::Currency::Convert::WebserviceX>
106  
107 =back
108  
109 =cut
110  
111 3 has converter_class => (
112 is => 'ro',
113 isa => quote_sub(q{ die "$_[0] is not a valid class name" unless ( ref(\$_[0]) eq 'SCALAR' && $_[0] =~ /^\S+$/ ) }),
114 default => "Finance::Currency::Convert::WebserviceX",
115 );
116  
117 =head2 converter
118  
119 Vivified L</converter_class>.
120  
121 =cut
122  
123 3 has converter => (
124 is => 'lazy',
125 isa => quote_sub(q{ die "Not a valid converter class" unless $_[0]->$_can('convert') }),
126 init_arg => undef,
127 );
128  
129 4 ▶sub _build_converter {
Subroutine _build_converter: called CC=1 SCAR=0.0
130 4 my $self = shift;
131 4 load_class( $self->converter_class );
132 3 return $self->converter_class->new;
133 }
134  
135 =head1 METHODS
136  
137 =head2 BUILD
138  
139 Sets precision for automatic rounding of L</value> to
140 L<CLDR::Number::Format::Currency/maximum_fraction_digits>.
141  
142 =cut
143  
144 43 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod documented
145 43 my $self = shift;
146  
147 # force cash trigger so we can reset precision on value
148 43 $self->cash( $self->cash );
149 43 $self->value->precision( -$self->maximum_fraction_digits );
150 }
151  
152 =head2 clone %new_attrs?
153  
154 Returns clone of the currency object possibly with new attribute values (if
155 any are supplied).
156  
157 =cut
158  
159 34 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
160 34 my ( $self, %new_attrs ) = @_;
161 34 return __PACKAGE__->new(
162 value => $self->value,
163 currency_code => $self->currency_code,
164 locale => $self->locale,
165 %new_attrs,
166 );
167 }
168  
169 =head2 convert $new_corrency_code
170  
171 Convert to new currency using L</converter>.
172  
173 B<NOTE:> If C</convert> is called in void context then the currency object
174 is mutated in place. If called in list or scalar context then the original
175 object is not modified and a new L<Interchange6::Currency> object is returned
176 instead.
177  
178 =cut
179  
180 11 ▶sub convert {
Subroutine convert: called CC=7 SCAR=19.6
Pod documented
181 11 my ( $self, $new_code ) = @_;
182  
183 11 ▶ if ( $self->currency_code eq $new_code ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->currency_code eq $new_code) { } 2 9 11
184  
185 # currency code has not changed
186 2 ▶ if ( defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 1 1 2
187  
188 # called in list or scalar context
189 1 return $self->clone;
190 }
191 else {
192  
193 # void context
194 1 return;
195 }
196 }
197 else {
198  
199 # remove precision before conversion since new currency may have
200 # different maximum_fraction_digits and we don't want to lose accuracy
201 9 $self->value->precision(undef);
202  
203 # currency code has changed so convert via converter_class
204  
205 9 my $new_value;
206  
207 9 ▶ if ( $self->converter_class eq 'Finance::Currency::Convert::XE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->converter_class eq 'Finance::Currency::Convert::XE') { } 0 9 9
208  
209 # XE is special
210 0 ▶ $new_value = $self->converter->convert(
Condition0%0/2
$self->converter->convert('value', $self->value, 'source', $self->currency_code, 'target', $new_code) || croak('convert failed: ' . $self->converter->error . "\n")
l!l
00
MC/DC0%0/1
$self->converter->convert('value', $self->value, 'source', $self->currency_code, 'target', $new_code) || croak('convert failed: ' . $self->converter->error . "\n")
$self->converter->convert('value', $self->value, 'source', $self->currency_code, 'target', $new_code)
Truth table0%0/2
A
A= $self->converter->convert('value', $self->value, 'source', $self->currency_code, 'target', $new_code)
Aresult
11
00
211 value => $self->value,
212 source => $self->currency_code,
213 target => $new_code
214 ) || croak "convert failed: " . $self->converter->error . "\n";
215 }
216 else {
217 # other
218 9 $new_value =
219 $self->converter->convert( $self->value, $self->currency_code,
220 $new_code );
221 }
222  
223 8 ▶ croak "convert failed" unless defined $new_value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_value 1 7 8
224  
225 7 ▶ if ( defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 5 2 7
226  
227 # called in list or scalar context
228  
229 5 return $self->clone(
230 currency_code => $new_code,
231 value => $new_value,
232 );
233 }
234 else {
235  
236 # void context
237  
238 2 $self->currency_code($new_code);
239 2 $self->_set_value($new_value);
240  
241 # force cash trigger so we can reset precision on value
242 2 $self->cash( $self->cash );
243 2 $self->value->precision( -$self->maximum_fraction_digits );
244  
245 2 return;
246 }
247 }
248 }
249  
250 =head2 as_string
251  
252 Stringified formatted currency, e.g.: $3.45
253  
254 =cut
255  
256 74 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
257 74 return $_[0]->format( $_[0]->value );
258 }
259  
260 =head2 stringify
261  
262 Alias for L</as_string>.
263  
264 =cut
265  
266 1 ▶sub stringify {
Subroutine stringify: called CC=1 SCAR=0.0
Pod documented
267 1 return $_[0]->as_string;
268 }
269  
270 =head2 add $arg
271  
272 Add C<$arg> to L</value> in place.
273  
274 =cut
275  
276 3 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
Pod documented
277 3 my ( $self, $arg ) = @_;
278 3 $self->value->badd( $self->_clean_arg($arg) );
279 }
280  
281 # for overloaded '+'
282 8 ▶sub _add {
Subroutine _add: called CC=1 SCAR=0.0
283 8 my ( $self, $arg ) = @_;
284 8 $self->clone(
285 value => $self->value->copy->badd( $self->_clean_arg($arg) ) );
286 }
287  
288 =head2 subtract $arg
289  
290 Subtract C<$arg> from L</value> in place.
291  
292 =cut
293  
294 3 ▶sub subtract {
Subroutine subtract: called CC=1 SCAR=0.0
Pod documented
295 3 my ( $self, $arg ) = @_;
296 3 $self->value->bsub( $self->_clean_arg($arg) );
297 }
298  
299 # for overloaded '-'
300 8 ▶sub _subtract {
Subroutine _subtract: called CC=2 SCAR=6.9
301 8 my ( $self, $arg, $swap ) = @_;
302 8 my $result = $self->value->copy->bsub( $self->_clean_arg($arg) );
303 8 ▶ $self->clone( value => $swap ? $result->bneg : $result );
Branch: 1 branch
BranchTrueFalseTotal
$swap ? : 2 6 8
304 }
305  
306 =head2 multiply $arg
307  
308 Multiply L</value> by C<$arg> in place.
309  
310 =cut
311  
312 3 ▶sub multiply {
Subroutine multiply: called CC=1 SCAR=0.0
Pod documented
313 3 my ( $self, $arg ) = @_;
314 3 $self->value->bmul( $self->_clean_arg($arg) );
315 }
316  
317 # for overloaded '*'
318 2 ▶sub _multiply {
Subroutine _multiply: called CC=1 SCAR=0.0
319 2 my ( $self, $arg ) = @_;
320 2 $self->clone(
321 value => $self->value->copy->bmul( $self->_clean_arg($arg) ) );
322 }
323  
324 =head2 divide $arg
325  
326 Divide L</value> by C<$arg> in place.
327  
328 =cut
329  
330 3 ▶sub divide {
Subroutine divide: called CC=1 SCAR=0.0
Pod documented
331 3 my ( $self, $arg ) = @_;
332 3 $self->value->bdiv( $self->_clean_arg($arg) );
333 }
334  
335 # for overloaded '/'
336 5 ▶sub _divide {
Subroutine _divide: called CC=2 SCAR=6.9
337 5 my ( $self, $arg, $swap ) = @_;
338 5 my $result;
339 5 ▶ if ($swap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($swap) { } 1 4 5
340 1 $result =
341 Math::BigFloat->new( $self->_clean_arg($arg) )->bdiv( $self->value );
342 }
343 else {
344 4 $result = $self->value->copy->bdiv( $self->_clean_arg($arg) );
345 }
346 5 $self->clone( value => $result );
347 }
348  
349 =head2 modulo $arg
350  
351 Return L</value> C<%> C<$arg> as currency object.
352  
353 =cut
354  
355 6 ▶sub modulo {
Subroutine modulo: called CC=2 SCAR=6.9
Pod documented
356 6 my ( $self, $arg, $swap ) = @_;
357 6 my $result;
358 6 ▶ if ($swap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($swap) { } 1 5 6
359 1 $result =
360 Math::BigFloat->new( $self->_clean_arg($arg) )->bmod( $self->value );
361 }
362 else {
363 5 $result = $self->value->copy->bmod( $self->_clean_arg($arg) );
364 }
365 5 $self->clone( value => $result );
366 }
367  
368 =head2 cmp_value $arg
369  
370 Equivalent to L</value> C<< <=> >> C<$arg>.
371  
372 =cut
373  
374 37 ▶sub cmp_value {
Subroutine cmp_value: called CC=2 SCAR=6.9
Pod documented
375 37 my ( $self, $arg, $swap ) = @_;
376 37 ▶ if ($swap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($swap) { } 3 34 37
377 3 return Math::BigFloat->new( $self->_clean_arg($arg) )
378 ->bcmp( $self->value );
379 }
380 else {
381 34 return $self->value->bcmp( $self->_clean_arg($arg) );
382 }
383 }
384  
385 =head2 cmp $arg
386  
387 String comparison.
388  
389 Not always useful in itself since string comparison of stringified currency
390 objects might not produce what you expect depending on locale and currency
391 code.
392  
393 =cut
394  
395 29 ▶sub cmp {
Subroutine cmp: called CC=2 SCAR=6.9
Pod documented
396 29 my ( $self, $arg, $swap ) = @_;
397 29 ▶ if ($swap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($swap) { } 3 26 29
398 3 return "$arg" cmp "$self";
399 }
400 else {
401 26 return "$self" cmp "$arg";
402 }
403 }
404  
405 =head2 SEE ALSO
406  
407 Other modules which perform currency maths:
408  
409 =over
410  
411 =item * L<Math::Currency>
412  
413 =item * L<Data::Currency>
414  
415 =back
416  
417 Other Interchange6 eCommerce modules:
418  
419 =over
420  
421 =item * L<Interchange6>
422  
423 =item * L<Interchange6::Schema>
424  
425 =item * L<Dancer::Plugin::Interchange6>
426  
427 =back
428  
429 =head1 AUTHORS
430  
431 Peter Mottram (SysPete), C<peter@sysnix.com>
432  
433 =head1 ACKNOWLEDGEMENTS
434  
435 Shutterstock, Inc for the excellent L<CLDR::Number> and all of the authors
436 of L<Math::BigFLoat> and L<Moo>.
437  
438 =head1 LICENSE AND COPYRIGHT
439  
440 Copyright 2015-2016 Peter Mottram (SysPete).
441  
442 This program is free software; you can redistribute it and/or modify it
443 under the terms of either: the GNU General Public License as published
444 by the Free Software Foundation; or the Artistic License.
445  
446 See http://dev.perl.org/licenses/ for more information.
447  
448 Unicode is a registered trademark of Unicode, Inc., in the United States
449 and other countries.
450  
451 =cut
452  
453 3 1;
↑ Summary