File: Base.pm

package info (click to toggle)
libnet-amqp-perl 0.06~dfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 304 kB
  • sloc: perl: 933; makefile: 10
file content (223 lines) | stat: -rw-r--r-- 5,485 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package Net::AMQP::Protocol::Base;

=head1 NAME

Net::AMQP::Protocol::Base - Base class of auto-generated protocol classes

=head1 DESCRIPTION

See L<Net::AMQP::Protocol/load_xml_spec> for how subclasses to this class are auto-generated.

=cut

use strict;
use warnings;
use base qw(Class::Data::Inheritable Class::Accessor::Fast);

BEGIN {
    __PACKAGE__->mk_classdata($_) foreach qw(
        class_id
        method_id
        frame_arguments
        class_spec
        method_spec
    );
}

=head1 CLASS METHODS

=head2 class_id

The class id from the specficiation.

=head2 method_id

The method id from the specification.  In the case of a content <class> (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:

  my $header_frame = Net::AMQP::Protocol::Basic::ContentHeader->new(
    content_type => 'text/html'
  );

  print $header_frame->method_id(); # prints '0'

=head2 frame_arguments

Contains an ordered arrayref of the fields that comprise a frame for this method.  For example:

  Net::AMQP::Protocol::Channel::Open->frame_arguments([
      out_of_band => 'short_string'
  ]);

This is used by the L<Net::AMQP::Frame> subclasses to (de)serialize raw binary data.  Each of these fields are also an accessor for the class objects.

=head2 class_spec

Contains the hashref that the C<load_xml_spec()> call generated for this class.

=head2 method_spec

Same as above, but for this method.

=cut

sub new {
    my ($class, %self) = @_;

    return bless \%self, $class;
}

sub register {
    my $class = shift;

    # Inform the Frame::Method class of the existance of this method type
    if ($class->class_id && $class->method_id) {
        Net::AMQP::Frame::Method->register_method_class($class);
    }
    elsif ($class->class_id && ! $class->method_id) {
        Net::AMQP::Frame::Header->register_header_class($class);
    }

    # Create accessor methods in the subclass for frame data
    my @accessors;
    my $arguments = $class->frame_arguments;
    for (my $i = 0; $i <= $#{ $arguments }; $i += 2) {
        my ($key, $type) = ($arguments->[$i], $arguments->[$i + 1]);
        push @accessors, $key;
    }
    $class->mk_accessors(@accessors);
}

=head1 OBJECT METHODS

=head2 frame_wrap

Returns a L<Net::AMQP::Frame> subclass object that wraps the given object, if possible.

=cut

sub frame_wrap {
    my $self = shift;

    if ($self->class_id && $self->method_id) {
        return Net::AMQP::Frame::Method->new( method_frame => $self );
    }
    elsif ($self->class_id) {
        return Net::AMQP::Frame::Header->new( header_frame => $self );
    }
    else {
        return $self;
    }
}

sub docs_as_pod {
    my $class = shift;
    my $package = __PACKAGE__;

    my $class_spec = $class->class_spec;
    my $method_spec = $class->method_spec;
    my $frame_arguments = $class->frame_arguments;
    
    my $description = "This is an auto-generated subclass of L<$package>; see the docs for that module for inherited methods.  Check the L</USAGE> below for details on the auto-generated methods within this class.\n";

    if ($class->method_id == 0) {
        my $base_class = 'Net::AMQP::Protocol::' . $class_spec->{name};
        $description .= "\n" . <<EOF;
This class is not a real class of the AMQP spec.  Instead, it's a helper class that allows you to create L<Net::AMQP::Frame::Header> objects for L<$base_class> frames.
EOF
    }
    else {
        $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";
    }

    my $synopsis_new_args = '';
    my $usage = <<EOF;
 =head2 Fields and Accessors

Each of the following represents a field in the specification.  These are the optional arguments to B<new()> and are also read/write accessors.

 =over

EOF

    use Data::Dumper;
    #$usage .= Dumper($method_spec);

    foreach my $field_spec (@{ $method_spec->{fields} }) {
        my $type = $field_spec->{type}; # may be 'undef'
        if ($field_spec->{domain}) {
            $type = $Net::AMQP::Protocol::spec{domain}{ $field_spec->{domain} }{type};
        }

        my $local_name = $field_spec->{name};
        $local_name =~ s{ }{_}g;

        $field_spec->{doc} ||= '';

        $usage .= <<EOF;
 =item I<$local_name> (type: $type)

$$field_spec{doc}

EOF

        $synopsis_new_args .= <<EOF;
      $local_name => \$$local_name,
EOF
    }

    chomp $synopsis_new_args; # trailing \n

    $usage .= "=back\n\n";


    my $pod = <<EOF;
 =pod

 =head1 NAME

$class - An auto-generated subclass of $package

 =head1 SYNOPSIS

  use $class;

  my \$object = $class\->new(
$synopsis_new_args
  );

 =head1 DESCRIPTION

$description

 =head1 USAGE

$usage

 =head1 SEE ALSO

L<$package>

EOF

    $pod =~ s{^ =}{=}gms;

    return $pod;
}

=head1 SEE ALSO

L<Net::AMQP::Protocol>

=head1 COPYRIGHT

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.

The full text of the license can be found in the LICENSE file included with this module.

=head1 AUTHOR

Eric Waters <ewaters@gmail.com>

=cut

1;