| 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 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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 | 4 | our $VERSION = 0.02; | |||||||||
| 16 | |||||||||||
| 17 | 43 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||
| 18 | 43 | my $pkg = shift; | |||||||||
| 19 | 43 | my $plugin = shift; | |||||||||
| 20 | |||||||||||
| 21 | 43 | ▶ | my $self = bless { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | plugin => $plugin, | ||||||||||
| 23 | info => (ref $_[0] ? shift : Object::Meta::Plugin::ExportList::Info->new()), | ||||||||||
| 24 | }, $pkg; | ||||||||||
| 25 | |||||||||||
| 26 | 43 | my @methods = @_; | |||||||||
| 27 | |||||||||||
| 28 | 43 | ▶ | if (@methods){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 0 | my %list = map { $_, undef } $plugin->exports(); # used to cross out what's not exported | |||||||||
| 30 | 0 | $self->{methods} = [ grep { exists $list{$_} } @methods ]; # filter the method list to be only what works; | |||||||||
| 31 | } else { | ||||||||||
| 32 | 43 | $self->{methods} = [ $plugin->exports() ]; # everything unless otherwise stated | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | 43 | $self; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 274 | ▶ | sub plugin { | ||||||||
|
Subroutine
plugin: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 39 | 274 | my $self = shift; | |||||||||
| 40 | 274 | $self->{plugin}; | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 0 | ▶ | sub exists { | ||||||||
|
Subroutine
exists: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 44 | 0 | my $self = shift; | |||||||||
| 45 | |||||||||||
| 46 | 0 | ▶ | $self->{index} = { map { $_, undef } @{ $self->{methods} } } unless (exists $self->{index}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | |||||||||||
| 48 | 0 | ▶ | if (wantarray){ # return a grepped list | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 0 | return grep { exists $self->{index}{$_} } @_; | |||||||||
| 50 | } else { # return a true or false | ||||||||||
| 51 | 0 | return exists $self->{index}{$_[0]}; | |||||||||
| 52 | } | ||||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 43 | ▶ | sub list { # list all under plugin | ||||||||
|
Subroutine
list: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 56 | 43 | my $self = shift; | |||||||||
| 57 | |||||||||||
| 58 | 43 | return @{ $self->{methods} }; | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 0 | ▶ | sub merge { # or another exoprt list into this one | ||||||||
|
Subroutine
merge: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 62 | 0 | my $self = shift; | |||||||||
| 63 | 0 | my $x = shift; | |||||||||
| 64 | |||||||||||
| 65 | 0 | my %uniq; | |||||||||
| 66 | 0 | @{ $self->{methods} } = grep { not $uniq{$_}++ } @{ $self->{methods} }, $x->list(); | |||||||||
| 67 | |||||||||||
| 68 | 0 | $self; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 0 | ▶ | sub unmerge { # and (not|complement) another export list into this one | ||||||||
|
Subroutine
unmerge: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 72 | 0 | my $self = shift; | |||||||||
| 73 | 0 | my $x = shift; | |||||||||
| 74 | |||||||||||
| 75 | 0 | my %seen = map { $_, undef } $x->list(); | |||||||||
| 76 | 0 | @{ $self->{methods} } = grep { not exists $seen{$_} } @{ $self->{methods} }; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 98 | ▶ | sub info { | ||||||||
|
Subroutine
info: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 80 | 98 | my $self = shift; | |||||||||
| 81 | |||||||||||
| 82 | 98 | ▶ | $self->{info} = shift if (@_); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | |||||||||||
| 84 | 98 | $self->{info}; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | package Object::Meta::Plugin::ExportList::Info; # for now it's basically a method->hashkey translator | ||||||||||
| 88 | |||||||||||
| 89 | 4 | our $AUTOLOAD; | |||||||||
| 90 | |||||||||||
| 91 | 44 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
| |||||||||||
| 92 | 44 | my $pkg = shift; | |||||||||
| 93 | 44 | ▶ | bless {@_ ? @_ : qw/ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | style implicit | ||||||||||
| 95 | /}, $pkg; | ||||||||||
| 96 | }; | ||||||||||
| 97 | |||||||||||
| 98 | 143 | ▶ | sub AUTOLOAD { | ||||||||
|
Subroutine
AUTOLOAD: called CC=3 SCAR=11.0
| |||||||||||
| 99 | 143 | my $self = shift; | |||||||||
| 100 | 143 | $AUTOLOAD =~ /.*::(.*)$/; | |||||||||
| 101 | 143 | my $method = $1; | |||||||||
| 102 | 143 | ▶ | return if $method eq 'DESTROY'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | |||||||||||
| 104 | 99 | ▶ | $self->{$method} = shift if (@_); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | |||||||||||
| 106 | 99 | $self->{$method}; | |||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 4 | 1; # Keep your mother happy. | |||||||||
| 110 | |||||||||||
| 111 | __END__ | ||||||||||