Chef/Ingredient.pm

filter: Statementstmt 89.5% 43 / 48 Branchbran 61.1% 11 / 18 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 5 / 5 totaltotal 82.3% 70 / 85 CC 11 SCAR 13.8
CC
11
Cov
81%
determine_type
8.1
new
4.0
value
3.2
CRAP 4.0
« Chef/Recipe.pm ↑ Summary Chef/Container.pm »
1  
2 package Acme::Chef::Ingredient;
3  
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 ▶use vars qw/$VERSION %Measures %MeasureTypes/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 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<Acme::Chef>;
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 10 %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 10 %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 1643 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
71 1643 my $proto = shift;
72 1643 ▶ my $class = ref $proto || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
1587560
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
73  
74 1643 my $self = {};
75  
76 1643 ▶ if ( ref $proto ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto) 1587 56 1643
77 1587 %$self = %$proto;
78 }
79  
80 1643 my %args = @_;
81  
82 1643 %$self = (
83 name => '',
84 value => undef,
85 measure => '',
86 measure_type => '',
87 type => '',
88 %$self,
89 %args,
90 );
91  
92 1643 bless $self => $class;
93  
94 1643 ▶ $self->determine_type() if not $self->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"type"} 1205 438 1643
95  
96 1643 return $self;
97 }
98  
99  
100 =item type
101  
102 Returns the type of the Ingredient.
103  
104 =cut
105  
106 248 ▶sub type {
Subroutine type: called CC=2 SCAR=6.9
Pod documented
107 248 my $self = shift;
108  
109 248 ▶ $self->determine_type() if $self->{type} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"type"} eq "" 41 207 248
110  
111 248 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 1246 ▶sub determine_type {
Subroutine determine_type: called CC=5 SCAR=20.9
Pod documented
122 1246 my $self = shift;
123  
124 1246 my $type = '';
125  
126 exists $Measures{$self->{measure}}
127 1246 ▶ or croak "Invalid measure specified: '$self->{measure}'.";
Branch: 1 branch
BranchTrueFalseTotal
exists $Acme::Chef::Ingredient::Measures{$self->{"measure"}} or croak(qq[Invalid measure specified: '$self->{"measure"}'.]) 1246 0 1246
128  
129 1246 $type = $Measures{ $self->{measure} };
130  
131 1246 ▶ if ( exists $MeasureTypes{ $self->{measure_type} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Acme::Chef::Ingredient::MeasureTypes{$self->{"measure_type"}}) 0 1246 1246
132  
133 0 ▶ if ( $type eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq '') { } 0 0 0
134 0 $type = $MeasureTypes{ $self->{measure_type} };
135 } else {
136 0 ▶ $MeasureTypes{ $self->{measure_type} } eq $type
Branch: 1 branch
BranchTrueFalseTotal
$Acme::Chef::Ingredient::MeasureTypes{$self->{"measure_type"}} eq $type or croak(qq['Measure type' ($self->{"measure_type"}) does not match type of measure ($type).]) 0 0 0
137 or croak "'Measure type' ($self->{measure_type}) does not match type of measure ($type).";
138 }
139  
140 }
141  
142 1246 $self->{type} = $type;
143 1246 return $self->{type};
144 }
145  
146  
147 =item value
148  
149 Mutator for the Ingredient's value.
150  
151 =cut
152  
153 3481 ▶sub value {
Subroutine value: called CC=3 SCAR=11.6
Pod documented
154 3481 my $self = shift;
155 3481 my $new_val = shift;
156  
157 3481 ▶ $self->{value} = $new_val if defined $new_val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_val 1100 2381 3481
158  
159 3481 ▶ if (not defined $self->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"value"}) 0 3481 3481
160 0 my $name = $self->{name};
161 0 croak "Attempted to use undefined ingredient '$name'.";
162 }
163  
164 3481 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 174 ▶sub liquify {
Subroutine liquify: called CC=1 SCAR=0.0
Pod documented
175 174 my $self = shift;
176  
177 174 $self->{type} = 'liquid';
178  
179 174 return $self;
180 }
181  
182 __END__
« Chef/Recipe.pm ↑ Summary Chef/Container.pm »