File Coverage

blib/lib/Object/Meta/Plugin/ExportList.pm
Criterion Covered Total %
statement 29 57 50.8
branch 7 16 43.7
condition n/a
subroutine 8 11 72.7
pod 7 7 100.0
total 51 91 56.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2             # $Id: ExportList.pm,v 1.5 2003/12/10 02:37:24 nothingmuch Exp $
3              
4             package Object::Meta::Plugin::ExportList; # an object representing the skin of a plugin - what can be plugged and unseamed at the top level.
5              
6 2     2   10 use strict;
  2         5  
  2         67  
7 2     2   11 use warnings;
  2         3  
  2         1975  
8              
9             # this is a simple string based Object::Meta::Plugin::Export list. That is, all the methods are strings, and not code refs,
10             # which gives a somewhat more controlled environment.
11              
12             # you could laxen these limits by writing your own ExportList, which will use code refs, and thus allow a plugin to nibble methods from other classes without base classing.
13             # you'd also have to subclass Object::Meta::Plugin::Host to handle coderefs. Perhaps a dualvalue system could be useful.
14              
15             our $VERSION = 0.02;
16              
17             sub new {
18 1     1 1 3 my $pkg = shift;
19 1         2 my $plugin = shift;
20            
21 1 50       64 my $self = bless {
22             plugin => $plugin,
23             info => (ref $_[0] ? shift : Object::Meta::Plugin::ExportList::Info->new()),
24             }, $pkg;
25            
26 1         3 my @methods = @_;
27            
28 1 50       4 if (@methods){
29 0         0 my %list = map { $_, undef } $plugin->exports(); # used to cross out what's not exported
  0         0  
30 0         0 $self->{methods} = [ grep { exists $list{$_} } @methods ]; # filter the method list to be only what works;
  0         0  
31             } else {
32 1         9 $self->{methods} = [ $plugin->exports() ]; # everything unless otherwise stated
33             }
34            
35 1         5 $self;
36             }
37              
38             sub plugin {
39 8     8 1 11 my $self = shift;
40 8         167 $self->{plugin};
41             }
42              
43             sub exists {
44 0     0 1 0 my $self = shift;
45              
46 0 0       0 $self->{index} = { map { $_, undef } @{ $self->{methods} } } unless (exists $self->{index});
  0         0  
  0         0  
47            
48 0 0       0 if (wantarray){ # return a grepped list
49 0         0 return grep { exists $self->{index}{$_} } @_;
  0         0  
50             } else { # return a true or false
51 0         0 return exists $self->{index}{$_[0]};
52             }
53             }
54              
55             sub list { # list all under plugin
56 1     1 1 2 my $self = shift;
57            
58 1         2 return @{ $self->{methods} };
  1         6  
59             }
60              
61             sub merge { # or another exoprt list into this one
62 0     0 1 0 my $self = shift;
63 0         0 my $x = shift;
64            
65 0         0 my %uniq;
66 0         0 @{ $self->{methods} } = grep { not $uniq{$_}++ } @{ $self->{methods} }, $x->list();
  0         0  
  0         0  
  0         0  
67              
68 0         0 $self;
69             }
70              
71             sub unmerge { # and (not|complement) another export list into this one
72 0     0 1 0 my $self = shift;
73 0         0 my $x = shift;
74            
75 0         0 my %seen = map { $_, undef } $x->list();
  0         0  
76 0         0 @{ $self->{methods} } = grep { not exists $seen{$_} } @{ $self->{methods} };
  0         0  
  0         0  
  0         0  
77             }
78              
79             sub info {
80 6     6 1 27 my $self = shift;
81            
82 6 50       12 $self->{info} = shift if (@_);
83            
84 6         34 $self->{info};
85             }
86              
87             package Object::Meta::Plugin::ExportList::Info; # for now it's basically a method->hashkey translator
88              
89             our $AUTOLOAD;
90              
91             sub new {
92 1     1   3 my $pkg = shift;
93 1 50       11 bless {@_ ? @_ : qw/
94             style implicit
95             /}, $pkg;
96             };
97              
98             sub AUTOLOAD {
99 6     6   7 my $self = shift;
100 6         23 $AUTOLOAD =~ /.*::(.*)$/;
101 6         9 my $method = $1;
102 6 50       14 return if $method eq 'DESTROY';
103            
104 6 100       23 $self->{$method} = shift if (@_);
105            
106 6         25 $self->{$method};
107             }
108              
109             1; # Keep your mother happy.
110              
111             __END__