| 1 | package Bio::Phylo::Factory; | ||||||||||||||
| 2 | 51 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 51 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 51 | ▶ | use Bio::Phylo::Util::Exceptions 'throw'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 51 | ▶ | use Bio::Phylo::Util::CONSTANT qw'looks_like_hash looks_like_class'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 51 | our $AUTOLOAD; | |||||||||||||
| 7 | 51 | my %class = ( | |||||||||||||
| 8 | 'taxa' => 'Bio::Phylo::Taxa', | ||||||||||||||
| 9 | 'taxon' => 'Bio::Phylo::Taxa::Taxon', | ||||||||||||||
| 10 | 'datum' => 'Bio::Phylo::Matrices::Datum', | ||||||||||||||
| 11 | 'matrix' => 'Bio::Phylo::Matrices::Matrix', | ||||||||||||||
| 12 | 'characters' => 'Bio::Phylo::Matrices::Characters', | ||||||||||||||
| 13 | 'character' => 'Bio::Phylo::Matrices::Character', | ||||||||||||||
| 14 | 'datatype' => 'Bio::Phylo::Matrices::Datatype', | ||||||||||||||
| 15 | 'forest' => 'Bio::Phylo::Forest', | ||||||||||||||
| 16 | 'node' => 'Bio::Phylo::Forest::Node', | ||||||||||||||
| 17 | 'tree' => 'Bio::Phylo::Forest::Tree', | ||||||||||||||
| 18 | 'logger' => 'Bio::Phylo::Util::Logger', | ||||||||||||||
| 19 | 'drawer' => 'Bio::Phylo::Treedrawer', | ||||||||||||||
| 20 | 'treedrawer' => 'Bio::Phylo::Treedrawer', | ||||||||||||||
| 21 | 'project' => 'Bio::Phylo::Project', | ||||||||||||||
| 22 | 'annotation' => 'Bio::Phylo::Annotation', | ||||||||||||||
| 23 | 'set' => 'Bio::Phylo::Set', | ||||||||||||||
| 24 | 'generator' => 'Bio::Phylo::Generator', | ||||||||||||||
| 25 | 'xmlwritable' => 'Bio::Phylo::NeXML::Writable', | ||||||||||||||
| 26 | 'xmlliteral' => 'Bio::Phylo::NeXML::Meta::XMLLiteral', | ||||||||||||||
| 27 | 'meta' => 'Bio::Phylo::NeXML::Meta', | ||||||||||||||
| 28 | 'dom' => 'Bio::Phylo::NeXML::DOM', | ||||||||||||||
| 29 | 'document' => 'Bio::Phylo::NeXML::DOM::Document', | ||||||||||||||
| 30 | 'element' => 'Bio::Phylo::NeXML::DOM::Element', | ||||||||||||||
| 31 | 'client' => 'Bio::Phylo::PhyloWS::Client', | ||||||||||||||
| 32 | 'server' => 'Bio::Phylo::PhyloWS::Server', | ||||||||||||||
| 33 | 'resource' => 'Bio::Phylo::PhyloWS::Resource', | ||||||||||||||
| 34 | 'description' => 'Bio::Phylo::PhyloWS::Resource::Description', | ||||||||||||||
| 35 | ); | ||||||||||||||
| 36 | |||||||||||||||
| 37 | 346 | ▶ | sub import { | ||||||||||||
|
Subroutine
import: called CC=2 SCAR=7.2
| |||||||||||||||
| 38 | 346 | my $package = shift; | |||||||||||||
| 39 | 346 | ▶ | $package->register_class(@_) if @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | =head1 NAME | ||||||||||||||
| 43 | |||||||||||||||
| 44 | Bio::Phylo::Factory - Creator of objects, reduces hardcoded class names in code | ||||||||||||||
| 45 | |||||||||||||||
| 46 | =head1 SYNOPSIS | ||||||||||||||
| 47 | |||||||||||||||
| 48 | use Bio::Phylo::Factory; | ||||||||||||||
| 49 | my $fac = Bio::Phylo::Factory->new; | ||||||||||||||
| 50 | my $node = $fac->create_node( '-name' => 'node1' ); | ||||||||||||||
| 51 | |||||||||||||||
| 52 | # probably prints 'Bio::Phylo::Forest::Node'? | ||||||||||||||
| 53 | print ref $node; | ||||||||||||||
| 54 | |||||||||||||||
| 55 | =head1 DESCRIPTION | ||||||||||||||
| 56 | |||||||||||||||
| 57 | The factory module is used to create other objects without having to 'use' | ||||||||||||||
| 58 | their classes. This allows for greater flexibility in Bio::Phylo's design, | ||||||||||||||
| 59 | as class names are no longer hard-coded all over the place. | ||||||||||||||
| 60 | |||||||||||||||
| 61 | =head1 METHODS | ||||||||||||||
| 62 | |||||||||||||||
| 63 | =head2 CONSTRUCTOR | ||||||||||||||
| 64 | |||||||||||||||
| 65 | =over | ||||||||||||||
| 66 | |||||||||||||||
| 67 | =item new() | ||||||||||||||
| 68 | |||||||||||||||
| 69 | Factory constructor. | ||||||||||||||
| 70 | |||||||||||||||
| 71 | Type : Constructor | ||||||||||||||
| 72 | Title : new | ||||||||||||||
| 73 | Usage : my $fac = Bio::Phylo::Factory->new; | ||||||||||||||
| 74 | Function: Initializes a Bio::Phylo::Factory object. | ||||||||||||||
| 75 | Returns : A Bio::Phylo::Factory object. | ||||||||||||||
| 76 | Args : (optional) a hash keyed on short names, with | ||||||||||||||
| 77 | class names for values. For example, | ||||||||||||||
| 78 | 'node' => 'Bio::Phylo::Forest::Node', which | ||||||||||||||
| 79 | will allow you to subsequently call $fac->create_node, | ||||||||||||||
| 80 | which will return a Bio::Phylo::Forest::Node object. | ||||||||||||||
| 81 | (Note that this example is enabled by default, so you | ||||||||||||||
| 82 | don't need to specify it.) | ||||||||||||||
| 83 | |||||||||||||||
| 84 | =cut | ||||||||||||||
| 85 | |||||||||||||||
| 86 | 329 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=4 SCAR=22.3
Pod
documented
| |||||||||||||||
| 87 | 329 | my $class = shift; | |||||||||||||
| 88 | 329 | ▶ | if (@_) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 89 | 0 | my %args = looks_like_hash @_; | |||||||||||||
| 90 | 0 | ▶ | while ( my ( $key, $value ) = each %args ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 91 | 0 | ▶ | if ( looks_like_class $value ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 92 | 0 | $class{$key} = $value; | |||||||||||||
| 93 | } | ||||||||||||||
| 94 | } | ||||||||||||||
| 95 | } | ||||||||||||||
| 96 | 329 | bless \$class, $class; | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | |||||||||||||||
| 99 | =back | ||||||||||||||
| 100 | |||||||||||||||
| 101 | =head2 FACTORY METHODS | ||||||||||||||
| 102 | |||||||||||||||
| 103 | =over | ||||||||||||||
| 104 | |||||||||||||||
| 105 | =item create($class, %args) | ||||||||||||||
| 106 | |||||||||||||||
| 107 | Type : Factory methods | ||||||||||||||
| 108 | Title : create | ||||||||||||||
| 109 | Usage : my $foo = $fac->create('Foo::Class'); | ||||||||||||||
| 110 | Function: Creates an instance of $class, with constructor arguments %args | ||||||||||||||
| 111 | Returns : A Bio::Phylo::* object. | ||||||||||||||
| 112 | Args : $class, a class name (required), | ||||||||||||||
| 113 | %args, constructor arguments (optional) | ||||||||||||||
| 114 | |||||||||||||||
| 115 | =cut | ||||||||||||||
| 116 | |||||||||||||||
| 117 | 0 | ▶ | sub create { | ||||||||||||
|
Subroutine
create: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 118 | 0 | my $self = shift; | |||||||||||||
| 119 | 0 | my $class = shift; | |||||||||||||
| 120 | 0 | ▶ | if ( looks_like_class $class ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 121 | 0 | return $class->new(@_); | |||||||||||||
| 122 | } | ||||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | =item register_class() | ||||||||||||||
| 126 | |||||||||||||||
| 127 | Registers the argument class name such that subsequently | ||||||||||||||
| 128 | the factory can instantiates objects of that class. For | ||||||||||||||
| 129 | example, if you register Foo::Bar, the factory will be | ||||||||||||||
| 130 | able to instantiate objects through the create_bar() | ||||||||||||||
| 131 | method. | ||||||||||||||
| 132 | |||||||||||||||
| 133 | Type : Factory methods | ||||||||||||||
| 134 | Title : register_class | ||||||||||||||
| 135 | Usage : $fac->register_class('Foo::Bar'); | ||||||||||||||
| 136 | Function: Registers a class name for instantiation | ||||||||||||||
| 137 | Returns : Invocant | ||||||||||||||
| 138 | Args : $class, a class name (required), or | ||||||||||||||
| 139 | 'bar' => 'Foo::Bar', such that you | ||||||||||||||
| 140 | can subsequently call $fac->create_bar() | ||||||||||||||
| 141 | |||||||||||||||
| 142 | =cut | ||||||||||||||
| 143 | |||||||||||||||
| 144 | 0 | ▶ | sub register_class { | ||||||||||||
|
Subroutine
register_class: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||
| 145 | 0 | my ( $self, @args ) = @_; | |||||||||||||
| 146 | 0 | my ( $short, $class ); | |||||||||||||
| 147 | 0 | ▶ | if ( @args == 1 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 148 | 0 | $class = $args[0]; | |||||||||||||
| 149 | } | ||||||||||||||
| 150 | else { | ||||||||||||||
| 151 | 0 | ( $short, $class ) = @args; | |||||||||||||
| 152 | } | ||||||||||||||
| 153 | 0 | my $path = $class; | |||||||||||||
| 154 | 0 | $path =~ s|::|/|g; | |||||||||||||
| 155 | 0 | $path .= '.pm'; | |||||||||||||
| 156 | 0 | ▶ | if ( not $INC{$path} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 157 | 0 | eval { require $path }; | |||||||||||||
| 158 | 0 | ▶ | if ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 159 | 0 | throw 'ExtensionError' => "Can't register $class - $@"; | |||||||||||||
| 160 | } | ||||||||||||||
| 161 | } | ||||||||||||||
| 162 | 0 | ▶ | if ( not defined $short ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 163 | 0 | $short = $class; | |||||||||||||
| 164 | 0 | $short =~ s/.*://; | |||||||||||||
| 165 | 0 | $short = lc $short; | |||||||||||||
| 166 | } | ||||||||||||||
| 167 | 0 | $class{$short} = $class; | |||||||||||||
| 168 | 0 | return $self; | |||||||||||||
| 169 | } | ||||||||||||||
| 170 | |||||||||||||||
| 171 | # need empty destructor here so we don't autoload it | ||||||||||||||
| 172 | 0 | ▶ | sub DESTROY {} | ||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||
| 173 | |||||||||||||||
| 174 | 7650 | ▶ | sub AUTOLOAD { | ||||||||||||
|
Subroutine
AUTOLOAD: called CC=4 SCAR=14.4
| |||||||||||||||
| 175 | 7650 | my $self = shift; | |||||||||||||
| 176 | 7650 | my $method = $AUTOLOAD; | |||||||||||||
| 177 | 7650 | $method =~ s/.*://; | |||||||||||||
| 178 | 7650 | my $type = $method; | |||||||||||||
| 179 | 7650 | $type =~ s/^create_//; | |||||||||||||
| 180 | 7650 | ▶ | if ( exists $class{$type} ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 181 | 7650 | my $class = $class{$type}; | |||||||||||||
| 182 | 7650 | my $path = $class; | |||||||||||||
| 183 | 7650 | $path =~ s|::|/|g; | |||||||||||||
| 184 | 7650 | $path .= '.pm'; | |||||||||||||
| 185 | 7650 | ▶ | if ( not $INC{$path} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 186 | |||||||||||||||
| 187 | # here we need to do a string eval use so that the | ||||||||||||||
| 188 | # entire symbol table is populated | ||||||||||||||
| 189 | 127 | require $path; | |||||||||||||
| 190 | } | ||||||||||||||
| 191 | 7650 | return $class{$type}->new(@_); | |||||||||||||
| 192 | } | ||||||||||||||
| 193 | elsif ( $method =~ qr/^[A-Z]+$/ ) { | ||||||||||||||
| 194 | 0 | return; | |||||||||||||
| 195 | } | ||||||||||||||
| 196 | else { | ||||||||||||||
| 197 | 0 | throw 'UnknownMethod' => "No such method: $method"; | |||||||||||||
| 198 | } | ||||||||||||||
| 199 | } | ||||||||||||||
| 200 | |||||||||||||||
| 201 | =back | ||||||||||||||
| 202 | |||||||||||||||
| 203 | =head1 SEE ALSO | ||||||||||||||
| 204 | |||||||||||||||
| 205 | There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo> | ||||||||||||||
| 206 | for any user or developer questions and discussions. | ||||||||||||||
| 207 | |||||||||||||||
| 208 | =over | ||||||||||||||
| 209 | |||||||||||||||
| 210 | =item L<Bio::Phylo::Manual> | ||||||||||||||
| 211 | |||||||||||||||
| 212 | Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>. | ||||||||||||||
| 213 | |||||||||||||||
| 214 | =back | ||||||||||||||
| 215 | |||||||||||||||
| 216 | =head1 CITATION | ||||||||||||||
| 217 | |||||||||||||||
| 218 | If you use Bio::Phylo in published research, please cite it: | ||||||||||||||
| 219 | |||||||||||||||
| 220 | B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen> | ||||||||||||||
| 221 | and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl. | ||||||||||||||
| 222 | I<BMC Bioinformatics> B<12>:63. | ||||||||||||||
| 223 | L<http://dx.doi.org/10.1186/1471-2105-12-63> | ||||||||||||||
| 224 | |||||||||||||||
| 225 | =cut | ||||||||||||||
| 226 | |||||||||||||||
| 227 | 51 | 1; | |||||||||||||