File Coverage

blib/lib/Net/AMQP/Protocol/Base.pm
Criterion Covered Total %
statement 13 58 22.4
branch 0 14 0.0
condition 0 11 0.0
subroutine 5 9 55.5
pod 2 4 50.0
total 20 96 20.8


line stmt bran cond sub pod time code
1             package Net::AMQP::Protocol::Base;
2              
3             =head1 NAME
4              
5             Net::AMQP::Protocol::Base - Base class of auto-generated protocol classes
6              
7             =head1 DESCRIPTION
8              
9             See L for how subclasses to this class are auto-generated.
10              
11             =cut
12              
13 5     5   24 use strict;
  5         10  
  5         179  
14 5     5   23 use warnings;
  5         9  
  5         211  
15 5     5   23 use base qw(Class::Data::Inheritable Class::Accessor::Fast);
  5         8  
  5         4966  
16              
17             BEGIN {
18 5     5   29937 __PACKAGE__->mk_classdata($_) foreach qw(
19             class_id
20             method_id
21             frame_arguments
22             class_spec
23             method_spec
24             );
25             }
26              
27             =head1 CLASS METHODS
28              
29             =head2 class_id
30              
31             The class id from the specficiation.
32              
33             =head2 method_id
34              
35             The method id from the specification. In the case of a content (such as Basic, File or Stream), method_id is 0 for the virtual ContentHeader method. This allows you to create a Header frame in much the same way you create a Method frame, but with the virtual method 'ContentHeader'. For example:
36              
37             my $header_frame = Net::AMQP::Protocol::Basic::ContentHeader->new(
38             content_type => 'text/html'
39             );
40              
41             print $header_frame->method_id(); # prints '0'
42              
43             =head2 frame_arguments
44              
45             Contains an ordered arrayref of the fields that comprise a frame for this method. For example:
46              
47             Net::AMQP::Protocol::Channel::Open->frame_arguments([
48             out_of_band => 'short_string'
49             ]);
50              
51             This is used by the L subclasses to (de)serialize raw binary data. Each of these fields are also an accessor for the class objects.
52              
53             =head2 class_spec
54              
55             Contains the hashref that the C call generated for this class.
56              
57             =head2 method_spec
58              
59             Same as above, but for this method.
60              
61             =back
62              
63             =cut
64              
65             sub new {
66 0     0 1   my ($class, %self) = @_;
67              
68 0           return bless \%self, $class;
69             }
70              
71             sub register {
72 0     0 0   my $class = shift;
73              
74             # Inform the Frame::Method class of the existance of this method type
75 0 0 0       if ($class->class_id && $class->method_id) {
    0 0        
76 0           Net::AMQP::Frame::Method->register_method_class($class);
77             }
78             elsif ($class->class_id && ! $class->method_id) {
79 0           Net::AMQP::Frame::Header->register_header_class($class);
80             }
81              
82             # Create accessor methods in the subclass for frame data
83 0           my @accessors;
84 0           my $arguments = $class->frame_arguments;
85 0           for (my $i = 0; $i <= $#{ $arguments }; $i += 2) {
  0            
86 0           my ($key, $type) = ($arguments->[$i], $arguments->[$i + 1]);
87 0           push @accessors, $key;
88             }
89 0           $class->mk_accessors(@accessors);
90             }
91              
92             =head1 OBJECT METHODS
93              
94             =head2 frame_wrap
95              
96             Returns a L subclass object that wraps the given object, if possible.
97              
98             =cut
99              
100             sub frame_wrap {
101 0     0 1   my $self = shift;
102              
103 0 0 0       if ($self->class_id && $self->method_id) {
    0          
104 0           return Net::AMQP::Frame::Method->new( method_frame => $self );
105             }
106             elsif ($self->class_id) {
107 0           return Net::AMQP::Frame::Header->new( header_frame => $self );
108             }
109             else {
110 0           return $self;
111             }
112             }
113              
114             sub docs_as_pod {
115 0     0 0   my $class = shift;
116 0           my $package = __PACKAGE__;
117              
118 0           my $class_spec = $class->class_spec;
119 0           my $method_spec = $class->method_spec;
120 0           my $frame_arguments = $class->frame_arguments;
121            
122 0           my $description = "This is an auto-generated subclass of L<$package>; see the docs for that module for inherited methods. Check the L below for details on the auto-generated methods within this class.\n";
123              
124 0 0         if ($class->method_id == 0) {
125 0           my $base_class = 'Net::AMQP::Protocol::' . $class_spec->{name};
126 0           $description .= "\n" . <
127             This class is not a real class of the AMQP spec. Instead, it's a helper class that allows you to create L objects for L<$base_class> frames.
128             EOF
129             }
130             else {
131 0 0         $description .= "\n" . "This class implements the class B<$$class_spec{name}> (id ".$class->class_id.") method B<$$method_spec{name}> (id ".$class->method_id."), which is ".($method_spec->{synchronous} ? 'a synchronous' : 'an asynchronous')." method\n";
132             }
133              
134 0           my $synopsis_new_args = '';
135 0           my $usage = <
136             =head2 Fields and Accessors
137              
138             Each of the following represents a field in the specification. These are the optional arguments to B and are also read/write accessors.
139              
140             =over
141              
142             EOF
143              
144 5     5   9451 use Data::Dumper;
  5         54609  
  5         1774  
145             #$usage .= Dumper($method_spec);
146              
147 0           foreach my $field_spec (@{ $method_spec->{fields} }) {
  0            
148 0           my $type = $field_spec->{type}; # may be 'undef'
149 0 0         if ($field_spec->{domain}) {
150 0           $type = $Net::AMQP::Protocol::spec{domain}{ $field_spec->{domain} }{type};
151             }
152              
153 0           my $local_name = $field_spec->{name};
154 0           $local_name =~ s{ }{_}g;
155              
156 0   0       $field_spec->{doc} ||= '';
157              
158 0           $usage .= <
159             =item I<$local_name> (type: $type)
160              
161             $$field_spec{doc}
162              
163             EOF
164              
165 0           $synopsis_new_args .= <
166             $local_name => \$$local_name,
167             EOF
168             }
169              
170 0           chomp $synopsis_new_args; # trailing \n
171              
172 0           $usage .= "=back\n\n";
173              
174              
175 0           my $pod = <
176             =pod
177              
178             =head1 NAME
179              
180             $class - An auto-generated subclass of $package
181              
182             =head1 SYNOPSIS
183              
184             use $class;
185              
186             my \$object = $class\->new(
187             $synopsis_new_args
188             );
189              
190             =head1 DESCRIPTION
191              
192             $description
193              
194             =head1 USAGE
195              
196             $usage
197              
198             =head1 SEE ALSO
199              
200             L<$package>
201              
202             EOF
203              
204 0           $pod =~ s{^ =}{=}gms;
205              
206 0           return $pod;
207             }
208              
209             =head1 SEE ALSO
210              
211             L
212              
213             =head1 COPYRIGHT
214              
215             Copyright (c) 2009 Eric Waters and XMission LLC (http://www.xmission.com/). All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
216              
217             The full text of the license can be found in the LICENSE file included with this module.
218              
219             =head1 AUTHOR
220              
221             Eric Waters
222              
223             =cut
224              
225             1;