File Coverage

blib/lib/Acme/Chef/Ingredient.pm
Criterion Covered Total %
statement 40 45 88.8
branch 11 18 61.1
condition 2 3 66.6
subroutine 9 9 100.0
pod 5 5 100.0
total 67 80 83.7


line stmt bran cond sub pod time code
1              
2             package Acme::Chef::Ingredient;
3              
4 7     7   37 use strict;
  7         60  
  7         230  
5 7     7   36 use warnings;
  7         13  
  7         154  
6              
7 7     7   39 use Carp;
  7         23  
  7         430  
8              
9 7     7   33 use vars qw/$VERSION %Measures %MeasureTypes/;
  7         11  
  7         4733  
10             $VERSION = '1.00';
11              
12             =head1 NAME
13              
14             Acme::Chef::Ingredient - Internal module used by Acme::Chef
15              
16             =head1 SYNOPSIS
17              
18             use Acme::Chef;
19              
20             =head1 DESCRIPTION
21              
22             Please see L;
23              
24             =head2 METHODS
25              
26             This is a list of methods in the package.
27              
28             =over 2
29              
30             =cut
31              
32              
33             %Measures = (
34             '' => '',
35              
36             g => 'dry',
37             kg => 'dry',
38             pinch => 'dry',
39             pinches => 'dry',
40             ml => 'liquid',
41             l => 'liquid',
42             dash => 'liquid',
43             dashes => 'liquid',
44             cup => '',
45             cups => '',
46             teaspoon => '',
47             teaspoons => '',
48             tablespoon => '',
49             tablespoons => '',
50             );
51              
52             %MeasureTypes = (
53             heaped => 'dry',
54             level => 'dry',
55             );
56              
57             =item new
58              
59             Acme::Chef::Ingredient constructor. Takes key/value pairs as argument which
60             will be used as attributes. The following attributes are currently
61             required:
62              
63             name
64             value
65             measure
66             measure_type
67              
68             =cut
69              
70             sub new {
71 1493     1493 1 1880 my $proto = shift;
72 1493   66     3419 my $class = ref $proto || $proto;
73              
74 1493         1973 my $self = {};
75              
76 1493 100       2875 if ( ref $proto ) {
77 1449         6288 %$self = %$proto;
78             }
79              
80 1493         2819 my %args = @_;
81              
82 1493         11757 %$self = (
83             name => '',
84             value => undef,
85             measure => '',
86             measure_type => '',
87             type => '',
88             %$self,
89             %args,
90             );
91              
92 1493         5043 bless $self => $class;
93              
94 1493 100       4262 $self->determine_type() if not $self->{type};
95              
96 1493         5999 return $self;
97             }
98              
99              
100             =item type
101              
102             Returns the type of the Ingredient.
103              
104             =cut
105              
106             sub type {
107 197     197 1 208 my $self = shift;
108              
109 197 100       424 $self->determine_type() if $self->{type} eq '';
110              
111 197         482 return $self->{type};
112             }
113              
114             =item determine_type
115              
116             Also returns the type if the Ingredient, but forces a fresh run of the
117             type inferer.
118              
119             =cut
120              
121             sub determine_type {
122 1192     1192 1 1220 my $self = shift;
123              
124 1192         1819 my $type = '';
125              
126 1192 50       2725 exists $Measures{$self->{measure}}
127             or croak "Invalid measure specified: '$self->{measure}'.";
128              
129 1192         1835 $type = $Measures{ $self->{measure} };
130              
131 1192 50       2849 if ( exists $MeasureTypes{ $self->{measure_type} } ) {
132              
133 0 0       0 if ( $type eq '' ) {
134 0         0 $type = $MeasureTypes{ $self->{measure_type} };
135             } else {
136 0 0       0 $MeasureTypes{ $self->{measure_type} } eq $type
137             or croak "'Measure type' ($self->{measure_type}) does not match type of measure ($type).";
138             }
139              
140             }
141              
142 1192         1453 $self->{type} = $type;
143 1192         1826 return $self->{type};
144             }
145              
146              
147             =item value
148              
149             Mutator for the Ingredient's value.
150              
151             =cut
152              
153             sub value {
154 3421     3421 1 3661 my $self = shift;
155 3421         3280 my $new_val = shift;
156              
157 3421 100       5970 $self->{value} = $new_val if defined $new_val;
158              
159 3421 50       5949 if (not defined $self->{value}) {
160 0         0 my $name = $self->{name};
161 0         0 croak "Attempted to use undefined ingredient '$name'.";
162             }
163              
164 3421         8059 return $self->{value};
165             }
166              
167              
168             =item liquify
169              
170             Sets the type of the Ingredient to be liquid.
171              
172             =cut
173              
174             sub liquify {
175 126     126 1 117 my $self = shift;
176              
177 126         155 $self->{type} = 'liquid';
178              
179 126         221 return $self;
180             }
181              
182             __END__