diff options
author | Jesse Luehrs <doy@tozt.net> | 2011-02-17 18:57:19 -0600 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2011-02-17 18:57:19 -0600 |
commit | a7c37d5489611bf79889dd689abb0ac686d640c9 (patch) | |
tree | 68d9b965c581dcc049c36b5e6a5d9b877ff0312b /lib/Narwhal | |
parent | 251066735d9b3392ba7ea35da2fc0d6cd896e3a1 (diff) | |
download | narwhal-a7c37d5489611bf79889dd689abb0ac686d640c9.tar.gz narwhal-a7c37d5489611bf79889dd689abb0ac686d640c9.zip |
refactor this a bit
Diffstat (limited to 'lib/Narwhal')
-rw-r--r-- | lib/Narwhal/Component/Wiki.pm | 65 | ||||
-rw-r--r-- | lib/Narwhal/Component/Wiki/Edit.pm | 68 | ||||
-rw-r--r-- | lib/Narwhal/Component/Wiki/Page.pm | 53 | ||||
-rw-r--r-- | lib/Narwhal/RouteBuilder/HTTPMethod.pm | 64 |
4 files changed, 185 insertions, 65 deletions
diff --git a/lib/Narwhal/Component/Wiki.pm b/lib/Narwhal/Component/Wiki.pm deleted file mode 100644 index fa07342..0000000 --- a/lib/Narwhal/Component/Wiki.pm +++ /dev/null @@ -1,65 +0,0 @@ -package Narwhal::Component::Wiki; -use Moose; - -use Narwhal::Page; - -has kioku => ( - isa => 'KiokuX::Model', - required => 1, - handles => 'KiokuDB::Role::API', -); - -has tt => ( - isa => 'Template', - required => 1, - handles => ['process'], -); - -has scope => ( - is => 'ro', - isa => 'KiokuDB::LiveObjects::Scope', - lazy => 1, - default => sub { shift->new_scope }, -); - -sub BUILD { - my $self = shift; - $self->scope; -} - -sub page { - my $self = shift; - my ($req, $page) = @_; - my $page_obj = $self->lookup("page:$page"); - return $req->new_response(404) - unless $page_obj; - my $out; - $self->process('page.tt', { uri_for => sub { $req->uri_for({@_}) }, text => $page_obj->text, page => $page }, \$out); - return $req->new_response(200, [], $out); -} - -sub edit { - my $self = shift; - my ($req, $page) = @_; - if ($req->method eq 'POST') { - my $page_obj = Narwhal::Page->new(text => $req->param('text')); - $self->txn_do(sub { - $self->delete("page:$page"); - $self->insert("page:$page" => $page_obj); - }); - my $res = $req->new_response(303); - $res->location($req->uri_for({controller => 'wiki', action => 'page', page_name => $page})); - return $res; - } - else { - my $page_obj = $self->lookup("page:$page"); - my $out; - $self->process('edit.tt', { uri_for => sub { $req->uri_for({@_}) }, text => ($page_obj ? $page_obj->text : ''), page => $page }, \$out); - return $req->new_response(200, [], $out); - } -} - -__PACKAGE__->meta->make_immutable; -no Moose; - -1; diff --git a/lib/Narwhal/Component/Wiki/Edit.pm b/lib/Narwhal/Component/Wiki/Edit.pm new file mode 100644 index 0000000..fabeb0c --- /dev/null +++ b/lib/Narwhal/Component/Wiki/Edit.pm @@ -0,0 +1,68 @@ +package Narwhal::Component::Wiki::Edit; +use Moose; + +has kioku => ( + isa => 'KiokuX::Model', + required => 1, + handles => 'KiokuDB::Role::API', +); + +has tt => ( + isa => 'Template', + required => 1, + handles => ['process'], +); + +has scope => ( + is => 'ro', + isa => 'KiokuDB::LiveObjects::Scope', + lazy => 1, + default => sub { shift->new_scope }, +); + +sub BUILD { + my $self = shift; + $self->scope; +} + +sub get { + my $self = shift; + my ($req, $page) = @_; + + my $page_obj = $self->lookup("page:$page"); + my $out; + $self->process( + 'edit.tt', + { + uri_for => sub { $req->uri_for({@_}) }, + text => ($page_obj ? $page_obj->text : ''), + page => $page + }, + \$out, + ); + return $req->new_response(200, [], $out); +} + +sub post { + my $self = shift; + my ($req, $page) = @_; + + my $page_obj = Narwhal::Page->new(text => $req->param('text')); + $self->txn_do(sub { + $self->delete("page:$page"); + $self->insert("page:$page" => $page_obj); + }); + my $res = $req->new_response(303); + $res->location( + $req->uri_for({ + action => 'page', + page_name => $page + }) + ); + return $res; +} + +__PACKAGE__->meta->make_immutable; +no Moose; + +1; diff --git a/lib/Narwhal/Component/Wiki/Page.pm b/lib/Narwhal/Component/Wiki/Page.pm new file mode 100644 index 0000000..726439a --- /dev/null +++ b/lib/Narwhal/Component/Wiki/Page.pm @@ -0,0 +1,53 @@ +package Narwhal::Component::Wiki::Page; +use Moose; + +has kioku => ( + isa => 'KiokuX::Model', + required => 1, + handles => 'KiokuDB::Role::API', +); + +has tt => ( + isa => 'Template', + required => 1, + handles => ['process'], +); + +has scope => ( + is => 'ro', + isa => 'KiokuDB::LiveObjects::Scope', + lazy => 1, + default => sub { shift->new_scope }, +); + +sub BUILD { + my $self = shift; + $self->scope; +} + +sub get { + my $self = shift; + my ($req, $page) = @_; + + my $page_obj = $self->lookup("page:$page"); + return $req->new_response(404) + unless $page_obj; + + my $out; + $self->process( + 'page.tt', + { + uri_for => sub { $req->uri_for({@_}) }, + text => $page_obj->text, + page => $page + }, + \$out + ); + + return $req->new_response(200, [], $out); +} + +__PACKAGE__->meta->make_immutable; +no Moose; + +1; diff --git a/lib/Narwhal/RouteBuilder/HTTPMethod.pm b/lib/Narwhal/RouteBuilder/HTTPMethod.pm new file mode 100644 index 0000000..fb2f124 --- /dev/null +++ b/lib/Narwhal/RouteBuilder/HTTPMethod.pm @@ -0,0 +1,64 @@ +package Narwhal::RouteBuilder::HTTPMethod; +use Moose; + +with 'OX::RouteBuilder'; + +sub import { + my $meta = Class::MOP::class_of(caller); + $meta->add_route_builder( + class => __PACKAGE__, + route_spec => sub { + my $spec = shift; + return if ref($spec) || $spec !~ /^\w+$/; + return { + action => $spec, + }; + }, + ); +} + +sub compile_routes { + my $self = shift; + + my $spec = $self->route_spec; + my $params = $self->params; + + my ($defaults, $validations) = $self->extract_defaults_and_validations($params); + $defaults = { %$spec, %$defaults }; + + my $s = $self->service; + + return [ + $self->path, + defaults => $defaults, + target => sub { + my ($req) = @_; + + my %match = %{ $req->env->{'plack.router.match'}->mapping }; + my $a = $match{action}; + my $component = $s->get_dependency($a)->get; + my $method = lc($req->method); + + if ($component->can($method)) { + return $component->$method(@_); + } + elsif ($component->can('any')) { + return $component->any(@_); + } + else { + return [ + 500, + [], + ["Component $component has no method $method"] + ]; + } + + }, + validations => $validations, + ]; +} + +__PACKAGE__->meta->make_immutable; +no Moose; + +1; |