File Coverage

blib/lib/JOAP/Proxy/Instance.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             # JOAP::Proxy::Instance.pm - class for Instances
2             #
3             # Copyright (c) {$YEAR}, {$NAME} {$EMAIL}.
4             #
5             # This library is free software; you can redistribute it and/or
6             # modify it under the terms of the GNU Lesser General Public
7             # License as published by the Free Software Foundation; either
8             # version 2.1 of the License, or (at your option) any later version.
9             #
10             # This library is distributed in the hope that it will be useful,
11             # but WITHOUT ANY WARRANTY; without even the implied warranty of
12             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13             # Lesser General Public License for more details.
14             #
15             # You should have received a copy of the GNU Lesser General Public
16             # License along with this library; if not, write to the Free Software
17             # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18              
19             # tag: JOAP class proxy instance class
20              
21             package JOAP::Proxy::Instance;
22 1     1   2249 use JOAP::Proxy;
  0            
  0            
23             use base qw/JOAP::Proxy/;
24              
25             use 5.008;
26             use strict;
27             use warnings;
28              
29             our %EXPORT_TAGS = ( 'all' => [ qw// ] );
30             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
31             our @EXPORT = qw//;
32              
33             our $VERSION = $JOAP::VERSION;
34             our $AUTOLOAD;
35              
36             sub get {
37              
38             my $proto = shift;
39             my $pkg = ref($proto) || $proto;
40             my $address = shift;
41             my $self = bless({_address => $address}, $pkg);
42              
43             my %named = @_;
44              
45             # If these are passed in, we don't have to do a describe.
46              
47             if (defined $named{methods} &&
48             defined $named{attributes} &&
49             defined $named{superclasses} &&
50             defined $named{timestamp} &&
51             defined $named{description})
52             {
53             $self->methods($named{methods});
54             $self->attributes($named{attributes});
55             $self->superclasses($named{superclasses});
56             $self->_set_timestamp($named{timestamp});
57             $self->_set_description($named{description});
58             }
59              
60             $self->_read();
61              
62             return $self;
63             }
64              
65             sub superclasses {
66             my $self = shift;
67             return (@_) ? $self->{_superclasses} = shift : $self->{_superclasses};
68             }
69              
70             sub _describe {
71             my $self = shift;
72             my $resp = $self->SUPER::_describe(@_);
73              
74             my @classes = $resp->GetQuery->GetSuperclass;
75             $self->superclasses(\@classes);
76              
77             return $resp;
78             }
79              
80             sub _edit {
81              
82             my $self = shift;
83             my $resp = $self->SUPER::_edit(@_);
84             my $edit = $resp->GetQuery;
85              
86             if (ref($self) && $edit->DefinedNewAddress) {
87             $self->address($edit->GetNewAddress);
88             }
89              
90             return $resp;
91             }
92              
93             sub _default_edit_attrs {
94              
95             my $self = shift;
96              
97             # This _should_ return only writable attributes
98              
99             my $attrs = $self->SUPER::_default_edit_attrs;
100              
101             my @right_alloc = grep { $attrs->{$_}->{allocation} ne 'class' } keys %$attrs;
102              
103             # make that into a hash
104              
105             my %write = map {($_, $attrs->{$_})} @right_alloc;
106              
107             # return a reference to that hash
108              
109             return \%write;
110             }
111              
112             sub delete {
113              
114             my $self = shift;
115              
116             my $con = $self->Connection || throw JOAP::Proxy::Error::Local("Can't delete without a connection.");
117              
118             my $iq = new Net::Jabber::IQ();
119              
120             $iq->SetIQ(to => $self->address, type => 'set');
121             $iq->NewQuery($JOAP::NS, 'delete');
122              
123             my $resp = $con->SendAndReceiveWithID($iq);
124              
125             if ($resp->GetType eq 'error') {
126             throw JOAP::Proxy::Error::Remote(value => $resp->GetErrorCode, text => $resp->GetError);
127             }
128              
129             return 1;
130             }
131              
132             sub can {
133             my $self = shift;
134             my $name = shift;
135             my $func = $self->UNIVERSAL::can($name); # See if it's findable by standard lookup.
136              
137             if (!$func) { # if not, see if it's something we should make ourselves.
138             my $methdesc = $self->_method_descriptor($name);
139              
140             if ($methdesc && $methdesc->{allocation} ne 'class') {
141             $func = $self->_proxy_method($methdesc);
142             } else {
143             my $attrdesc = $self->_attribute_descriptor($name);
144             if ($attrdesc && $attrdesc->{allocation} ne 'class') {
145             $func = $self->_proxy_accessor($attrdesc);
146             }
147             }
148             }
149              
150             return $func;
151             }
152              
153             1; # don't forget to return a true value from the file
154              
155             __END__