File Coverage

blib/lib/Alien/Taco/Object.pm
Criterion Covered Total %
statement 23 23 100.0
branch n/a
condition n/a
subroutine 10 10 100.0
pod 5 6 83.3
total 38 39 97.4


line stmt bran cond sub pod time code
1             # Taco Perl object module.
2             # Copyright (C) 2013-2014 Graham Bell
3             #
4             # This program is free software: you can redistribute it and/or modify
5             # it under the terms of the GNU General Public License as published by
6             # the Free Software Foundation, either version 3 of the License, or
7             # (at your option) any later version.
8             #
9             # This program is distributed in the hope that it will be useful,
10             # but WITHOUT ANY WARRANTY; without even the implied warranty of
11             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12             # GNU General Public License for more details.
13             #
14             # You should have received a copy of the GNU General Public License
15             # along with this program. If not, see <http://www.gnu.org/licenses/>.
16              
17             =head1 NAME
18              
19             Alien::Taco::Object - Taco Perl object module
20              
21             =head1 SYNOPSIS
22              
23             my $obj = $taco->construct_object('ClassName', args => [...]);
24              
25             my $result = $obj->call_method('method_name', args => [...]);
26             $obj->set_attribute('attribute_name', $value);
27             my $value = $obj->get_attribute('attribute_name');
28              
29             =head1 DESCRIPTION
30              
31             This class is used to represent objects through by Taco actions.
32             Instances of this class will returned by methods of L<Alien::Taco>
33             objects and should not normally be constructed explicitly.
34              
35             The objects reside on the server side and are referred to by instances
36             of this class by their object number. When these instances are
37             destroyed the I<destroy_object> action is sent automatically.
38              
39             =cut
40              
41             package Alien::Taco::Object;
42              
43 4     4   108388 use strict;
  4         21  
  4         1535  
44              
45             our $VERSION = '0.003';
46              
47             # new($taco_client, $object_number)
48             #
49             # Constructs a new instance of this class. A reference to the Taco client
50             # is stored to allow actions to be sent via it.
51              
52             sub new {
53 6     6 0 2372 my $class = shift;
54 6         13 my $client = shift;
55 6         12 my $number = shift;
56              
57 6         29 return bless {client => $client, number => $number}, $class;
58             }
59              
60             # DESTROY
61             #
62             # Destructor method. This invokes the _destroy_object method of
63             # the Taco client so that the object on the server side can be deleted.
64              
65             sub DESTROY {
66 6     6   3369 my $self = shift;
67              
68 6         34 $self->{'client'}->_destroy_object($self->{'number'});
69             }
70              
71             =head1 METHODS
72              
73             =head2 Taco Methods
74              
75             =over 4
76              
77             =item call_method('method_name', [args => \@args], [kwargs => \%kwargs])
78              
79             Invoke the given method on the object with the specified arguments
80             and keyword arguments. The context (void / scalar / list) of this method
81             call is detected and sent as an action parameter.
82              
83             =cut
84              
85             sub call_method {
86 3     3 1 816 my $self = shift;
87              
88             # Invoke this directly by the return command to
89             # allow _call_method to detect the context.
90 3         24 return $self->{'client'}->_call_method($self->{'number'}, @_);
91             }
92              
93             =item get_attribute('attribute_name')
94              
95             Retrieve the value of the given attribute.
96              
97             =cut
98              
99             sub get_attribute {
100 2     2 1 808 my $self = shift;
101              
102 2         15 return $self->{'client'}->_get_attribute($self->{'number'}, @_);
103             }
104              
105             # _number()
106             #
107             # Returns the object number identifying this object on the server side.
108             # This is an internal method for use by L<Alien::Taco> when sending
109             # a reference to this object as an action parameter.
110              
111             sub _number {
112 2     2   1613 my $self = shift;
113              
114 2         11 return $self->{'number'};
115             }
116              
117             =item set_attribute('attribute_name', $value)
118              
119             Set the value of the given attribute.
120              
121             =cut
122              
123             sub set_attribute {
124 2     2 1 1567 my $self = shift;
125              
126 2         14 $self->{'client'}->_set_attribute($self->{'number'}, @_);
127             }
128              
129             =back
130              
131             =head2 Convenience Methods
132              
133             =over 4
134              
135             =item method('method_name')
136              
137             Return a subroutine reference which calls the given function
138             with plain arguments only. For example:
139              
140             my $ymd = $afd->method('ymd');
141             print $ymd->('/'), "\n";
142              
143             =cut
144              
145             sub method {
146 1     1 1 555 my $self = shift;
147 1         3 my $name = shift;
148              
149             return sub {
150 1     1   378 return $self->call_method($name, args => \@_);
151 1         5 };
152             }
153              
154             =back
155              
156             =head2 JSON Methods
157              
158             =over 4
159              
160             =item TO_JSON
161              
162             This method will be called by the JSON encoder to convert the object
163             to a hashref which is encodable as JSON.
164              
165             =cut
166              
167             sub TO_JSON {
168 1     1 1 2 my $self = shift;
169 1         9 return {_Taco_Object_ => $self->{'number'}};
170             }
171              
172             1;
173              
174             __END__
175              
176             =back
177              
178             =cut