Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
initial
  • Loading branch information
jamhed.nb committed Jan 26, 2012
0 parents commit 78a0ba1
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
logs
sessions
8 changes: 8 additions & 0 deletions app.psgi
@@ -0,0 +1,8 @@
#!/usr/bin/env perl

use lib qw( ./lib );
use Dancer;

load_app 'Sample';

dance;
18 changes: 18 additions & 0 deletions config.yml
@@ -0,0 +1,18 @@
logger: "file"
log: "debug"
logger_format: "%t %h %m"
show_errors: 1

appname: "dancer-oo"
appdir: './'
session: "YAML"
session_dir: "sessions"
session_expires: 2592000
charset: 'utf-8'

template: "template_toolkit"
engines:
template_toolkit:
encoding: 'utf8'
start_tag: '[%'
end_tag: '%]'
61 changes: 61 additions & 0 deletions lib/Dancer/OO/Dancer.pm
@@ -0,0 +1,61 @@
package Dancer::OO::Dancer;
use strict;
use Dancer ();
use JSON;

sub debug {
Dancer::debug( join( ' ', map { ref $_ ? Dump($_) : $_ } map { $_ ? $_ : '_' } @_ ) );
}

sub import {
my ($self) = @_;
my $class = caller;
no strict 'refs';
*{"$class\::get"} = sub {
push @{ ${"$class\::_handler"} }, [ 'get', @_ ];
};
*{"$class\::post"} = sub {
push @{ ${"$class\::_handler"} }, [ 'post', @_ ];
};
*{"$class\::any"} = sub {
push @{ ${"$class\::_handler"} }, [ 'any', @_ ];
};
*{"$class\::template"} = sub {
my ( $self, $template, $args ) = @_;
$args = {} unless $args;
$args->{uri} = sub { join( '', ${"$self\::_prefix"}, $_[0] ) };
my $k = join( '/', ${"$self\::_prefix"}, 'pager' );
$args->{c} ||= Dancer::session($k) || {};
return Dancer::template( join( '/', ${"$self\::_prefix"}, $template ), $args );
};
*{"$class\::upload"} = \&Dancer::upload;
*{"$class\::content_type"} = \&Dancer::content_type;
*{"$class\::header"} = \&Dancer::header;
*{"$class\::redirect"} = \&Dancer::redirect;
*{"$class\::wrap"} = sub (&) {
my ($handler) = @_;
return sub {
my ($self) = @_;
return sub {
my $k = join( '/', ${"$self\::_prefix"}, 'pager' );
my $p = Dancer::params;
my $context = Dancer::session($k) || {};
my $pp;
foreach my $k ( keys %$p ) {
$pp->{ decode( 'utf8', $k ) } = $p->{$k}; # fucking dancer utf8 bug again?
}
my $ret = $handler->( $self, $context, $pp );
Dancer::session( $k, $context );
return $ret;
}
}
};

*{"$class\::json"} = sub { JSON->new->utf8(0)->pretty(1)->encode(shift) };
*{"$class\::debug"} = \&debug;

*{"$class\::params"} = \&Dancer::params;
*{"$class\::session"} = \&Dancer::session;
}

1;
34 changes: 34 additions & 0 deletions lib/Dancer/OO/Object.pm
@@ -0,0 +1,34 @@
package Dancer::OO::Object;
use strict;
use Dancer::OO::Dancer qw( debug );

# this module actually installs deferred and properly stacked uri handlers

sub _build_tree {
my ($package) = @_;
return unless $package;
no strict 'refs';
return map { $_, _build_tree($_) } @{"$package\::ISA"};
}

# delay declarations
sub import {
my ( $self, $prefix ) = @_;
no strict 'refs';
${"$self\::_prefix"} = $prefix;
my @tree = ($self, _build_tree($self));
my %seen;
foreach my $isa (@tree) {
foreach ( @{ ${"$isa\::_handler"} } ) {
my $path = join( '', $_->[0], $prefix, $_->[1] ) || '/';
next if $seen{$path};
debug( 'for', $self, $_->[0], $prefix . $_->[1], 'in', $isa);
$seen{$path} = 1;
Dancer::get $prefix . $_->[1] => $_->[2]->($self) if $_->[0] eq 'get';
Dancer::post $prefix . $_->[1] => $_->[2]->($self) if $_->[0] eq 'post';
Dancer::any $prefix . $_->[1] => $_->[2]->($self) if $_->[0] eq 'any';
}
}
}

1;
7 changes: 7 additions & 0 deletions lib/Sample.pm
@@ -0,0 +1,7 @@
package Sample;
use strict;

use Sample::Root '/';
use Sample::Demo '/demo';

1;
13 changes: 13 additions & 0 deletions lib/Sample/Base.pm
@@ -0,0 +1,13 @@
package Sample::Base;
use strict;
use parent 'Dancer::OO::Object';
use Dancer::OO::Dancer;

get '' => wrap {
my ($self, $context, $params) = @_;
return <<HTML;
<html><body>Holla</body></html>
HTML
};

1;
4 changes: 4 additions & 0 deletions lib/Sample/Demo.pm
@@ -0,0 +1,4 @@
package Sample::Demo;
use parent 'Sample::Base';

1;
4 changes: 4 additions & 0 deletions lib/Sample/Root.pm
@@ -0,0 +1,4 @@
package Sample::Root;
use parent 'Sample::Base';

1;

0 comments on commit 78a0ba1

Please sign in to comment.