File Coverage

lib/Badger/Prototype.pm
Criterion Covered Total %
statement 31 31 100.0
branch 6 6 100.0
condition 5 9 55.5
subroutine 7 7 100.0
pod 2 2 100.0
total 51 55 92.7


line stmt bran cond sub pod time code
1             #========================================================================
2             #
3             # Badger::Prototype
4             #
5             # DESCRIPTION
6             # Base class module for a protoype class that has a default instance
7             # that can be created on demand.
8             #
9             # AUTHOR
10             # Andy Wardley
11             #
12             #========================================================================
13              
14             package Badger::Prototype;
15              
16             use Badger::Class
17 70         796 base => 'Badger::Base',
18             version => 0.01,
19             debug => 0,
20             constants => 'PKG REFS ONCE',
21 70     70   395 words => 'PROTOTYPE';
  70         104  
22              
23             sub prototype {
24 6278     6278 1 6204 my $class = shift;
25 6278 100       10874 return $class if ref $class;
26 70     70   442 no strict REFS;
  70         109  
  70         1969  
27 70     70   308 no warnings ONCE;
  70         109  
  70         9203  
28            
29 2020 100 66     4723 if (@_ == 1 && ! defined $_[0]) {
    100          
30             # if only a single undef argument is provided, then clear any
31             # prototype from $PROTOTYPE and return a reference to it.
32 1         2 my $proto = ${$class.PKG.PROTOTYPE};
  1         4  
33 1         2 undef ${$class.PKG.PROTOTYPE};
  1         2  
34 1         2 return $proto;
35             }
36             elsif (@_) {
37             # if any other arguments are provided then it forces us to create
38             # a new prototype with the fresh configuration options.
39 1         2 undef ${$class.PKG.PROTOTYPE};
  1         4  
40             }
41            
42             # return the cached value (assuming we didn't just clear it) or create
43             # a new one (if we did, or if there wasn't a previous value)
44 2019   66     1819 return ${$class.PKG.PROTOTYPE} ||= $class->new(@_);
  2019         7943  
45             }
46              
47             sub has_prototype {
48 2     2 1 5 my $self = shift;
49 2   33     8 my $class = ref $self || $self;
50 70     70   420 no strict REFS;
  70         111  
  70         2044  
51 70     70   353 no warnings ONCE;
  70         164  
  70         4284  
52 2         3 defined ${$class.PKG.PROTOTYPE};
  2         13  
53             }
54            
55              
56             1;
57              
58             __END__