[Moose-commits] r7196 - in Moose/branches/moose-manual: . lib lib/Moose/Cookbook/Basics lib/Moose/Manual lib/Moose/Meta lib/Moose/Util t/020_attributes t/050_metaclasses t/100_bugs

autarch at code2.0beta.co.uk autarch at code2.0beta.co.uk
Tue Dec 30 14:54:21 GMT 2008


Author: autarch
Date: 2008-12-30 06:54:21 -0800 (Tue, 30 Dec 2008)
New Revision: 7196

Added:
   Moose/branches/moose-manual/t/020_attributes/024_attribute_traits_parameterized.t
   Moose/branches/moose-manual/t/050_metaclasses/020_metaclass_parameterized_traits.t
Modified:
   Moose/branches/moose-manual/
   Moose/branches/moose-manual/Changes
   Moose/branches/moose-manual/Makefile.PL
   Moose/branches/moose-manual/lib/Moose.pm
   Moose/branches/moose-manual/lib/Moose/Cookbook/Basics/Recipe2.pod
   Moose/branches/moose-manual/lib/Moose/Manual/BestPractices.pod
   Moose/branches/moose-manual/lib/Moose/Meta/Attribute.pm
   Moose/branches/moose-manual/lib/Moose/Util/MetaRole.pm
   Moose/branches/moose-manual/t/100_bugs/011_DEMOLISH_eats_exceptions.t
Log:
merge from trunk to moose-manual branch


Property changes on: Moose/branches/moose-manual
___________________________________________________________________
Name: svk:merge
   - 08e7d58d-de06-4458-8c15-335e402ab116:/local/Moose:76787
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/BUILDARGS:4770
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/Moose-dumb_roles:1727
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/Moose-moosex_compile_support:3337
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/Moose-real_class_meta_type_constraint:3322
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/deprototype:6072
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/inline_wrapped_constructor:3777
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/meta-role-helper:5585
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/method-changes:5953
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/moose-exporter:5196
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/parameterize_constructor_role:5927
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/parameterized_type_fixes:6365
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/predicate_fix:2940
48425d73-d564-4cf1-a4fc-b7d19ba782dc:/local/Moose:54077
f7901bf9-e7b5-442b-97a4-4d7fd5798e10:/local/moose/Moose:41354
   + 08e7d58d-de06-4458-8c15-335e402ab116:/local/Moose:77743
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/BUILDARGS:4770
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/Moose-dumb_roles:1727
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/Moose-moosex_compile_support:3337
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/Moose-real_class_meta_type_constraint:3322
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/deprototype:6072
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/inline_wrapped_constructor:3777
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/meta-role-helper:5585
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/method-changes:5953
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/moose-exporter:5196
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/parameterize_constructor_role:5927
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/parameterized_type_fixes:6365
3efe9002-19ed-0310-8735-a98156148065:/Moose/branches/predicate_fix:2940
3efe9002-19ed-0310-8735-a98156148065:/Moose/trunk:7170
48425d73-d564-4cf1-a4fc-b7d19ba782dc:/local/Moose:54077
f7901bf9-e7b5-442b-97a4-4d7fd5798e10:/local/moose/Moose:41354

Modified: Moose/branches/moose-manual/Changes
===================================================================
--- Moose/branches/moose-manual/Changes	2008-12-29 23:12:49 UTC (rev 7195)
+++ Moose/branches/moose-manual/Changes	2008-12-30 14:54:21 UTC (rev 7196)
@@ -3,6 +3,12 @@
 0.64
     * Moose::Meta::Method::Accessor
       - Always inline predicate and clearer methods (Sartak)
+    * Moose::Meta::Attribute
+      - Support for parameterized traits (Sartak)
+      - verify_against_type_constraint method to avoid duplication
+        and enhance extensibility (Sartak)
+    * Moose::Meta::Class
+      - Tests (but no support yet) for parameterized traits (Sartak)
 
 0.63 Mon, December 8, 2008
     * Moose::Unsweetened
@@ -14,7 +20,7 @@
       - When a class does not provide all of a role's required
         methods, the error thrown now mentions all of the missing
         methods, as opposed to just the first one found. Requested by
