updatin
[gitmo/Moose.git] / lib / Moose / Cookbook / Recipe5.pod
1
2 =pod
3
4 =head1 NAME
5
6 Moose::Cookbook::Recipe5 - More subtypes, coercion in a B<Request> class
7
8 =head1 SYNOPSIS
9
10   package Request;
11   use strict;
12   use warnings;
13   use Moose;
14   use Moose::Util::TypeConstraints;
15   
16   use HTTP::Headers  ();
17   use Params::Coerce ();
18   use URI            ();
19   
20   subtype Header
21       => as Object
22       => where { $_->isa('HTTP::Headers') };
23   
24   coerce Header
25       => from ArrayRef
26           => via { HTTP::Headers->new( @{ $_ } ) }
27       => from HashRef
28           => via { HTTP::Headers->new( %{ $_ } ) };
29   
30   subtype Uri
31       => as Object
32       => where { $_->isa('URI') };
33   
34   coerce Uri
35       => from Object
36           => via { $_->isa('URI') ? $_ : Params::Coerce::coerce( 'URI', $_ ) }
37       => from Str
38           => via { URI->new( $_, 'http' ) };
39   
40   subtype Protocol
41       => as Str
42       => where { /^HTTP\/[0-9]\.[0-9]$/ };
43   
44   has 'base'     => (is => 'rw', isa => 'Uri', coerce  => 1);
45   has 'url'      => (is => 'rw', isa => 'Uri', coerce  => 1);   
46   has 'method'   => (is => 'rw', isa => 'Str'); 
47   has 'protocol' => (is => 'rw', isa => 'Protocol');            
48   has 'headers'  => (
49       is      => 'rw',
50       isa     => 'Header',
51       coerce  => 1,
52       default => sub { HTTP::Headers->new } 
53   );
54
55 =head1 DESCRIPTION
56
57 Coming Soon. 
58
59 (the other 4 recipes kinda burned me out a bit)
60
61 =head1 AUTHOR
62
63 Stevan Little E<lt>stevan@iinteractive.comE<gt>
64
65 =head1 COPYRIGHT AND LICENSE
66
67 Copyright 2006 by Infinity Interactive, Inc.
68
69 L<http://www.iinteractive.com>
70
71 This library is free software; you can redistribute it and/or modify
72 it under the same terms as Perl itself.
73
74 =cut