blib/lib/CLDR/Number/Format/Currency.pm

filter: Statementstmt 96.0% 73 / 76 Branchbran 76.4% 26 / 34 Conditioncond 71.4% 15 / 21 MC/DCmcdc 50.0% 7 / 14 Subroutinesub 100% 18 / 18 Podpod 50.0% 1 / 2 totaltotal 84.8% 140 / 165 CC 26 SCAR 15.3
CC
26
Cov
87%
_trigger_cash
9.8
format
7.0
_build_currency_sign
6.0
CRAP 4.6
« blib/lib/CLDR/Number/Role/Format.pm ↑ Summary Build »
1 package CLDR::Number::Format::Currency;
2  
3 11 ▶use v5.8.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use CLDR::Number::Constant qw( $C );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use CLDR::Number::Data::Currency;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 11 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 our $VERSION = '0.17';
13  
14 11 with qw( CLDR::Number::Role::Format );
15  
16 has currency_code => (
17 is => 'rw',
18 isa => sub {
19 86 ▶ croak 'uninitialized value' if !defined $_[0];
Subroutine __ANON__: called CC=3 SCAR=12.0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 86 86
20 86 ▶ croak qq{invalid value "$_[0]"} if $_[0] !~ m{ ^ [A-Z]{3} $ }x;
Branch: 1 branch
BranchTrueFalseTotal
if not $_[0] =~ / ^ [A-Z]{3} $ /x 0 86 86
21 },
22 86 ▶ coerce => sub { defined $_[0] ? uc $_[0] : $_[0] },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 86 0 86
23 11 trigger => 1,
24 );
25  
26 has currency_sign => (
27 is => 'rw',
28 isa => sub {
29 174 ▶ croak "currency_sign is not defined" if !defined $_[0];
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] 0 174 174
30 },
31 11 );
32  
33 # TODO: accounting NYI
34 has accounting => (
35 is => 'rw',
36 81 ▶ coerce => sub { $_[0] ? 1 : 0 },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 81 81
37 11 default => 0,
38 );
39  
40 has cash => (
41 is => 'rw',
42 81 ▶ coerce => sub { $_[0] ? 1 : 0 },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 81 81
43 11 trigger => 1,
44 default => 0,
45 );
46  
47 11 has _pattern_type => (
48 is => 'ro',
49 default => 'currency',
50 );
51  
52 after _trigger_locale => sub {
53 87 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
54  
55 87 ▶ if ($self->currency_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->currency_code) 82 5 87
56 82 $self->_build_currency_sign;
57 }
58  
59 87 ▶ if (my $decimal = $self->_get_data(symbol => 'currency_decimal')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $decimal = $self->_get_data("symbol", "currency_decimal")) 2 85 87
60 2 $self->decimal_sign($decimal);
61 }
62 11 };
63  
64 ▶sub BUILD {}
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
65  
66 sub _trigger_currency_code {
67 86 ▶ my ($self, $currency_code) = @_;
Subroutine _trigger_currency_code: called CC=1 SCAR=0.0
68  
69 86 $self->_build_currency_sign;
70 86 $self->_trigger_cash;
71 }
72  
73 sub _build_currency_sign {
74 168 ▶ my ($self) = @_;
Subroutine _build_currency_sign: called CC=6 SCAR=17.9
75 168 my $data = $CLDR::Number::Data::Currency::LOCALES;
76 168 my $currency_sign;
77  
78 168 ▶ return if $self->_has_init_arg('currency_sign');
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_init_arg("currency_sign") 2 166 168
79  
80 166 for my $locale (@{$self->_locale_inheritance}) {
81 169 ▶ next if !exists $data->{$locale} || !exists $data->{$locale}{$self->currency_code};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{$locale} and exists $data->{$locale}{$self->currency_code} 89 80 169
Condition100%3/3
exists $data->{$locale} and exists $data->{$locale}{$self->currency_code}
!ll&&!rl&&r
187180
MC/DC100%2/2
exists $data->{$locale} and exists $data->{$locale}{$self->currency_code}
exists $data->{$locale} exists $data->{$locale}{$self->currency_code}
Truth table100%3/3
A and B
A= exists $data->{$locale}B= exists $data->{$locale}{$self->currency_code}
ABresult
0X0
100
111
82 80 $currency_sign = $data->{$locale}{$self->currency_code};
83 80 last;
84 }
85  
86 166 ▶ $self->currency_sign($currency_sign || $self->currency_code);
Condition66%2/3
$currency_sign || $self->currency_code
l!l&&r!l&&!r
80860
MC/DC0%0/2
$currency_sign || $self->currency_code
$currency_sign $self->currency_code
Truth table66%2/3
A || B
A= $currency_signB= $self->currency_code
ABresult
1X1
011
000
87 }
88  
89 sub _trigger_cash {
90 86 ▶ my ($self) = @_;
Subroutine _trigger_cash: called CC=7 SCAR=22.8
91 86 my $currencies = $CLDR::Number::Data::Currency::CURRENCIES;
92 my $currency_data
93 = $self->currency_code && exists $currencies->{$self->currency_code}
94 ? $currencies->{$self->currency_code}
95 86 ▶ : $currencies->{DEFAULT};
Branch: 1 branch
BranchTrueFalseTotal
$self->currency_code && exists $$currencies{$self->currency_code} ? : 16 70 86
Condition66%2/3
$self->currency_code and exists $$currencies{$self->currency_code}
!ll&&!rl&&r
07016
MC/DC50%1/2
$self->currency_code and exists $$currencies{$self->currency_code}
$self->currency_code exists $$currencies{$self->currency_code}
Truth table66%2/3
A and B
A= $self->currency_codeB= exists $$currencies{$self->currency_code}
ABresult
0X0
100
111
96  
97 86 ▶ if ($self->cash && exists $currency_data->{cash_digits}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cash and exists $currency_data->{'cash_digits'}) { } 0 86 86
Condition33%1/3
$self->cash and exists $currency_data->{"cash_digits"}
!ll&&!rl&&r
8600
MC/DC0%0/2
$self->cash and exists $currency_data->{"cash_digits"}
$self->cash exists $currency_data->{"cash_digits"}
Truth table33%1/3
A and B
A= $self->cashB= exists $currency_data->{"cash_digits"}
ABresult
0X0
100
111
98 0 $self->minimum_fraction_digits($currency_data->{cash_digits});
99 0 $self->maximum_fraction_digits($currency_data->{cash_digits});
100 }
101 else {
102 86 $self->minimum_fraction_digits($currency_data->{digits});
103 86 $self->maximum_fraction_digits($currency_data->{digits});
104 }
105  
106 86 ▶ if ($self->cash && exists $currency_data->{cash_rounding}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cash and exists $currency_data->{'cash_rounding'}) { } 0 86 86
Condition33%1/3
$self->cash and exists $currency_data->{"cash_rounding"}
!ll&&!rl&&r
8600
MC/DC0%0/2
$self->cash and exists $currency_data->{"cash_rounding"}
$self->cash exists $currency_data->{"cash_rounding"}
Truth table33%1/3
A and B
A= $self->cashB= exists $currency_data->{"cash_rounding"}
ABresult
0X0
100
111
107 0 $self->rounding_increment($currency_data->{cash_rounding});
108 }
109 else {
110 86 $self->rounding_increment($currency_data->{rounding});
111 }
112 }
113  
114 sub format {
115 111 ▶ my ($self, $num) = @_;
Subroutine format: called CC=7 SCAR=19.5
Pod documented
116  
117 111 $num = $self->_validate_number(format => $num);
118 111 ▶ return undef unless defined $num;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $num 1 110 111
119  
120 110 ▶ croak 'Missing required attribute: currency_code'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->currency_code 1 109 110
121 unless $self->currency_code;
122  
123 109 my $sign = $self->currency_sign;
124 109 my $format = $self->_format_number($num);
125  
126 # spacing before currency sign
127 109 ▶ if ($sign =~ m{ ^ \PS }x && $format =~ m{ \d $C }x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign =~ / ^ \PS /x and $format =~ / \d $CLDR::Number::Format::Currency::C /x) 4 105 109
Condition100%3/3
$sign =~ / ^ \PS /x and $format =~ / \d $CLDR::Number::Format::Currency::C /x
!ll&&!rl&&r
64414
MC/DC100%2/2
$sign =~ / ^ \PS /x and $format =~ / \d $CLDR::Number::Format::Currency::C /x
$sign =~ / ^ \PS /x $format =~ / \d $CLDR::Number::Format::Currency::C /x
Truth table100%3/3
A and B
A= $sign =~ / ^ \PS /xB= $format =~ / \d $CLDR::Number::Format::Currency::C /x
ABresult
0X0
100
111
128 4 $sign = ' ' . $sign;
129 }
130  
131 # spacing after currency sign
132 109 ▶ if ($sign =~ m{ \PS $ }x && $format =~ m{ $C \d }x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign =~ / \PS $ /x and $format =~ / $CLDR::Number::Format::Currency::C \d /x) 14 95 109
Condition100%3/3
$sign =~ / \PS $ /x and $format =~ / $CLDR::Number::Format::Currency::C \d /x
!ll&&!rl&&r
761914
MC/DC100%2/2
$sign =~ / \PS $ /x and $format =~ / $CLDR::Number::Format::Currency::C \d /x
$sign =~ / \PS $ /x $format =~ / $CLDR::Number::Format::Currency::C \d /x
Truth table100%3/3
A and B
A= $sign =~ / \PS $ /xB= $format =~ / $CLDR::Number::Format::Currency::C \d /x
ABresult
0X0
100
111
133 14 $sign .= ' ';
134 }
135  
136 109 $format =~ s{$C}{$sign};
137  
138 109 return $format;
139 }
140  
141 11 1;
142  
143 __END__
« blib/lib/CLDR/Number/Role/Format.pm ↑ Summary Build »