-        Michael Schwern (RT #41119). (Dave Rolsky)
+        Curtis Poe (RT #41119). (Dave Rolsky)
 
     * Moose::Meta::Method::Constructor
       - Moose will no longer inline a constructor for your class
@@ -1246,7 +1252,7 @@
           ArrayRef[Int]    # array or integers
           HashRef[Object]  # a hash with object values
       They can also be nested:
-          ArrayRef[HashRef[RegExpr]] # an array of hashes with regexpr values
+          ArrayRef[HashRef[RegexpRef]] # an array of hashes with regex values
       And work with the type unions as well:
           ArrayRef[Int | Str]  # array of integers of strings
 

Modified: Moose/branches/moose-manual/Makefile.PL
===================================================================
--- Moose/branches/moose-manual/Makefile.PL	2008-12-29 23:12:49 UTC (rev 7195)
+++ Moose/branches/moose-manual/Makefile.PL	2008-12-30 14:54:21 UTC (rev 7196)
@@ -12,7 +12,7 @@
 requires 'perl'             => '5.008';
 requires 'Scalar::Util'     => '1.19';
 requires 'Carp';
-requires 'Class::MOP'       => '0.71_02';
+requires 'Class::MOP'       => '0.72';
 requires 'List::MoreUtils'  => '0.12';
 requires 'Sub::Exporter'    => '0.972';
 requires 'Task::Weaken'     => '0';

Modified: Moose/branches/moose-manual/lib/Moose/Cookbook/Basics/Recipe2.pod
===================================================================
--- Moose/branches/moose-manual/lib/Moose/Cookbook/Basics/Recipe2.pod	2008-12-29 23:12:49 UTC (rev 7195)
+++ Moose/branches/moose-manual/lib/Moose/Cookbook/Basics/Recipe2.pod	2008-12-30 14:54:21 UTC (rev 7196)
@@ -45,11 +45,11 @@
 
 The first recipe demonstrated how to build very basic Moose classes,
 focusing on creating and manipulating attributes. The objects in that
-recipe very data-oriented, and did not have much in the way of
+recipe were very data-oriented, and did not have much in the way of
 behavior (i.e. methods). In this recipe, we expand upon the concepts
 from the first recipe to include some real behavior. In particular, we
-should how you can use a method modifier to implement new behavior for
-a method.
+show how you can use a method modifier to implement new behavior for a
+method.
 
 The classes in the SYNOPSIS show two kinds of bank account. A simple
 bank account has one attribute, the balance, and two behaviors,

Modified: Moose/branches/moose-manual/lib/Moose/Manual/BestPractices.pod
===================================================================
--- Moose/branches/moose-manual/lib/Moose/Manual/BestPractices.pod	2008-12-29 23:12:49 UTC (rev 7195)
+++ Moose/branches/moose-manual/lib/Moose/Manual/BestPractices.pod	2008-12-30 14:54:21 UTC (rev 7196)
@@ -30,6 +30,8 @@
 
 =head2 use builder for everything else
 
+don't use initializer
+
 =head2 use lazy_build
 
 keep builders private
@@ -37,3 +39,5 @@
 consider keeping clearers & predicates private
 
 consider keeping writers private
+
+=head2 use MX::AH instead of auto_deref

Modified: Moose/branches/moose-manual/lib/Moose/Meta/Attribute.pm
===================================================================
--- Moose/branches/moose-manual/lib/Moose/Meta/Attribute.pm	2008-12-29 23:12:49 UTC (rev 7195)
+++ Moose/branches/moose-manual/lib/Moose/Meta/Attribute.pm	2008-12-30 14:54:21 UTC (rev 7196)
@@ -108,11 +108,24 @@
     my @traits;
 
     if (my $traits = $options{traits}) {
-        if ( @traits = grep { not $class->does($_) } map {
-            Moose::Util::resolve_metatrait_alias( Attribute => $_ )
-                or
-            $_
-        } @$traits ) {
+        my $i = 0;
+        while ($i < @$traits) {
+            my $trait = $traits->[$i++];
+            next if ref($trait); # options to a trait we discarded
+
+            $trait = Moose::Util::resolve_metatrait_alias(Attribute => $trait)
+                  || $trait;
+
+            next if $class->does($trait);
+
+            push @traits, $trait;
+
+            # are there options?
+            push @traits, $traits->[$i++]
+                if $traits->[$i] && ref($traits->[$i]);
+        }
+
+        if (@traits) {
             my $anon_class = Moose::Meta::Class->create_anon_class(
                 superclasses => [ $class ],
                 roles        => [ @traits ],
@@ -398,11 +411,7 @@
         if ($self->should_coerce && $type_constraint->has_coercion) {
             $val = $type_constraint->coerce($val);
         }
-        $type_constraint->check($val)
-            || $self->throw_error("Attribute (" 
-                     . $self->name 
-                     . ") does not pass the type constraint because: " 
-                     . $type_constraint->get_message($val), data => $val, object => $instance);
+        $self->verify_against_type_constraint($val, instance => $instance);
     }
 
     $self->set_initial_value($instance, $val);
@@ -454,11 +463,7 @@
         if ($type_constraint) {
             $val = $type_constraint->coerce($val)
                 if $can_coerce;
-            $type_constraint->check($val)
-                || $self->throw_error("Attribute (" 
-                         . $slot_name 
-                         . ") does not pass the type constraint because: " 
-                         . $type_constraint->get_message($val), data => $val, object => $instance);
+            $self->verify_against_type_constraint($val, object => $instance);
         }
         $meta_instance->set_slot_value($instance, $slot_name, $val);
     };
@@ -522,10 +527,7 @@
                 my $type_constraint = $self->type_constraint;
                 $value = $type_constraint->coerce($value)
                     if ($self->should_coerce);
-                $type_constraint->check($value) 
-                  || $self->throw_error("Attribute (" . $self->name
-                      . ") does not pass the type constraint because: "
-                      . $type_constraint->get_message($value), type_constraint => $type_constraint, data => $value);
+                $self->verify_against_type_constraint($value);
             }
             $self->set_initial_value($instance, $value);
         }
@@ -634,7 +636,7 @@
         }
         elsif ($handle_type eq 'Regexp') {
             ($self->has_type_constraint)
-                || $self->throw_error("Cannot delegate methods based on a RegExpr without a type constraint (isa)", data => $handles);
+                || $self->throw_error("Cannot delegate methods based on a Regexp without a type constraint (isa)", data => $handles);
             return map  { ($_ => $_) }
                    grep { /$handles/ } $self->_get_delegate_method_list;
         }
@@ -718,6 +720,21 @@
     );
 }
 
