| 1 | package RWDE::AbstractFactory; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Error qw(:try); | ||
| 7 | use RWDE::Exceptions; | ||
| 8 | |||
| 9 | use vars qw($VERSION); | ||
| 10 | $VERSION = sprintf "%d", q$Revision: 506 $ =~ /(\d+)/; | ||
| 11 | |||
| 12 | =pod | ||
| 13 | |||
| 14 | =head1 RWDE::AbstractFactory | ||
| 15 | |||
| 16 | Abstract Factory, instantiates and returns any App object | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | =head2 instantiate | ||
| 21 | |||
| 22 | Instantiate an instance of the class specified in the parameter | ||
| 23 | |||
| 24 | Requires class parameter | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | sub instantiate { | ||
| 29 | my ($self, $params) = @_; | ||
| 30 | |||
| 31 | throw RWDE::DevelException({ info => 'AbstractFactory::Parameter error - class not specified' }) unless ($$params{'class'}); | ||
| 32 | |||
| 33 | my $proto = $$params{class}; | ||
| 34 | |||
| 35 | my $requested_type = ref $proto || $proto; | ||
| 36 | |||
| 37 | delete $$params{class}; | ||
| 38 | |||
| 39 | my $library = $requested_type . '.pm'; | ||
| 40 | |||
| 41 | $library =~ s/::/\//g; | ||
| 42 | |||
| 43 | require $library; | ||
| 44 | |||
| 45 | return $requested_type->new($params); | ||
| 46 | } | ||
| 47 | |||
| 48 | 1; |