Hatena::Groupcatalyst

masaki@catalyst RSSフィード

Fork me on GitHub
 | 

2007-08-07

Flavour(0.30)にflavours指定を復活させたりRequestに変なメソッドを追加するパッチ

| 17:05 | Flavour(0.30)にflavours指定を復活させたりRequestに変なメソッドを追加するパッチ - masaki@catalyst を含むブックマーク はてなブックマーク - Flavour(0.30)にflavours指定を復活させたりRequestに変なメソッドを追加するパッチ - masaki@catalyst Flavour(0.30)にflavours指定を復活させたりRequestに変なメソッドを追加するパッチ - masaki@catalyst のブックマークコメント

少なくとも C::P::F::Data に fallback => 1 を加えたことだけは意味あるかな.

--- /usr/lib/perl5/site_perl/5.8.8/Catalyst/Plugin/Flavour.pm	2006-11-28 16:19:40.000000000 +0900
+++ lib/Catalyst/Plugin/Flavour.pm	2007-08-07 16:57:02.000000000 +0900
@@ -4,8 +4,9 @@
 use NEXT;
 
 use Catalyst::Plugin::Flavour::Data;
+use Catalyst::Request;
 
-our $VERSION = '0.03';
+our $VERSION = '0.03_01';
 
 __PACKAGE__->mk_accessors(qw/flavour/);
 
@@ -19,6 +20,42 @@
     *ruri  = \&real_uri;
 
     sub real_path { shift->real_uri->path };
+
+    *ruri_with = \&real_uri_with;
+
+    sub real_uri_with {
+        my ($self, $args) = @_;
+
+        Carp::carp('No arguments passed to real_uri_with()') unless $args;
+
+        for my $value (values %$args) {
+            next unless defined $value;
+            for (ref $value eq 'ARRAY' ? @$value : $value) {
+                $_ = "$_";
+                utf8::encode($_);
+            }
+        };
+
+        my $uri = $self->real_uri->clone;
+        $uri->query_form(%{ $uri->query_form_hash }, %$args);
+
+        return $uri;
+    }
+
+    sub typed_uri_with {
+        my ($self, $flavour) = @_;
+
+        my $uri = $self->uri->clone;
+        $uri->path(join '.' => $uri->path, $flavour);
+
+        return $uri;
+    }
+}
+
+# overload fallback
+{
+    package Catalyst::Plugin::Flavour::Data;
+    use overload fallback => 1;
 }
 
 =head1 NAME
@@ -29,6 +66,13 @@
 
     use Catalyst qw/Flavour/;
 
+    __PACKAGE__->config(
+        flavour => {
+            flavours        => [qw/html rss json/],
+            default_flavour => 'html',
+        }
+    );
+
 =head1 DESCRIPTION
 
 This plugin allows you to handle request flavours like Blosxom.
@@ -63,7 +107,9 @@
 
     my $config = $c->config->{flavour};
 
-    if ( my ( $fn, $flavour ) = ( $path[-1] || '' ) =~ /(.*)\.(.*?)$/ ) {
+    # accept flavours
+    my $re = $config->{flavours} ? join('|' => @{ $config->{flavours} }) : '[a-z0-9]+';
+    if ( my ( $fn, $flavour ) = ( $path[-1] || '' ) =~ m!([^/]+)\.($re)$! ) {
         $c->flavour->fn($fn);
         $c->flavour->flavour($flavour);
         $path[-1] =~ s/\.$flavour$//;

トラックバック - http://catalyst.g.hatena.ne.jp/ikasam_a/20070807
 |