+sub verify_against_type_constraint {
+    my $self = shift;
+    my $val  = shift;
+
+    return 1 if !$self->has_type_constraint;
+
+    my $type_constraint = $self->type_constraint;
+
+    $type_constraint->check($val)
+        || $self->throw_error("Attribute ("
+                 . $self->name
+                 . ") does not pass the type constraint because: "
+                 . $type_constraint->get_message($val), data => $val, @_);
+}
+
 package Moose::Meta::Attribute::Custom::Moose;
 sub register_implementation { 'Moose::Meta::Attribute' }
 
@@ -833,6 +850,11 @@
 more information on what you can do with this, see the documentation
 for L<Moose::Meta::TypeConstraint>.
 
+=item B<verify_against_type_constraint>
+
+Verifies that the given value is valid under this attribute's type
+constraint, otherwise throws an error.
+
 =item B<has_handles>
 
 Returns true if this meta-attribute performs delegation.

Modified: Moose/branches/moose-manual/lib/Moose/Util/MetaRole.pm
===================================================================
--- Moose/branches/moose-manual/lib/Moose/Util/MetaRole.pm	2008-12-29 23:12:49 UTC (rev 7195)
+++ Moose/branches/moose-manual/lib/Moose/Util/MetaRole.pm	2008-12-30 14:54:21 UTC (rev 7196)
@@ -159,7 +159,7 @@
 This utility module is designed to help authors of Moose extensions
 write extensions that are able to cooperate with other Moose
 extensions. To do this, you must write your extensions as roles, which
-can then be dynamically applyied to the caller's metaclasses.
+can then be dynamically applied to the caller's metaclasses.
 
 This module makes sure to preserve any existing superclasses and roles
 already set for the meta objects, which means that any number of

