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