File Coverage

blib/lib/Money/ChangeMaker/Denomination.pm
Criterion Covered Total %
statement 29 35 82.8
branch 8 14 57.1
condition 4 9 44.4
subroutine 6 6 100.0
pod 4 4 100.0
total 51 68 75.0


line stmt bran cond sub pod time code
1             # Money::ChangeMaker::Denomination -- A representation of a monetary unit for
2             # use by Money::ChangeMaker
3             package Money::ChangeMaker::Denomination;
4              
5             require 5;
6 7     7   44 use strict;
  7         12  
  7         5708  
7              
8             our $VERSION = '0.3';
9              
10             ## Here there be methods
11              
12             # The constructor method.
13             # It takes 2 required arguments and one optional argument
14             # The first argument must be the numeric value of the monetary unit.
15             # It should be positive, but need not be integral. See the documentation
16             # for Money::ChangeMaker::Presets for information on the possible pitfalls
17             # of making non-integer denominations.
18             # The second argument must be the name of the denomination. ie "dime" or
19             # "one pound note".
20             # The optional third argument is the pluralization of the name. If this
21             # is not provided, then an "s" is appended to the name. It is
22             # surprising how often this is correct. :) An example -- "pennies".
23             sub new {
24 483     483 1 613 my($proto) = shift;
25 483   33     1873 my($class) = ref($proto) || $proto;
26 483         716 my($self) = {};
27 483         1069 bless($self, $class);
28 483 50 66     1218 if (@_ != 2 && @_ != 3) {
29 0         0 return Money::ChangeMaker::_warn(
30             "Denomination constructor must take 2 or 3 arguments"
31             );
32             }
33             # Allow "5f" to be treated as "5"
34 483         474 my $val;
35 483 50       1031 unless (($val = _number_of($_[0])) > 0) {
36 0         0 return Money::ChangeMaker::_warn(
37             "Denomenation value must be a number greater than 0"
38             );
39             }
40 483         1204 $self->{_VALUE} = $_[0];
41 483         783 $self->{_NAME} = $_[1];
42 483         841 $self->{_PLURAL} = $_[2];
43 483         1923 return($self);
44             }
45              
46             # Sets/returns the numeric value of this denomination
47             sub value {
48 597     597 1 790 my($self) = shift;
49 597         722 my($value) = shift;
50 597 50 33     1269 if (defined($value) && $value > 0) {
51 0         0 $self->{_VALUE} = $value;
52             }
53 597         2632 return $self->{_VALUE};
54             }
55              
56             # Sets/returns the descriptive name of this denomination
57             sub name {
58 146     146 1 269 my($self) = shift;
59 146         173 my($name) = shift;
60 146 50       291 if (defined($name)) {
61 0         0 $self->{_NAME} = $name;
62             }
63 146         577 return $self->{_NAME};
64             }
65              
66             # Sets/returns the descriptive name for plurals of this denomination.
67             # If this value is undef, will return the "name" value appended with an "s"
68             sub plural {
69 18     18 1 35 my($self) = shift;
70 18         28 my($plural) = shift;
71 18 50       47 if (defined($plural)) {
72 0         0 $self->{_PLURAL} = $plural;
73             }
74 18 100       1263 return $self->{_PLURAL} ? $self->{_PLURAL} : $self->{_NAME} . "s";
75             }
76              
77             # Converts a string containing numbers and letters into just a number.
78             # Returns -1 if there are no word-leading numbers
79             sub _number_of {
80 483     483   742 my $test = shift;
81 483 50       1687 if ($test =~ /^([\d.]+)/) {
82 483         2058 return $1;
83             }
84 0           return -1;
85             }
86              
87             1;
88              
89             __END__