Modified: Moose/branches/moose-manual/lib/Moose.pm
===================================================================
--- Moose/branches/moose-manual/lib/Moose.pm	2008-12-29 23:12:49 UTC (rev 7195)
+++ Moose/branches/moose-manual/lib/Moose.pm	2008-12-30 14:54:21 UTC (rev 7196)
@@ -15,7 +15,7 @@
 
 use Moose::Exporter;
 
-use Class::MOP 0.71_02;
+use Class::MOP 0.72;
 
 use Moose::Meta::Class;
 use Moose::Meta::TypeConstraint;

Added: Moose/branches/moose-manual/t/020_attributes/024_attribute_traits_parameterized.t
===================================================================
--- Moose/branches/moose-manual/t/020_attributes/024_attribute_traits_parameterized.t	                        (rev 0)
+++ Moose/branches/moose-manual/t/020_attributes/024_attribute_traits_parameterized.t	2008-12-30 14:54:21 UTC (rev 7196)
@@ -0,0 +1,53 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More tests => 4;
+
+{
+    package My::Attribute::Trait;
+    use Moose::Role;
+
+    sub reversed_name {
+        my $self = shift;
+        scalar reverse $self->name;
+    }
+}
+
+{
+    package My::Class;
+    use Moose;
+
+    has foo => (
+        traits => [
+            'My::Attribute::Trait' => {
+                alias => {
+                    reversed_name => 'eman',
+                },
+            },
+        ],
+    );
+}
+
+{
+    package My::Other::Class;
+    use Moose;
+
+    has foo => (
+        traits => [
+            'My::Attribute::Trait' => {
+                alias => {
+                    reversed_name => 'reversed',
+                },
+            },
+        ],
+    );
+}
+
+my $attr = My::Class->meta->get_attribute('foo');
+is($attr->eman, 'oof', 'the aliased method is in the attribute');
+ok(!$attr->can('reversed'), "the method was not installed under the other class' alias");
+
+my $other_attr = My::Other::Class->meta->get_attribute('foo');
+is($other_attr->reversed, 'oof', 'the aliased method is in the attribute');
+ok(!$other_attr->can('enam'), "the method was not installed under the other class' alias");
+

Added: Moose/branches/moose-manual/t/050_metaclasses/020_metaclass_parameterized_traits.t
===================================================================
--- Moose/branches/moose-manual/t/050_metaclasses/020_metaclass_parameterized_traits.t	                        (rev 0)
+++ Moose/branches/moose-manual/t/050_metaclasses/020_metaclass_parameterized_traits.t	2008-12-30 14:54:21 UTC (rev 7196)
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More skip_all => "Feature not implemented yet";
+#use Test::More tests => 1;
+
+{
+    package My::Trait;
+    use Moose::Role;
+
+    sub reversed_name {
+        my $self = shift;
+        scalar reverse $self->name;
+    }
+}
+
+{
+    package My::Class;
+    use Moose -traits => [
+        'My::Trait' => {
+            alias => {
+                reversed_name => 'enam',
+            },
+        },
+    ];
+}
+
+is(My::Class->meta->enam, 'ssalC::yM', 'parameterized trait applied');
+

Modified: Moose/branches/moose-manual/t/100_bugs/011_DEMOLISH_eats_exceptions.t
===================================================================
--- Moose/branches/moose-manual/t/100_bugs/011_DEMOLISH_eats_exceptions.t	2008-12-29 23:12:49 UTC (rev 7195)
+++ Moose/branches/moose-manual/t/100_bugs/011_DEMOLISH_eats_exceptions.t	2008-12-30 14:54:21 UTC (rev 7196)
@@ -11,7 +11,7 @@
 
 subtype 'FilePath'
     => as 'Str'
-    => where { $_ =~ m#^(/[a-zA-Z0-9_.-]+)+$#
+    => where { $_ =~ m#^(/[a-zA-Z0-9_.-]+)+/?$#
                 || $_ =~ m#^([c-zC-Z]:/[a-zA-Z0-9_.-]+)# };
 {
     package Baz;




More information about the Moose-commits mailing list