File Coverage

lib/Sub/Contract/Pool.pm
Criterion Covered Total %
statement 55 61 90.1
branch 12 16 75.0
condition 13 27 48.1
subroutine 14 16 87.5
pod 9 9 100.0
total 103 129 79.8


line stmt bran cond sub pod time code
1             #
2             # Sub::Contract::Pool - The pool of contracts
3             #
4             # $Id: Pool.pm,v 1.15 2009/06/16 12:23:58 erwan_lemonnier Exp $
5             #
6              
7             package Sub::Contract::Pool;
8              
9 22     22   3879 use strict;
  22         40  
  22         1227  
10 22     22   109 use warnings;
  22         40  
  22         591  
11              
12 22     22   109 use Carp qw(croak);
  22         47  
  22         1040  
13              
14 22     22   119 use vars qw($AUTOLOAD);
  22         35  
  22         1625  
15 22         136 use accessors qw( _contract_index
16 22     22   108 );
  22         43  
17              
18 22     22   2049 use base qw(Exporter);
  22         44  
  22         22743  
19              
20             our $VERSION = '0.12';
21              
22             our @EXPORT = ();
23             our @EXPORT_OK = ('get_contract_pool');
24              
25             #---------------------------------------------------------------
26             #
27             # A singleton pattern with lazy initialization and embedded constructor
28             #
29              
30             my $pool;
31              
32             sub get_contract_pool {
33 24 100   24 1 178 if (!defined $pool) {
34 22         98 $pool = bless({},__PACKAGE__);
35 22         176 $pool->_contract_index({});
36             }
37 24         491 return $pool;
38             }
39              
40             #---------------------------------------------------------------
41             #
42             # list_all_contracts - return all contracts registered in the pool
43             #
44              
45             sub list_all_contracts {
46 21     21 1 1829 my $self = shift;
47 21         31 return values %{$self->_contract_index};
  21         71  
48             }
49              
50             #---------------------------------------------------------------
51             #
52             # has_contract -
53             #
54              
55             # TODO: should it be removed? to use find_contract instead? would it be too slow?
56              
57             sub has_contract {
58 151     151 1 42932 my ($self, $contractor) = @_;
59              
60 151 100 66     3753 croak "method has_contract() expects a fully qualified function name as argument"
      66        
      66        
61             if ( scalar @_ != 2 ||
62             !defined $contractor ||
63             ref $contractor ne '' ||
64             $contractor !~ /::/
65             );
66              
67 144         516 my $index = $self->_contract_index;
68 144         1574 return exists $index->{$contractor};
69             }
70              
71             #---------------------------------------------------------------
72             #
73             # _add_contract
74             #
75              
76             sub _add_contract {
77 49     49   107 my ($self, $contract) = @_;
78              
79 49 50       216 croak "method add_contract() expects only 1 argument"
80             if (scalar @_ != 2);
81 49 50 33     422 croak "method add_contract() expects an instance of Sub::contract as argument"
82             if (!defined $contract || ref $contract ne 'Sub::Contract');
83              
84 49         255 my $index = $self->_contract_index;
85 49         343 my $contractor = $contract->contractor;
86              
87 49 50       146 croak "trying to contract function [$contractor] twice"
88             if ($self->has_contract($contractor));
89              
90 49         148 $index->{$contractor} = $contract;
91              
92 49         107 return $self;
93             }
94              
95             ################################################################
96             #
97             #
98             # Operations on contracts during runtime
99             #
100             #
101             ################################################################
102              
103             sub enable_all_contracts {
104 1     1 1 614 my $self = shift;
105 1         4 map { $_->enable } $self->list_all_contracts;
  4         30  
106             }
107              
108             sub disable_all_contracts {
109 1     1 1 712 my $self = shift;
110 1         5 map { $_->disable } $self->list_all_contracts;
  4         22  
111             }
112              
113             sub enable_contracts_matching {
114 0     0 1 0 my $self = shift;
115 0         0 map { $_->enable } $self->find_contracts_matching(@_);
  0         0  
116             }
117              
118             sub disable_contracts_matching {
119 0     0 1 0 my $self = shift;
120 0         0 map { $_->disable } $self->find_contracts_matching(@_);
  0         0  
121             }
122              
123             sub find_contract {
124 50     50 1 2830 my ($self, $contractor) = @_;
125              
126 50 100 33     1211 croak "method find_contract() expects a fully qualified function name as argument"
      33        
      66        
127             if ( scalar @_ != 2 ||
128             !defined $contractor ||
129             ref $contractor ne '' ||
130             $contractor !~ /::/
131             );
132              
133 45         164 my $index = $self->_contract_index;
134 45         373 return $index->{$contractor};
135             }
136              
137             sub find_contracts_matching {
138 10     10 1 5050 my $self = shift;
139 10         15 my $match = shift;
140 10         12 my @contracts;
141              
142             # TODO: fix croak level when called from enable/disable_matching
143             # local $Carp::CarpLevel = 2 if ((caller(1))[3] =~ /^Sub::Contract::Pool::(enable|disable)_contracts_matching$/);
144              
145 10 50 33     91 croak "method find_contracts_matching() expects a regular expression"
      33        
146             if (scalar @_ != 0 || !defined $match || ref $match ne '');
147              
148 10         13 while ( my ($name,$contract) = each %{$self->_contract_index} ) {
  50         126  
149 40 100       553 push @contracts, $contract if ($name =~ /^$match$/);
150             }
151              
152 10         176 return @contracts;
153             }
154              
155             1;
156              
157             __END__