File Coverage

blib/lib/GenOO/RegionCollection/Factory/DBIC.pm
Criterion Covered Total %
statement 28 28 100.0
branch 7 14 50.0
condition n/a
subroutine 7 7 100.0
pod 0 1 0.0
total 42 50 84.0


line stmt bran cond sub pod time code
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.1';
52              
53             #######################################################################
54             ####################### Load External modules #####################
55             #######################################################################
56 1     1   388756 use Modern::Perl;
  1         2  
  1         7  
57 1     1   130 use autodie;
  1         1  
  1         7  
58 1     1   3422 use Moose;
  1         2  
  1         9  
59 1     1   5757 use namespace::autoclean;
  1         1  
  1         8  
60              
61              
62             #######################################################################
63             ######################## Load GenOO modules #######################
64             #######################################################################
65 1     1   584 use GenOO::RegionCollection::Type::DBIC;
  1         4  
  1         490  
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 2     2 0 2967 my ($self) = @_;
136            
137 2         88 my $init_data = {
138             dsn => $self->dsn,
139             };
140 2 50       89 ($init_data->{table} = $self->table) if defined $self->table;
141 2 50       92 ($init_data->{attributes} = $self->attributes) if defined $self->attributes;
142 2 50       86 ($init_data->{user} = $self->user) if defined $self->user;
143 2 50       87 ($init_data->{password} = $self->password) if defined $self->password;
144 2 50       90 ($init_data->{records_class} = $self->records_class) if defined $self->records_class;
145            
146 2         97 return GenOO::RegionCollection::Type::DBIC->new($init_data);
147             }
148              
149             #######################################################################
150             ######################### Private Methods #########################
151             #######################################################################
152             sub _build_dsn {
153 2     2   4 my ($self) = @_;
154            
155 2         71 my $dsn = 'dbi:'.$self->driver.':database='.$self->database;
156 2 50       71 $dsn .= ';host='.$self->host if defined $self->host;
157 2 50       80 $dsn .= ';port='.$self->port if defined $self->port;
158            
159 2         65 return $dsn;
160             }
161              
162             1;