[Jifty-commit] r2026 - in jifty/trunk: .

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Tue Oct 17 11:47:41 EDT 2006


Author: jesse
Date: Tue Oct 17 11:47:40 2006
New Revision: 2026

Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/plugins/REST/lib/Jifty/Plugin/REST/Dispatcher.pm

Log:
 r28379 at 104:  jesse | 2006-10-10 17:52:58 -0400
 * The first bit of major refactoring of the REST plugin.


Modified: jifty/trunk/plugins/REST/lib/Jifty/Plugin/REST/Dispatcher.pm
==============================================================================
--- jifty/trunk/plugins/REST/lib/Jifty/Plugin/REST/Dispatcher.pm	(original)
+++ jifty/trunk/plugins/REST/lib/Jifty/Plugin/REST/Dispatcher.pm	Tue Oct 17 11:47:40 2006
@@ -66,44 +66,59 @@
         $apache->send_http_header;
         print Data::Dumper::Dumper(@_);
     }
-    elsif (ref($_[0]) eq 'ARRAY') {
-        print start_html(-encoding => 'UTF-8', -declare_xml => 1, -title => 'models'),
+    else {
+         
+        $apache->header_out('Content-Type' => 'text/html; charset=UTF-8');
+        $apache->send_http_header;
+        print render_as_html($prefix, $url, @_);
+    }
+
+    last_rule;
+}
+
+sub render_as_html {
+    my $prefix = shift;
+    my $url = shift;
+    my $content = shift;
+    if (ref($content) eq 'ARRAY') {
+        return start_html(-encoding => 'UTF-8', -declare_xml => 1, -title => 'models'),
               ul(map {
                 li(a({-href => "$url/".Jifty::Web->escape_uri($_)}, Jifty::Web->escape($_)))
-              } @{$_[0]}),
+              } @{$content}),
               end_html();
     }
-    elsif (ref($_[0]) eq 'HASH') {
-        print start_html(-encoding => 'UTF-8', -declare_xml => 1, -title => 'models'),
+    elsif (ref($content) eq 'HASH') {
+        return start_html(-encoding => 'UTF-8', -declare_xml => 1, -title => 'models'),
               dl(map {
                   dt(a({-href => "$url/".Jifty::Web->escape_uri($_)}, Jifty::Web->escape($_))),
-                  dd(html_dump($_[0]->{$_})),
-              } sort keys %{$_[0]}),
+                  dd(html_dump($content->{$_})),
+              } sort keys %{$content}),
               end_html();
     }
     else {
-        print start_html(-encoding => 'UTF-8', -declare_xml => 1, -title => 'models'),
-              Jifty::Web->escape($_[0]),
+        return start_html(-encoding => 'UTF-8', -declare_xml => 1, -title => 'models'),
+              Jifty::Web->escape($content),
               end_html();
     }
-
-    last_rule;
 }
 
+
+
 sub html_dump {
-    if (ref($_[0]) eq 'ARRAY') {
+    my $content = shift;
+    if (ref($content) eq 'ARRAY') {
         ul(map {
             li(html_dump($_))
-        } @{$_[0]});
+        } @{$content});
     }
-    elsif (ref($_[0]) eq 'HASH') {
+    elsif (ref($content) eq 'HASH') {
         dl(map {
             dt(Jifty::Web->escape($_)),
-            dd(html_dump($_[0]->{$_})),
-        } sort keys %{$_[0]}),
+            dd(html_dump($content->{$_})),
+        } sort keys %{$content}),
     }
     else {
-        Jifty::Web->escape($_[0]);
+        Jifty::Web->escape($content);
     }
 }
 
@@ -134,29 +149,25 @@
 }
 
 sub list_model_items {
+
     # Normalize model name - fun!
-    my ($model, $column) = (model($1), $2);
+    my ( $model, $column ) = ( model($1), $2 );
     my $col = $model->new->collection_class->new;
     $col->unlimit;
     $col->columns($column);
-    $col->order_by(column => $column);
+    $col->order_by( column => $column );
 
-    list(
-        ['model', $model, $column],
-        map { $_->$column() } @{ $col->items_array_ref || [] }
-    );
+    list( [ 'model', $model, $column ],
+        map { $_->$column() } @{ $col->items_array_ref || [] } );
 }
 
 sub show_item_field {
-    my ($model, $column, $key, $field) = (model($1), $2, $3, $4);
+    my ( $model, $column, $key, $field ) = ( model($1), $2, $3, $4 );
     my $rec = $model->new;
     $rec->load_by_cols( $column => $key );
-    $rec->id or abort(404);
-    exists $rec->{values}{$field} or abort(404);
-    outs(
-        ['model', $model, $column, $key, $field],
-        $rec->{values}{$field}
-    );
+    $rec->id          or abort(404);
+    $rec->can($field) or abort(404);
+    outs( [ 'model', $model, $column, $key, $field ], $rec->$field());
 }
 
 sub show_item {
@@ -164,10 +175,7 @@
     my $rec = $model->new;
     $rec->load_by_cols( $column => $key );
     $rec->id or abort(404);
-    outs(
-        ['model', $model, $column, $key],
-        $rec->{values}
-    );
+    outs( ['model', $model, $column, $key], { map {$_ => $rec->$_()} map {$_->name} $rec->columns});
 }
 
 sub replace_item {
@@ -206,11 +214,11 @@
 }
 
 sub run_action {
-    my ($action) = action($1) or abort(404);
-    Jifty::Util->require($action) or abort(404);
-    $action = $action->new or abort(404);
+    my ($action_name) = action($1) or abort(404);
+    Jifty::Util->require($action_name) or abort(404);
+    my $action = $action_name->new or abort(404);
 
-    # Jifty->api->is_allowed( $action ) or abort(403);
+    Jifty->api->is_allowed( $action ) or abort(403);
 
     my $args = Jifty->web->request->arguments;
     delete $args->{''};
@@ -232,10 +240,8 @@
         } 'model', ref($rec), 'id', $rec->id);
         Jifty->handler->apache->header_out('Location' => $url);
     }
-
-    print start_html(-encoding => 'UTF-8', -declare_xml => 1, -title => 'models'),
-          ul(map { li(html_dump($_)) } $action->result->message, Jifty->web->response->messages),
-          end_html();
+    #outs(undef, [$action->result->message, Jifty->web->response->messages]);
+    print start_html(-encoding => 'UTF-8', -declare_xml => 1, -title => 'models'), ul(map { li(html_dump($_)) } $action->result->message, Jifty->web->response->messages), end_html();
 
     last_rule;
 }


More information about the Jifty-commit mailing list