Created
July 5, 2010 08:15
-
-
Save jamhed/464130 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package smsw::Table; | |
use Dancer ':syntax'; | |
use Dancer::Plugin::Scoped; | |
our $VERSION = '0.1'; | |
use strict; | |
sub my_template ($$$) { | |
my ($prefix, $file, $args) = @_; | |
$args->{lib}->{nbsp} = sub { $_[0] =~ s/\s+/ /g; $_[0] }; | |
$args->{lib}->{row} = sub { join '', map { join('', '<', $_[0],'>',$_,'</',$_[0],'>') } @{$_[1]} }; | |
template join('', $prefix, $file), $args; | |
} | |
sub table_handlers { | |
my ($class, $prefix, $table) = @_; | |
my $old_prefix = prefix; | |
prefix $prefix; | |
get '/table' => sub { | |
my_template $prefix, '/table', { result => [ $table->list ] }; | |
}; | |
get '/create' => sub { | |
my_template $prefix, '/create', {}; | |
}; | |
post '/create' => scoped { | |
$table->insert( id => $table->new_id, %{ params() }); | |
}; | |
get '/update/:id' => scoped { | |
our ($id); | |
my_template $prefix, '/update', { result => $table->load( id => $id ) }; | |
}; | |
post '/update/:id' => sub { | |
$table->update( %{ params() } ); | |
}; | |
post '/delete/:id' => scoped { | |
our ($id); # magic | |
$table->delete_many( id => $id ); | |
}; | |
prefix $old_prefix; | |
} | |
true; | |
~ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment