Changeset 1702

Show
Ignore:
Timestamp:
01/09/06 02:13:05
Author:
miyagawa
Message:

added scalar support of expose_stash

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • Catalyst-View-JSON/trunk/Changes

    r1700 r1702  
    11Revision history for Perl extension Catalyst::View::JSON 
     2 
     30.04  Sun Jan  8 17:06:56 UTC 2006 
     4        - Added scalar support to expose_hash and updated the 
     5          document. 
    26 
    370.03  Sun Jan  8 16:36:36 UTC 2006 
  • Catalyst-View-JSON/trunk/lib/Catalyst/View/JSON.pm

    r1701 r1702  
    22 
    33use strict; 
    4 our $VERSION = '0.03'; 
     4our $VERSION = '0.04'; 
    55 
    66use base qw( Catalyst::View ); 
     
    99use Catalyst::Exception; 
    1010 
    11 __PACKAGE__->mk_accessors(qw( allow_callback callback_param expose_stash )); 
     11__PACKAGE__->mk_accessors(qw( allow_callback callback_param expose_stash __json )); 
    1212 
    1313sub new { 
     
    2424    } 
    2525 
     26    $self->__json( JSON::Converter->new ); 
    2627    $self; 
    2728} 
     
    3334    my $cond = sub { 1 }; 
    3435 
     36    my $single_key; 
    3537    if (my $expose = $self->expose_stash) { 
    3638        if (ref($expose) eq 'Regexp') { 
     
    3941            my %match = map { $_ => 1 } @$expose; 
    4042            $cond = sub { $match{$_[0]} }; 
     43        } elsif (!ref($expose)) { 
     44            $single_key = $expose; 
    4145        } else { 
    42             $c->log->warn("expose_stash shold be an array referernce or Regexp object."); 
     46            $c->log->warn("expose_stash should be an array referernce or Regexp object."); 
    4347        } 
    4448    } 
    4549 
    46     my %data = map { $cond->($_) ? ($_ => $c->stash->{$_}) : () } 
    47                 keys %{$c->stash}; 
     50    my $data; 
     51    if ($single_key) { 
     52        $data = $c->stash->{$single_key}; 
     53    } else { 
     54        $data = { map { $cond->($_) ? ($_ => $c->stash->{$_}) : () } 
     55                  keys %{$c->stash} }; 
     56    } 
    4857 
    4958    my $cb_param = $self->allow_callback 
     
    5665    my $output; 
    5766    $output .= "$cb(" if $cb; 
    58     $output .= JSON::objToJson(\%data); 
     67    $output .= $self->_jsonize($data); 
    5968    $output .= ");"   if $cb; 
    6069 
    6170    $c->res->output($output); 
     71} 
     72 
     73sub _jsonize { 
     74    my($self, $data) = @_; 
     75    ref $data ? $self->__json->objToJson($data) : $self->__json->valueToJson($data); 
    6276} 
    6377 
     
    119133=item expose_stash 
    120134 
    121 List or regular expression object, to specify which stash keys are 
    122 exposed as a JSON response. Defaults to everything. Examples: 
     135Scalar, List or regular expression object, to specify which stash keys are 
     136exposed as a JSON response. Defaults to everything. Examples configuration: 
     137 
     138  # use 'json_data' value as a data to return 
     139  expose_stash => 'json_data', 
    123140 
    124141  # only exposes keys 'foo' and 'bar' 
     
    127144  # only exposes keys that matches with /^json_/ 
    128145  expose_stash => qr/^json_/, 
     146 
     147Suppose you have data structure of the following. 
     148 
     149  $c->stash->{foo} = [ 1, 2 ]; 
     150  $c->stash->{bar} = [ 3, 4 ]; 
     151 
     152By default, this view will return: 
     153 
     154  {"foo":[1,2],"bar":2} 
     155 
     156But when you set C<< expose_stash => 'foo' >>, it'll just return 
     157 
     158  [1,2] 
     159 
     160instead of the whole object (hashref in perl). This option will be 
     161useful when you share the method with different views (e.g. TT) and 
     162don't want to expose non-irrelevant stash variables as in JSON. 
    129163 
    130164=back 
  • Catalyst-View-JSON/trunk/t/01_server.t

    r1700 r1702  
    55use lib "$FindBin::Bin/lib"; 
    66 
    7 use Test::More tests => 17
     7use Test::More tests => 22
    88use Catalyst::Test 'TestApp'; 
    99use JSON (); 
     
    4141 
    4242{ 
     43    my $request = HTTP::Request->new( GET => "http://localhost/foo2" ); 
     44 
     45    ok( my $response = request($request), 'Request' ); 
     46    ok( $response->is_success, 'Response Successful 2xx' ); 
     47    is( $response->code, 200, 'Response Code' ); 
     48    ok( $response->content_type, 'text/javascript+json' ); 
     49 
     50    my $data = JSON::jsonToObj($response->content); 
     51    is $data, "bar"; 
     52} 
     53 
     54{ 
    4355    my $request = HTTP::Request->new( GET => $entrypoint . "?cb=foobar" ); 
    4456 
  • Catalyst-View-JSON/trunk/t/lib/TestApp.pm

    r1700 r1702  
    2121    my ( $self, $c ) = @_; 
    2222 
     23    $c->component('View::JSON')->expose_stash(qr/^json_/); 
    2324    $c->stash->{json_foo} = "bar"; 
    2425    $c->stash->{json_baz} = [ 1, 2, 3 ]; 
    2526    $c->stash->{foo}      = "barbarbar"; 
     27 
     28    $c->forward('TestApp::View::JSON'); 
     29} 
     30 
     31sub foo2 : Global { 
     32    my( $self, $c ) = @_; 
     33 
     34    $c->component('View::JSON')->expose_stash('json_foo'); 
     35    $c->stash->{json_foo} = "bar"; 
     36    $c->stash->{json_baz} = [ 1, 2, 3 ]; 
    2637 
    2738    $c->forward('TestApp::View::JSON');