use delgation onto Parser and Producer instead of after BUILD
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator.pm
CommitLineData
c5051351 1package SQL::Translator;
a832857f 2use namespace::autoclean;
3use Moose;
4use MooseX::Types::Moose qw(Str);
c16f2fa9 5use SQL::Translator::Types qw(DBIHandle Parser Producer);
a832857f 6
7has 'parser' => (
44547961 8 isa => Str,
9 is => 'ro',
10 init_arg => 'from',
11 required => 1,
a832857f 12);
13
14has 'producer' => (
44547961 15 isa => Str,
16 is => 'ro',
17 init_arg => 'to',
18 required => 1,
a832857f 19);
20
c16f2fa9 21has '_parser' => (
22 isa => Parser,
23 is => 'rw',
24 lazy_build => 1,
25 handles => [ qw(parse) ],
26);
27
28has '_producer' => (
29 isa => Producer,
30 is => 'rw',
31 lazy_build => 1,
32 handles => [ qw(produce) ],
33);
34
a832857f 35has 'dbh' => (
36 isa => DBIHandle,
37 is => 'ro',
38 predicate => 'has_dbh',
39);
40
41has 'filename' => (
42 isa => Str,
43 is => 'ro',
44 predicate => 'has_ddl',
45);
46
c16f2fa9 47sub _build__parser {
a832857f 48 my $self = shift;
c16f2fa9 49 my $class = 'SQL::Translator::Parser';
50
51 Class::MOP::load_class($class);
a832857f 52
c16f2fa9 53 my $parser = $class->new({ dbh => $self->dbh });
a832857f 54
c16f2fa9 55 return $parser;
56}
57
58sub _build__producer {
59 my $self = shift;
60 my $class = 'SQL::Translator::Producer';
61 my $role = $class . '::' . $self->producer;
a832857f 62
c16f2fa9 63 Class::MOP::load_class($class);
64 eval { Class::MOP::load_class($role); };
65 if ($@) {
66 $role = $class . '::SQL::' . $self->producer;
67 eval { Class::MOP::load_class($role); };
68 die $@ if $@;
69 }
a832857f 70
c16f2fa9 71 my $producer = $class->new({ schema => $self->parse });
72 $role->meta->apply($producer);
a832857f 73
c16f2fa9 74 return $producer;
75}
a832857f 76
77__PACKAGE__->meta->make_immutable;
c5051351 78
791;