lib/Class/Facade.pm

filter: Statementstmt 97.5% 39 / 40 Branchbran 87.5% 7 / 8 Conditioncond 77.7% 7 / 9 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 91.4% 64 / 70 CC 9 SCAR 17.1
CC
9
Cov
93%
init
9.1
BEGIN
1.0
BEGIN
1.0
CRAP 5.5
↑ Summary
1 #============================================================= -*-perl-*-
2 #
3 # Class::Facade
4 #
5 # DESCRIPTION
6 # Facade class for providing a unified interface to one or more
7 # delegate objects.
8 #
9 # AUTHOR
10 # Andy Wardley <abw@kfs.org>
11 #
12 # COPYRIGHT
13 # Copyright (C) 2001-2002 Andy Wardley. All Rights Reserved.
14 #
15 # This module is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
17 #
18 # REVISION
19 # $Id$
20 #
21 #========================================================================
22  
23 package Class::Facade;
24  
25 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Class::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use base qw( Class::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 our $VERSION = '0.01';
30 1 our $REVISION = sprintf("%d.%02d", q$Revision$ =~ /(\d+)\.(\d+)/);
31 1 our $ERROR;
32  
33  
34 #------------------------------------------------------------------------
35 # init()
36 #------------------------------------------------------------------------
37  
38 4 ▶sub init {
Subroutine init: called CC=9 SCAR=22.0
Pod documented
39 4 my ($self, $config) = @_;
40 4 my $class = ref $self;
41  
42 4 ▶ while (my ($name, $value) = each %$config) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$config) 14 2 16
43 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
44 14 my $type = ref $value;
45
46 14 ▶ if ($type eq 'CODE') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'CODE') { } 2 12 14
elsif ($type eq 'ARRAY') { } 6 6 12
elsif ($type eq 'HASH') { } 6 0 6
47 2 my $coderef = $value;
48 2 ▶ *{"$class\::$name"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
49 4 my $facade = shift;
50 4 &$coderef(@_);
51 2 };
52 }
53 elsif ($type eq 'ARRAY') {
54 6 my ($object, $method, @args) = @$value;
55 6 ▶ *{"$class\::$name"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
56 8 my $facade = shift;
57 8 $object->$method(@args, @_);
58 6 };
59 }
60 elsif ($type eq 'HASH') {
61 my $object = $value->{ class }
62 || $value->{ object }
63 6 ▶ || return $self->error("$name: no 'class' or 'object' specified");
Condition100%5/5
$value->{'class'} || $value->{'object'} || (return $self->error("${name}: no 'class' or 'object' specified"))
l!l
42
$value->{"class"} or $value->{"object"}
l!l&&r!l&&!r
222
MC/DC100%2/2
$value->{'class'} || $value->{'object'} || (return $self->error("${name}: no 'class' or 'object' specified"))
$value->{"class"} $value->{"object"}
Truth table100%3/3
A or B
A= $value->{"class"}B= $value->{"object"}
ABresult
1X1
011
000
64 my $method = $value->{ method }
65 4 ▶ || return $self->error("$name: no 'method' specified");
Condition50%1/2
$value->{'method'} || (return $self->error("${name}: no 'method' specified"))
l!l
40
MC/DC0%0/1
$value->{'method'} || (return $self->error("${name}: no 'method' specified"))
$value->{'method'}
Truth table50%1/2
A
A= $value->{'method'}
Aresult
11
00
66 4 ▶ my $args = $value->{ args } || [ ];
Condition50%1/2
$value->{'args'} || []
l!l
40
MC/DC0%0/1
$value->{'args'} || []
$value->{'args'}
Truth table50%1/2
A
A= $value->{'args'}
Aresult
11
00
67  
68 4 ▶ *{"$class\::$name"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
69 8 my $facade = shift;
70 8 $object->$method(@$args, @_);
71 4 };
72 }
73 else {
74 0 return $self->error("$name: invalid delegate specification");
75 }
76 }
77  
78 2 return $self;
79 }
80  
81  
82 1 1;
83  
84  
85 =head1 NAME
86  
87 Class::Facade - interface to one or more delegates
88  
89 =head1 SYNOPSIS
90  
91 use Class::Facade;
92  
93 my $facade = Class::Facade->new({
94 method1 => sub { ... },
95 method2 => [ $class, $method, $arg1, $arg2, ... ],
96 method3 => [ $object, $method, $arg1, $arg2, ... ],
97 method4 => {
98 class => 'My::Delegate::Class',
99 method => 'method_name',
100 args => [ $arg1, $arg2, ... ],
101 },
102 method5 => {
103 object => $object,
104 method => 'method_name',
105 args => [ $arg1, $arg2, ... ],
106 },
107 });
108  
109 $facade->method1($more_args1, ...);
110 $facade->method2($more_args2, ...);
111 # ...etc...
112  
113 =head1 DESCRIPTION
114  
115 This module implements a simple facade class, allowing you to create
116 objects that delegate their methods to subroutines or other object or
117 class methods.
118  
119 To create a delegate object, simply call the new() constructor passing a
120 reference to a hash array describing the methods and their delegates.
121 Each key in the hash specifies a method name for your facade object.
122 Each value specifies the delegate target and should be a reference to
123 a subroutine, list or hash array.
124  
125 In the case of a list, the elements in the list should be a class name
126 or object reference followed by a method name and a list of any
127 arguments that you want passed to the method when it is called. Any
128 additional arguments that the caller of the facade method specifies will
129 also be passed.
130  
131 In the case of a hash, the C<class> or C<object> element specifies a
132 class name or object references, the C<method> element names the
133 class/object method to be called and C<args> is an optional reference
134 to a list of arguments as above.
135  
136 The Class::Facade constructor creates accessor methods in the module's
137 symbol table. One important side effect of this is that all methods
138 defined will be created for all object of the same class. For this
139 reason it is recommended that you create your own facade modules which
140 are subclass from Class::Facade.
141  
142 package My::Facade::One;
143 use base qw( Class::Facade );
144  
145 package My::Facade::Two;
146 use base qw( Class::Facade );
147  
148 package main;
149 my $one = My::Facade::One->new({ ... });
150 my $two = My::Facade::Two->new({ ... });
151  
152 =head1 AUTHOR
153  
154 Andy Wardley E<lt>abw@kfs.orgE<gt>
155  
156 =head1 COPYRIGHT
157  
158 Copyright (C) 2001-2002 Andy Wardley. All Rights Reserved.
159  
160 This module is free software; you can redistribute it and/or
161 modify it under the same terms as Perl itself.
162  
163 =cut
↑ Summary