App-mkfeyorm

 view release on metacpan or  search on metacpan

lib/App/mkfeyorm.pm  view on Meta::CPAN

use MooseX::StrictConstructor;
use autodie;

use Data::Section -setup;
use File::Basename;
use File::Spec::Functions;
use Template;

has 'schema' => (
    is       => 'rw',
    isa      => 'Str',
    required => 1,
);

subtype 'TableRef',
    as 'HashRef';

sub _db_table_name {
    my $table = shift;

    $table =~ s/([A-Z]+)::/"_\L$1_"/ge;
    $table =~ s/([A-Z]+)([A-Z])/"_\L$1_$2"/ge;
    $table =~ s/([A-Z])/"_\L$1"/ge;
    $table =~ s/::/_/g;
    $table =~ s/_+/_/g;
    $table =~ s/^_//;

    return $table;
}

coerce 'TableRef',
    from 'ArrayRef',
    via {
        my %result = map { $_ => _db_table_name($_) } @$_;

        \%result;
    };

has 'tables' => (
    is       => 'rw',
    isa      => 'TableRef',
    required => 1,
    coerce   => 1,
);

has '_db_tables' => (
    is       => 'rw',
    isa      => 'ArrayRef',
);

has 'output_path' => (
    is      => 'rw',
    isa     => 'Str',
    default => 'lib',
);

after 'set_output_path' => sub {
    my ( $self, $path ) = @_;

    my $tt = Template->new({
        OUTPUT_PATH      => $self->output_path,
        DEFAULT_ENCODING => 'utf-8',
    }) || die "$Template::ERROR\n";

    $self->_set_template($tt);
};

has 'namespace' => (
    is      => 'rw',
    isa     => 'Str',
);

has 'table_namespace' => (
    is      => 'rw',
    isa     => 'Str',
);

has 'schema_namespace' => (
    is      => 'rw',
    isa     => 'Str',
);

has 'schema_template' => (
    is      => 'rw',
    isa     => 'Str',
    default => ${ __PACKAGE__->section_data('schema.tt') },
);

has 'table_template' => (
    is      => 'rw',
    isa     => 'Str',
    default => ${ __PACKAGE__->section_data('table.tt') },
);

has 'cache' => (
    is      => 'rw',
    isa     => 'Bool',
    default => 0,
);

has 'template_params' => (
    is      => 'rw',
    isa     => 'HashRef',
    default => sub { {} },
);

has '_template' => (
    is         => 'rw',
    isa        => 'Template',
    lazy_build => 1,
);

sub _build__template {
    my $self = shift;

    my $tt = Template->new({
        OUTPUT_PATH      => $self->output_path,
        DEFAULT_ENCODING => 'utf-8',
    }) || die "$Template::ERROR\n";

    return $tt;
}

sub schema_module {
    my $self = shift;

    my $full_name = join(
        '::',
        grep { $_ } (
            $self->namespace,
            $self->schema_namespace,
            $self->schema
        )
    );

    return $full_name;
}

sub table_modules {
    my ( $self, @tables ) = @_;

    my @full_names;
    if (@tables) {
        for my $table (@tables) {
            my $full_name = join(
                '::',
                grep { $_ } (
                    $self->namespace,
                    $self->table_namespace,
                    $table,
                )
            );
            push @full_names, $full_name;
        }
    }
    else {
        for my $table ( sort keys %{ $self->tables } ) {
            my $full_name = join(
                '::',
                grep { $_ } (
                    $self->namespace,
                    $self->table_namespace,
                    $table,
                )
            );
            push @full_names, $full_name;
        }
    }

    return @full_names;
}

sub process {
    my $self = shift;

    $self->process_schema;
    $self->process_table($_, $self->tables->{$_}) for keys %{ $self->tables };



( run in 2.087 seconds using v1.01-cache-2.11-cpan-13bb782fe5a )