[Moose-commits] r7614 - in Moose/trunk: lib/Moose/Cookbook/Basics
t/000_recipes/basics
autarch at code2.0beta.co.uk
autarch at code2.0beta.co.uk
Wed Feb 11 19:31:37 GMT 2009
Author: autarch
Date: 2009-02-11 11:31:37 -0800 (Wed, 11 Feb 2009)
New Revision: 7614
Modified:
Moose/trunk/lib/Moose/Cookbook/Basics/Recipe5.pod
Moose/trunk/t/000_recipes/basics/005_coercion.t
Log:
Don't define a coercion directly to a class like URI. Make a subtype
and coerce that instead.
Modified: Moose/trunk/lib/Moose/Cookbook/Basics/Recipe5.pod
===================================================================
--- Moose/trunk/lib/Moose/Cookbook/Basics/Recipe5.pod 2009-02-11 19:19:54 UTC (rev 7613)
+++ Moose/trunk/lib/Moose/Cookbook/Basics/Recipe5.pod 2009-02-11 19:31:37 UTC (rev 7614)
@@ -15,17 +15,17 @@
use Params::Coerce ();
use URI ();
- class_type('HTTP::Headers');
+ subtype 'My.HTTP::Headers' => as class_type('HTTP::Headers');
- coerce 'HTTP::Headers'
+ coerce 'My.HTTP::Headers'
=> from 'ArrayRef'
=> via { HTTP::Headers->new( @{$_} ) }
=> from 'HashRef'
=> via { HTTP::Headers->new( %{$_} ) };
- class_type('URI');
+ subtype 'My.URI' => as class_type('HTTP::Headers');
- coerce 'URI'
+ coerce 'My.URI'
=> from 'Object'
=> via { $_->isa('URI')
? $_
@@ -37,13 +37,13 @@
=> as 'Str'
=> where { /^HTTP\/[0-9]\.[0-9]$/ };
- has 'base' => ( is => 'rw', isa => 'URI', coerce => 1 );
- has 'uri' => ( is => 'rw', isa => 'URI', coerce => 1 );
+ has 'base' => ( is => 'rw', isa => 'My.URI', coerce => 1 );
+ has 'uri' => ( is => 'rw', isa => 'My.URI', coerce => 1 );
has 'method' => ( is => 'rw', isa => 'Str' );
has 'protocol' => ( is => 'rw', isa => 'Protocol' );
has 'headers' => (
is => 'rw',
- isa => 'HTTP::Headers',
+ isa => 'My.HTTP::Headers',
coerce => 1,
default => sub { HTTP::Headers->new }
);
@@ -61,11 +61,17 @@
First, we create the subtype to which we will coerce the other types:
- class_type('HTTP::Headers');
+ subtype 'My.HTTP::Headers' => as class_type('HTTP::Headers');
-The C<class_type> sugar function is simply a shortcut for something
-like this:
+We are creating a subtype rather than using C<HTTP::Headers> as a type
+directly. The reason we do this is coercions are global, and a
+coercion defined for C<HTTP::Headers> in our C<Request> class would
+then be defined for I<all> Moose-using classes in the current Perl
+interpreter. It's a L<best practice|Moose::Manual::BestPractices> to
+avoid this sort of namespace pollution.
+The C<class_type> sugar function is simply a shortcut for this:
+
subtype 'HTTP::Headers'
=> as 'Object'
=> where { $_->isa('HTTP::Headers') };
@@ -92,7 +98,7 @@
instance, and just do the right thing. This is exactly what coercion
is for:
- coerce 'HTTP::Headers'
+ coerce 'My.HTTP::Headers'
=> from 'ArrayRef'
=> via { HTTP::Headers->new( @{$_} ) }
=> from 'HashRef'
@@ -108,7 +114,7 @@
has 'headers' => (
is => 'rw',
- isa => 'Header',
+ isa => 'My.HTTP::Headers',
coerce => 1,
default => sub { HTTP::Headers->new }
);
@@ -131,11 +137,11 @@
Once again, we need to declare a class type for our non-Moose L<URI>
class:
- class_type('URI');
+ subtype 'My.URI' => as class_type('HTTP::Headers');
Then we define the coercion:
- coerce 'URI'
+ coerce 'My.URI'
=> from 'Object'
=> via { $_->isa('URI')
? $_
@@ -158,8 +164,8 @@
Finally, we need to make sure our attributes enable coercion.
- has 'base' => ( is => 'rw', isa => 'URI', coerce => 1 );
- has 'uri' => ( is => 'rw', isa => 'URI', coerce => 1 );
+ has 'base' => ( is => 'rw', isa => 'My.URI', coerce => 1 );
+ has 'uri' => ( is => 'rw', isa => 'My.URI', coerce => 1 );
Re-using the coercion lets us enforce a consistent API across multiple
attributes.
Modified: Moose/trunk/t/000_recipes/basics/005_coercion.t
===================================================================
--- Moose/trunk/t/000_recipes/basics/005_coercion.t 2009-02-11 19:19:54 UTC (rev 7613)
+++ Moose/trunk/t/000_recipes/basics/005_coercion.t 2009-02-11 19:31:37 UTC (rev 7614)
@@ -22,17 +22,17 @@
use Params::Coerce ();
use URI ();
- class_type('HTTP::Headers');
+ subtype 'My.HTTP::Headers' => as class_type('HTTP::Headers');
- coerce 'HTTP::Headers'
+ coerce 'My.HTTP::Headers'
=> from 'ArrayRef'
=> via { HTTP::Headers->new( @{$_} ) }
=> from 'HashRef'
=> via { HTTP::Headers->new( %{$_} ) };
- class_type('URI');
+ subtype 'My.URI' => as class_type('HTTP::Headers');
- coerce 'URI'
+ coerce 'My.URI'
=> from 'Object'
=> via { $_->isa('URI')
? $_
@@ -44,13 +44,13 @@
=> as 'Str'
=> where { /^HTTP\/[0-9]\.[0-9]$/ };
- has 'base' => ( is => 'rw', isa => 'URI', coerce => 1 );
- has 'uri' => ( is => 'rw', isa => 'URI', coerce => 1 );
+ has 'base' => ( is => 'rw', isa => 'My.URI', coerce => 1 );
+ has 'uri' => ( is => 'rw', isa => 'My.URI', coerce => 1 );
has 'method' => ( is => 'rw', isa => 'Str' );
has 'protocol' => ( is => 'rw', isa => 'Protocol' );
has 'headers' => (
is => 'rw',
- isa => 'HTTP::Headers',
+ isa => 'My.HTTP::Headers',
coerce => 1,
default => sub { HTTP::Headers->new }
);
More information about the Moose-commits
mailing list