Ado

 view release on metacpan or  search on metacpan

lib/Ado/Command/generate/crud.pm  view on Meta::CPAN

use Time::Piece ();
use List::Util qw(first);
File::Spec::Functions->import(qw(catfile catdir splitdir));

has description => "Generates directory structures for Ado-specific CRUD..\n";
has usage => sub { shift->extract_usage };

has routes => sub {
    $_[0]->{routes} = [];
    foreach my $t (@{$_[0]->args->{tables}}) {
        my $controller = camelize($t);
        my $route      = decamelize($controller);
        push @{$_[0]->{routes}},
          { route => "/$route",
            via   => ['GET'],
            to    => "$route#list",
          },
          { route => "/$route/list",
            via   => ['GET'],
            to    => "$route#list",
          },
          { route => "/$route/read/:id",
            via   => [qw(GET)],
            to    => "$route#read",
          },
          { route => "/$route/create",
            via   => [qw(GET POST)],
            to    => "$route#create",
            over  => {authenticated => 1},
          },
          { route => "/$route/update/:id",
            via   => [qw(GET PUT)],
            to    => "$route#update",
            over  => {authenticated => 1},
          },
          { route => "/$route/delete/:id",
            via   => [qw(GET DELETE)],
            to    => "$route#delete",
            over  => {authenticated => 1},
          };
    }
    return $_[0]->{routes};
};

sub initialise {
    my ($self, @args) = @_;
    return $self if $self->{_initialised};
    my $args = $self->args({tables => []})->args;

    GetOptionsFromArray(
        \@args,
        'C|controller_namespace=s' => \$args->{controller_namespace},

        #'d|dsn=s'                  => \$args->{dsn},
        'L|lib=s'             => \$args->{lib},
        'M|model_namespace=s' => \$args->{model_namespace},

        #'N|no_dsc_code'            => \$args->{no_dsc_code},
        'O|overwrite' => \$args->{overwrite},

        #'P|password=s'             => \$args->{password},
        'T|templates_root=s' => \$args->{templates_root},
        't|tables=s@'        => \$args->{tables},
        'H|home_dir=s'       => \$args->{home_dir},

        #'U|user=s'                 => \$args->{user},
    );

    @{$args->{tables}} = split(/\,/, join(',', @{$args->{tables}}));
    Carp::croak $self->usage unless scalar @{$args->{tables}};
    my $app = $self->app;
    $args->{controller_namespace} //= $app->routes->namespaces->[0];
    $args->{model_namespace} //=
      (first { ref($_) eq 'HASH' and $_->{name} eq 'DSC' } @{$app->config('plugins')})
      ->{config}{namespace};
    $args->{home_dir}       //= $app->home;
    $args->{lib}            //= catdir($args->{home_dir}, 'lib');
    $args->{templates_root} //= $app->renderer->paths->[0];
    $self->{_initialised} = 1;
    return $self;
}

sub run {
    my ($self) = shift->initialise(@_);
    my $args   = $self->args;
    my $app    = $self->app;

    foreach my $t (@{$args->{tables}}) {

        # Controllers
        my $class_name = camelize($t);
        $args->{class} = $args->{controller_namespace} . '::' . $class_name;
        my $c_file = catfile($args->{lib}, class_to_path($args->{class}));
        $args->{t} = lc $t;
        $self->render_to_file('class', $c_file, $args);

        # Templates
        my $template_dir  = decamelize($class_name);
        my $template_root = $args->{templates_root};
        my $t_file        = catfile($template_root, $template_dir, 'list.html.ep');
        $self->render_to_file('list_template', $t_file, $args);
        $t_file = catfile($template_root, $template_dir, 'create.html.ep');
        $self->render_to_file('create_template', $t_file, $args);
        $t_file = catfile($template_root, $template_dir, 'read.html.ep');
        $self->render_to_file('read_template', $t_file, $args);
        $t_file = catfile($template_root, $template_dir, 'delete.html.ep');
        $self->render_to_file('delete_template', $t_file, $args);
    }    # end foreach tables

    return $self;
}


1;


=pod

=encoding utf8

=head1 NAME



( run in 1.095 second using v1.01-cache-2.11-cpan-5a3173703d6 )