| 1 | # POD documentation - main docs before the code | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | GenOO::RegionCollection::Factory::DB - Factory for creating GenOO::RegionCollection object from a database table | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | # Creates GenOO::RegionCollection object from a database table | ||
| 10 | |||
| 11 | # Preferably use it through the generic GenOO::RegionCollection::Factory | ||
| 12 | my $db_factory_implementation = GenOO::RegionCollection::Factory->new('DB', | ||
| 13 | { | ||
| 14 | driver => undef, | ||
| 15 | host => undef, | ||
| 16 | database => undef, | ||
| 17 | table => undef, | ||
| 18 | record_type => undef, | ||
| 19 | user => undef, | ||
| 20 | password => undef, | ||
| 21 | port => undef, | ||
| 22 | } | ||
| 23 | ); | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | An instance of this class is a concrete factory for a GenOO::RegionCollection | ||
| 28 | object. It offers the method "read_collection" (as the consumed role requires) | ||
| 29 | which returns the actual GenOO::RegionCollection object in the form of | ||
| 30 | GenOO::RegionCollection::Type::DB. The latter is the implementation of the | ||
| 31 | GenOO::RegionCollection class based on a database table. | ||
| 32 | |||
| 33 | =head1 EXAMPLES | ||
| 34 | |||
| 35 | # Create a concrete factory | ||
| 36 | my $factory_implementation = GenOO::RegionCollection::Factory->new('DB', | ||
| 37 | { | ||
| 38 | file => 'sample.sam' | ||
| 39 | } | ||
| 40 | ); | ||
| 41 | |||
| 42 | # Return the actual GenOO::RegionCollection object | ||
| 43 | my $collection = $factory_implementation->read_collection; | ||
| 44 | print ref($collection) # GenOO::RegionCollection::Type::DB | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | # Let the code begin... | ||
| 49 | |||
| 50 | package GenOO::RegionCollection::Factory::DBIC; | ||
| 51 | $GenOO::RegionCollection::Factory::DBIC::VERSION = '1.5.0'; | ||
| 52 | |||
| 53 | ####################################################################### | ||
| 54 | ####################### Load External modules ##################### | ||
| 55 | ####################################################################### | ||
| 56 | use Modern::Perl; | ||
| 57 | use autodie; | ||
| 58 | use Moose; | ||
| 59 | use namespace::autoclean; | ||
| 60 | |||
| 61 | |||
| 62 | ####################################################################### | ||
| 63 | ######################## Load GenOO modules ####################### | ||
| 64 | ####################################################################### | ||
| 65 | use GenOO::RegionCollection::Type::DBIC; | ||
| 66 | |||
| 67 | |||
| 68 | ####################################################################### | ||
| 69 | ####################### Interface attributes ###################### | ||
| 70 | ####################################################################### | ||
| 71 | has 'dsn' => ( | ||
| 72 | isa => 'Str', | ||
| 73 | is => 'ro', | ||
| 74 | builder => '_build_dsn', | ||
| 75 | lazy => 1 | ||
| 76 | ); | ||
| 77 | |||
| 78 | has 'user' => ( | ||
| 79 | isa => 'Maybe[Str]', | ||
| 80 | is => 'ro' | ||
| 81 | ); | ||
| 82 | |||
| 83 | has 'password' => ( | ||
| 84 | isa => 'Maybe[Str]', | ||
| 85 | is => 'ro' | ||
| 86 | ); | ||
| 87 | |||
| 88 | has 'attributes' => ( | ||
| 89 | traits => ['Hash'], | ||
| 90 | is => 'ro', | ||
| 91 | isa => 'HashRef[Str]', | ||
| 92 | default => sub { {} }, | ||
| 93 | ); | ||
| 94 | |||
| 95 | has 'driver' => ( | ||
| 96 | isa => 'Str', | ||
| 97 | is => 'ro', | ||
| 98 | ); | ||
| 99 | |||
| 100 | has 'database' => ( | ||
| 101 | isa => 'Str', | ||
| 102 | is => 'ro', | ||
| 103 | ); | ||
| 104 | |||
| 105 | has 'table' => ( | ||
| 106 | isa => 'Str', | ||
| 107 | is => 'ro', | ||
| 108 | ); | ||
| 109 | |||
| 110 | has 'records_class' => ( | ||
| 111 | is => 'ro', | ||
| 112 | ); | ||
| 113 | |||
| 114 | has 'host' => ( | ||
| 115 | isa => 'Maybe[Str]', | ||
| 116 | is => 'ro', | ||
| 117 | ); | ||
| 118 | |||
| 119 | has 'port' => ( | ||
| 120 | isa => 'Maybe[Int]', | ||
| 121 | is => 'ro', | ||
| 122 | ); | ||
| 123 | |||
| 124 | |||
| 125 | ####################################################################### | ||
| 126 | ########################## Consumed Roles ######################### | ||
| 127 | ####################################################################### | ||
| 128 | with 'GenOO::RegionCollection::Factory::Requires'; | ||
| 129 | |||
| 130 | |||
| 131 | ####################################################################### | ||
| 132 | ######################## Interface Methods ######################## | ||
| 133 | ####################################################################### | ||
| 134 | sub read_collection { | ||
| 135 | my ($self) = @_; | ||
| 136 | |||
| 137 | my $init_data = { | ||
| 138 | dsn => $self->dsn, | ||
| 139 | }; | ||
| 140 | ($init_data->{table} = $self->table) if defined $self->table; | ||
| 141 | ($init_data->{attributes} = $self->attributes) if defined $self->attributes; | ||
| 142 | ($init_data->{user} = $self->user) if defined $self->user; | ||
| 143 | ($init_data->{password} = $self->password) if defined $self->password; | ||
| 144 | ($init_data->{records_class} = $self->records_class) if defined $self->records_class; | ||
| 145 | |||
| 146 | return GenOO::RegionCollection::Type::DBIC->new($init_data); | ||
| 147 | } | ||
| 148 | |||
| 149 | ####################################################################### | ||
| 150 | ######################### Private Methods ######################### | ||
| 151 | ####################################################################### | ||
| 152 | sub _build_dsn { | ||
| 153 | my ($self) = @_; | ||
| 154 | |||
| 155 | my $dsn = 'dbi:'.$self->driver.':database='.$self->database; | ||
| 156 | $dsn .= ';host='.$self->host if defined $self->host; | ||
| 157 | $dsn .= ';port='.$self->port if defined $self->port; | ||
| 158 | |||
| 159 | return $dsn; | ||
| 160 | } | ||
| 161 | |||
| 162 | 1; |