Show
Ignore:
Timestamp:
03/04/08 02:12:17 (5 years ago)
Author:
masaki
Message:

lang/perl/URI-Platonic: Decorator として動作するようにしてみた

Location:
lang/perl/URI-Platonic/trunk
Files:
1 added
6 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/URI-Platonic/trunk/MANIFEST

    r7070 r7441  
    2727t/02_platonic.t 
    2828t/03_invalid.t 
     29t/04_autoload.t 
  • lang/perl/URI-Platonic/trunk/Makefile.PL

    r7070 r7441  
    44 
    55requires('URI'); 
     6requires('Class::Accessor::Fast'); 
     7requires('Scalar::Util'); 
    68 
    79build_requires('Test::More'); 
  • lang/perl/URI-Platonic/trunk/lib/URI/Platonic.pm

    r7070 r7441  
    33use strict; 
    44use warnings; 
    5 use URI::http; 
     5use base 'Class::Accessor::Fast'; 
     6use overload '""' => \&as_string, fallback => 1; 
     7use Scalar::Util qw(blessed); 
     8use URI; 
    69 
    710our $VERSION = '0.01'; 
    811 
    9 { 
    10     package # hide from PAUSE 
    11         URI::http; 
     12__PACKAGE__->mk_accessors(qw( extension uri )); 
    1213 
    13     sub platonize { 
    14         my $self = shift; 
     14sub new { 
     15    my ($class, $uri) = @_; 
    1516 
    16         my $platonic = $self->clone; 
    17         $platonic->path($self->platonic_path); 
    18         $platonic; 
     17    my $self = $class->SUPER::new({ uri => blessed($uri) ? $uri->clone : URI->new($uri) }); 
     18 
     19    my $path = $self->uri->path; 
     20    if ($path =~ m![^/]+\.([^/\.]+)$!) { 
     21        $self->extension($1); 
     22        $path =~ s/\.$1$//; 
     23        $self->uri->path($path); 
    1924    } 
    2025 
    21     sub platonic_path { 
    22         my $self = shift; 
    23         $self->_platonic_accessor(path => @_); 
     26    $self; 
     27} 
     28 
     29sub clone { 
     30    my $self = shift; 
     31    my $class = ref $self || $self; 
     32    return $class->new($self->distinct); 
     33} 
     34 
     35sub distinct { 
     36    my $self = shift; 
     37 
     38    my $uri = $self->uri->clone; 
     39    if ($self->extension) { 
     40        $uri->path(join '.', $uri->path, $self->extension); 
    2441    } 
     42    $uri; 
     43} 
    2544 
    26     sub extension { 
    27         my $self = shift; 
    28         $self->_platonic_accessor(extension => @_); 
     45our $AUTOLOAD; 
     46sub AUTOLOAD { 
     47    my $self = shift; 
     48 
     49    my ($class, $method) = $AUTOLOAD =~ /^(.+)::([^:]+)$/; 
     50 
     51    if ($self->uri->can($method)) { 
     52        return $self->uri->$method(@_); 
    2953    } 
    30  
    31     sub _platonic_accessor { 
    32         my $self = shift; 
    33         my ($name, $value) = @_; 
    34  
    35         if (defined $value) { 
    36             my $parts = $self->_platonic_parts; 
    37             $parts->{$name} = $value; 
    38  
    39             my $path = $parts->{path}; 
    40             $path .= ".$parts->{extension}" if $parts->{extension}; 
    41             $self->path($path); 
    42         } 
    43  
    44         $self->_platonic_parts->{$name}; 
    45     } 
    46  
    47     sub _platonic_parts { 
    48         my $self = shift; 
    49  
    50         my ($path, $extension) = ($self->path, ''); 
    51         if ($path =~ m![^/]+\.([^/\.]+)$!) { 
    52             $extension = $1; 
    53             $path =~ s/\.$1//; 
    54         } 
    55         return +{ path => $path, extension => $extension }; 
     54    else { 
     55        require Carp; 
     56        Carp::croak(qq/Can't locate object method "$method" via package "$class"/); 
    5657    } 
    5758} 
     59 
     60sub DESTROY {} 
    5861 
    59621; 
     
    6164=head1 NAME 
    6265 
    63 URI::Platonic - platonic and distinct URIs 
     66URI::Platonic - Platonic URI decorator 
    6467 
    6568=head1 SYNOPSIS 
     
    6972   
    7073  my $uri = URI->new("http://example.com/path/to/resource.html"); 
    71   $uri->platonize;     # "http://example.com/path/to/resource" (URI object) 
    72   $uri->platonic_path; # "/path/to/resource" 
    73   $uri->extension;     # "html" 
    74   $uri->as_string;     # "http://example.com/path/to/resource.html" 
     74  my $platonic = URI::Platonic->new($uri); 
    7575   
    76   $uri->extension('xml'); 
    77   $uri->platonic_path; # "/path/to/resource" 
    78   $uri->as_string;     # "http://example.com/path/to/resource.xml" 
     76  $platonic->path;      # "/path/to/resource" 
     77  $platonic->extension; # "html" 
     78  $platonic->as_string; # "http://example.com/path/to/resource" 
    7979   
    80   $uri->platonic_path('/path/to/another'); 
    81   $uri->platonize;     # "http://example.com/path/to/another" (URI object) 
    82   $uri->extension;     # "xml" 
    83   $uri->as_string;     # "http://example.com/path/to/another.xml" 
     80  $platonic->extension('xml'); 
     81  $platonic->path;      # "/path/to/resource" 
     82  $platonic->as_string; # "http://example.com/path/to/resource" 
     83  $platonic->distinct;  # "http://example.com/path/to/resource.xml" (URI object) 
     84   
     85  $platonic->path('/path/to/another'); 
     86  $platonic->extension; # "xml" 
     87  $platonic->as_string; # "http://example.com/path/to/another" 
     88  $platonic->distinct;  # "http://example.com/path/to/another.xml" (URI object) 
    8489 
    8590=head1 DESCRIPTION 
    8691 
    87 URI::Platonic defines helper methods for "platonic" and "distinct" URIs, 
     92URI::Platonic is decorator for "Platonic" and "Distinct" URIs, 
    8893described in RESTful Web Services. 
    8994 
    9095=head1 METHODS 
    9196 
    92 =head2 platonize 
     97=over 4 
    9398 
    94 Returns a platonic URI object. 
     99=item $platonic = URI::Platonic->new($uri) 
    95100 
    96 =head2 platonic_path([ $path ]) 
     101Constructs a new L<URI::Platonic> object. 
    97102 
    98 Gets/Sets path for a platonic URI. 
     103=item $platonic->path([ $path ]) 
    99104 
    100 =head2 extension([ $extension ]) 
     105Gets/Sets path for a "Platonic" URI. 
    101106 
    102 Gets/Sets extension for a distinct URI. 
     107=item $platonic->extension([ $extension ]) 
     108 
     109Gets/Sets extension for a "Distinct" URI. 
     110 
     111=item $platonic->distinct 
     112 
     113Returns a "Distinct" L<URI> object. 
     114 
     115=item $platonic->as_string 
     116 
     117Returns a "Platonic" URI plain string. 
     118 
     119=item $platonic->clone 
     120 
     121Returns a copy of the $platonic. 
     122 
     123=back 
    103124 
    104125=head1 AUTHOR 
  • lang/perl/URI-Platonic/trunk/t/01_distinct.t

    r7070 r7441  
    44use URI::Platonic; 
    55 
    6 plan tests => 12; 
     6plan tests => 13; 
    77 
    8 my $uri = URI->new('http://example.com/path/to/resource.html'); 
    9 is($uri->platonic_path, '/path/to/resource'); 
     8my $orig = URI->new('http://example.com/path/to/resource.html'); 
     9my $uri = URI::Platonic->new($orig); 
     10 
     11is($uri->path, '/path/to/resource'); 
    1012is($uri->extension, 'html'); 
    11 is($uri->platonize, 'http://example.com/path/to/resource'); 
    12 is($uri, 'http://example.com/path/to/resource.html'); 
     13is($uri->as_string, 'http://example.com/path/to/resource'); 
     14is($uri->distinct, 'http://example.com/path/to/resource.html'); 
    1315 
    1416$uri->extension('jpg'); 
    15 is($uri->platonic_path, '/path/to/resource'); 
     17is($uri->path, '/path/to/resource'); 
    1618is($uri->extension, 'jpg'); 
    17 is($uri->platonize, 'http://example.com/path/to/resource'); 
    18 is($uri, 'http://example.com/path/to/resource.jpg'); 
     19is($uri->as_string, 'http://example.com/path/to/resource'); 
     20is($uri->distinct, 'http://example.com/path/to/resource.jpg'); 
    1921 
    20 $uri->platonic_path('/path/to/another'); 
    21 is($uri->platonic_path, '/path/to/another'); 
     22$uri->path('/path/to/another'); 
     23is($uri->path, '/path/to/another'); 
    2224is($uri->extension, 'jpg'); 
    23 is($uri->platonize, 'http://example.com/path/to/another'); 
    24 is($uri, 'http://example.com/path/to/another.jpg'); 
     25is($uri->as_string, 'http://example.com/path/to/another'); 
     26is($uri->distinct, 'http://example.com/path/to/another.jpg'); 
     27 
     28is($uri, $uri->as_string); 
  • lang/perl/URI-Platonic/trunk/t/02_platonic.t

    r7070 r7441  
    66plan tests => 12; 
    77 
    8 my $uri = URI->new('http://example.com/path/to/resource'); 
    9 is($uri->platonic_path, '/path/to/resource'); 
    10 is($uri->extension, ''); 
    11 is($uri->platonize, 'http://example.com/path/to/resource'); 
    12 is($uri, 'http://example.com/path/to/resource'); 
     8my $orig = URI->new('http://example.com/path/to/resource'); 
     9my $uri = URI::Platonic->new($orig); 
    1310 
    14 $uri->platonic_path('/path/to/another'); 
    15 is($uri->platonic_path, '/path/to/another'); 
    16 is($uri->extension, ''); 
    17 is($uri->platonize, 'http://example.com/path/to/another'); 
    18 is($uri, 'http://example.com/path/to/another'); 
     11is($uri->path, '/path/to/resource'); 
     12is($uri->extension, undef); 
     13is($uri->as_string, 'http://example.com/path/to/resource'); 
     14is($uri->distinct, 'http://example.com/path/to/resource'); 
     15 
     16$uri->path('/path/to/another'); 
     17is($uri->path, '/path/to/another'); 
     18is($uri->extension, undef); 
     19is($uri->as_string, 'http://example.com/path/to/another'); 
     20is($uri->distinct, 'http://example.com/path/to/another'); 
    1921 
    2022$uri->extension('xml'); 
    21 is($uri->platonic_path, '/path/to/another'); 
     23is($uri->path, '/path/to/another'); 
    2224is($uri->extension, 'xml'); 
    23 is($uri->platonize, 'http://example.com/path/to/another'); 
    24 is($uri, 'http://example.com/path/to/another.xml'); 
     25is($uri->as_string, 'http://example.com/path/to/another'); 
     26is($uri->distinct, 'http://example.com/path/to/another.xml'); 
  • lang/perl/URI-Platonic/trunk/t/03_invalid.t

    r7070 r7441  
    66plan tests => 8; 
    77 
    8 my $uri = URI->new('http://example.com/.extension'); 
    9 is($uri->platonic_path, '/.extension'); 
    10 is($uri->extension, ''); 
    11 is($uri->platonize, 'http://example.com/.extension'); 
    12 is($uri, 'http://example.com/.extension'); 
     8my $orig = URI->new('http://example.com/.extension'); 
     9my $uri = URI::Platonic->new($orig); 
     10 
     11is($uri->path, '/.extension'); 
     12is($uri->extension, undef); 
     13is($uri->as_string, 'http://example.com/.extension'); 
     14is($uri->distinct, 'http://example.com/.extension'); 
    1315 
    1416$uri->extension('jpg'); 
    15 is($uri->platonic_path, '/.extension'); 
     17is($uri->path, '/.extension'); 
    1618is($uri->extension, 'jpg'); 
    17 is($uri->platonize, 'http://example.com/.extension'); 
    18 is($uri, 'http://example.com/.extension.jpg'); 
     19is($uri->as_string, 'http://example.com/.extension'); 
     20is($uri->distinct, 'http://example.com/.extension.jpg');