encutened examples
Robert 'phaylon' Sedlacek [Wed, 5 Aug 2009 20:37:40 +0000 (22:37 +0200)]
examples/MyApp-Web/Makefile.PL
examples/MyApp-Web/lib/MyApp/Web/Controller/Calc.pm [new file with mode: 0644]
examples/MyApp-Web/lib/MyApp/Web/Model/Calc.pm [new file with mode: 0644]
examples/MyApp-Web/lib/MyApp/Web/View/Hello.pm
examples/MyApp-Web/t/02functionality.t [new file with mode: 0644]

index a9f7569..a6ab6b6 100644 (file)
@@ -6,7 +6,7 @@ use inc::Module::Install;
 name 'MyApp-Web';
 all_from 'lib/MyApp/Web.pm';
 
-requires 'CatalystX::Declare' => '0.001';
+requires 'CatalystX::Declare' => '0.004';
 requires 'Catalyst::Runtime' => '5.80007';
 requires 'Catalyst::Plugin::ConfigLoader';
 requires 'Catalyst::Plugin::Static::Simple';
diff --git a/examples/MyApp-Web/lib/MyApp/Web/Controller/Calc.pm b/examples/MyApp-Web/lib/MyApp/Web/Controller/Calc.pm
new file mode 100644 (file)
index 0000000..3a2fdae
--- /dev/null
@@ -0,0 +1,18 @@
+use CatalystX::Declare;
+
+# this "calculator" (mind the quotes) is extendable by the model
+controller MyApp::Web::Controller::Calc {
+
+    # we base of the common base in the root controller
+    action base as calc under '/base';
+
+    # we fetch our operator from the model in an action attached to the base
+    action operator <- base (Str $op) as '' {
+        $ctx->stash(operator => $ctx->component('Model::Calc')->op($op));
+    }
+
+    # here we know we have an operator and run it against the integers we got
+    final action evaluate <- operator (Int @nums) as '' {
+        $ctx->response->body($ctx->stash->{operator}->(@nums));
+    }
+}
diff --git a/examples/MyApp-Web/lib/MyApp/Web/Model/Calc.pm b/examples/MyApp-Web/lib/MyApp/Web/Model/Calc.pm
new file mode 100644 (file)
index 0000000..bb9462e
--- /dev/null
@@ -0,0 +1,27 @@
+use CatalystX::Declare;
+
+model MyApp::Web::Model::Calc {
+
+    method op (Str $op) {
+
+        if ($op eq 'add') { 
+            return sub {
+                my $num = shift;
+                $num += $_
+                    for @_;
+                return $num;
+            };
+        }
+        elsif ($op eq 'multiply') {
+            return sub {
+                my $num = shift;
+                $num *= $_
+                    for @_;
+                return $num;
+            };
+        }
+        else {
+            return sub { 'unknown operator' };
+        }
+    }
+}
index 39ddf64..891f35b 100644 (file)
@@ -1,7 +1,7 @@
-use MooseX::Declare;
+use CatalystX::Declare;
 
 # nothing special here, so it's just normal MooseX::Declare syntax
-class MyApp::Web::View::Hello extends Catalyst::View {
+view MyApp::Web::View::Hello {
 
     # the process method is the standard method that is forwarded to
     method process (Object $ctx) {
diff --git a/examples/MyApp-Web/t/02functionality.t b/examples/MyApp-Web/t/02functionality.t
new file mode 100644 (file)
index 0000000..fac9806
--- /dev/null
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/../lib";
+
+use Test::More;
+use Catalyst::Test 'MyApp::Web';
+
+like get('/'), qr/welcome/i, 'root page displays welcome';
+is get('/ifthisisfoundsomeonehasserioustestnamingissues'), 'Page Not Found', 'default captures 404';
+
+is get('/calc/add/3/4/5'), 12, 'addition';
+is get('/calc/multiply/2/3/4'), 24, 'multiplication';
+
+is get('/calc/unknownthingy/3/4/5'), 'unknown operator', 'unknown operator';
+is get('/calc/add/3/f/5'), 'Bad Request', 'bad request';
+
+like get('/foo/hello'), qr/root controller role/, 'root controller role';
+
+is get('/foo/2/3/add'), 5, 'add two';
+is get('/foo/2/3/multiply'), 6, 'multiply two';
+
+is get('/foo/2/f/add'), 'Bad Request', 'bad request capture args';
+
+done_testing;