File Coverage

blib/lib/Message/Passing/Role/HasAConnection.pm
Criterion Covered Total %
statement 13 21 61.9
branch 0 2 0.0
condition 0 3 0.0
subroutine 5 6 83.3
pod 1 1 100.0
total 19 33 57.5


line stmt bran cond sub pod time code
1             package Message::Passing::Role::HasAConnection;
2 2     2   17763 use Moo::Role;
  2         40409  
  2         13  
3 2     2   634 use Module::Runtime qw/ require_module /;
  2         4  
  2         19  
4 2     2   95 use Carp qw/ confess /;
  2         2  
  2         121  
5 2     2   1107 use namespace::clean -except => 'meta';
  2         20510  
  2         19  
6              
7             with qw/
8             Message::Passing::Role::HasTimeoutAndReconnectAfter
9             Message::Passing::Role::HasErrorChain
10             /;
11              
12             # requires qw/ _connection_manager_attributes _connection_manager_class /;
13             requires 'connected';
14              
15             has connection_manager => (
16             is => 'ro',
17             lazy => 1,
18             # isa => duck_type([qw/subscribe_to_connect/]),
19             builder => '_build_connection_manager',
20             );
21              
22             sub _build_connection_manager {
23 0     0   0 my $self = shift;
24 0 0 0     0 confess "Cannot auto-build this connection manager"
25             unless $self->can('_connection_manager_attributes')
26             && $self->can('_connection_manager_class');
27 0         0 my %attrs = map { $_ => $self->$_ } (@{ $self->_connection_manager_attributes }, qw/timeout reconnect_after error/);
  0         0  
  0         0  
28 0         0 my $class = $self->_connection_manager_class;
29 0         0 require_module($class);
30 0         0 $class->new(%attrs);
31             }
32              
33 1     1 1 61 sub BUILD {}
34             after BUILD => sub {
35             my $self = shift;
36             $self->connection_manager->subscribe_to_connect($self);
37             };
38              
39             1;
40              
41             =head1 NAME
42              
43             Message::Passing::Role::HasAConnection - Role for components which have a connection
44              
45             =head1 DESCRIPTION
46              
47             Provides a standard ->connection_manager attribute for inputs or outputs which need to
48             make a network connection before they can send or receive messages.
49              
50             The connection manager object is assumed to have the C<< ->subscribe_to_connect >> method
51             (from L).
52              
53             =head1 REQUIRED METHODS
54              
55             =head2 _build_connection_manager
56              
57             Will be called at BUILD (i.e. object construction) time, should return
58             a connection manager object (i.e. an object that C<< ->subscribe_to_connect >>
59             can be called on).
60              
61             =head2 connected ($client)
62              
63             Called by the connection manager when a connection is made.
64              
65             Usually used to do things like subscribe to queues..
66              
67             =head1 OPTIONAL METHODS
68              
69             =head2 disconnected ($client)
70              
71             The client received an error or otherwise disconnected.
72              
73             =head1 ATTRIBUTES
74              
75             =head2 connection_manager
76              
77             Holds the connection manger returned by the C<_build_connection_manager> method.
78              
79             =head1 WRAPPED METHODS
80              
81             =head2 BUILD
82              
83             Is wrapped to build the connection manager object.
84              
85             =head1 SEE ALSO
86              
87             =over
88              
89             =item L.
90              
91             =back
92              
93             =head1 SPONSORSHIP
94              
95             This module exists due to the wonderful people at Suretec Systems Ltd.
96             who sponsored its development for its
97             VoIP division called SureVoIP for use with
98             the SureVoIP API -
99            
100              
101             =head1 AUTHOR, COPYRIGHT AND LICENSE
102              
103             See L.
104              
105             =cut