File Coverage

blib/lib/Class/Adapter.pm
Criterion Covered Total %
statement 20 21 95.2
branch 4 6 66.6
condition n/a
subroutine 8 8 100.0
pod 1 1 100.0
total 33 36 91.6


line stmt bran cond sub pod time code
1             package Class::Adapter;
2              
3             =pod
4              
5             =head1 NAME
6              
7             Class::Adapter - Perl implementation of the "Adapter" Design Pattern
8              
9             =head1 DESCRIPTION
10              
11             The C class is intended as an abstract base class for
12             creating any sort of class or object that follows the I pattern.
13              
14             =head2 What is an Adapter?
15              
16             The term I refers to a I<"Design Pattern"> of the same name,
17             from the famous I<"Gang of Four"> book I<"Design Patterns">. Although
18             their original implementation was designed for Java and similar
19             single-inheritance strictly-typed langauge, the situation for which it
20             applies is still valid.
21              
22             An I in this Perl sense of the term is when a class is created
23             to achieve by composition (objects containing other object) something that
24             can't be achieved by inheritance (sub-classing).
25              
26             This is similar to the I pattern, but is intended to be
27             applied on a class-by-class basis, as opposed to being able to be applied
28             one object at a time, as is the case with the I pattern.
29              
30             The C object holds a parent object that it "wraps",
31             and when a method is called on the C, it manually
32             calls the same (or different) method with the same (or different)
33             parameters on the parent object contained within it.
34              
35             Instead of these custom methods being hooked in on an object-by-object
36             basis, they are defined at the class level.
37              
38             Basically, a C is one of your fall-back positions
39             when Perl's inheritance model fails you, or is no longer good enough,
40             and you need to do something twisty in order to make several APIs play
41             nicely with each other.
42              
43             =head2 What can I do with the actual Class::Adapter class
44              
45             Well... nothing really. It exist to provide some extremely low level
46             fundamental methods, and to provide a common base for inheritance of
47             Adapter classes.
48              
49             The base C class doesn't even implement a way to push
50             method calls through to the underlying object, since the way in which
51             B happens is the bit that changes from case to case.
52              
53             To actually DO something, you probably want to go take a look at
54             L, which makes the creation of I
55             classes relatively quick and easy.
56              
57             =head1 METHODS
58              
59             The C class itself supplies only the two most common
60             methods, a default constructor and a private method to access the
61             underlying object.
62              
63             =cut
64              
65 7     7   70531 use 5.005;
  7         162  
  7         294  
66 7     7   36 use strict;
  7         14  
  7         389  
67 7     7   46 use Carp ();
  7         11  
  7         137  
68 7     7   39 use Scalar::Util 1.10 ();
  7         195  
  7         160  
69              
70 7     7   35 use vars qw{$VERSION};
  7         15  
  7         460  
71             BEGIN {
72 7     7   1046 $VERSION = '1.08';
73             }
74              
75              
76              
77              
78              
79             #####################################################################
80             # Constructor
81              
82             =pod
83              
84             =head2 new $object
85              
86             The default C constructor takes a single object as argument and
87             creates a new object which holds the passed object.
88              
89             Returns a new C object, or C if you do not pass
90             in an object.
91              
92             =cut
93              
94             sub new {
95 17 50   17 1 6895 my $class = ref $_[0] ? ref shift : shift;
96 17 100       93 my $object = Scalar::Util::blessed($_[0]) ? shift : return undef;
97 8         39 return bless { OBJECT => $object }, $class;
98             }
99              
100              
101              
102              
103              
104             #####################################################################
105             # Private Methods
106              
107             =pod
108              
109             =head2 _OBJECT_
110              
111             The C<_OBJECT_> method is provided primarily as a convenience, and a tool
112             for people implementing sub-classes, and allows the C
113             interface to provide a guarenteed correct way of getting to the underlying
114             object, should you need to do so.
115              
116             =cut
117              
118             sub _OBJECT_ {
119 19 50   19   1227 return $_[0]->{OBJECT} if ref $_[0];
120 0           Carp::croak('Class::Adapter::_OBJECT_ called as a static method');
121             }
122              
123             1;
124              
125             =pod
126              
127             =head1 SUPPORT
128              
129             Bugs should be reported via the CPAN bug tracker at
130              
131             L
132              
133             For other issues, contact the author.
134              
135             =head1 TO DO
136              
137             - Write more comprehensive tests
138              
139             =head1 AUTHOR
140              
141             Adam Kennedy Eadamk@cpan.orgE
142              
143             =head1 SEE ALSO
144              
145             L, L, L
146              
147             =head1 COPYRIGHT
148              
149             Copyright 2005 - 2011 Adam Kennedy.
150              
151             This program is free software; you can redistribute
152             it and/or modify it under the same terms as Perl itself.
153              
154             The full text of the license can be found in the
155             LICENSE file included with this module.
156              
157             =cut