From: Stevan Little Date: Sun, 23 Apr 2006 13:56:12 +0000 (+0000) Subject: stuff X-Git-Tag: 0_05~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=daea75c973a07c6b047153878a032ccca24a21c9;p=gitmo%2FMoose.git stuff --- diff --git a/Changes b/Changes index f953c4e..1cd0bcd 100644 --- a/Changes +++ b/Changes @@ -7,7 +7,12 @@ Revision history for Perl extension Moose - has keyword now takes a 'metaclass' option to support custom attribute meta-classes on a per-attribute basis - - added tests for this + - added tests for this + - the 'has' keyword not accepts inherited slot + specifications (has '+foo'). This is still an + experimental feature and probably not finished + see t/038_attribute_inherited_slot_specs.t for + more details, or ask about it on #moose * Moose::Role - keywords are now exported with Sub::Exporter @@ -30,6 +35,9 @@ Revision history for Perl extension Moose * Moose::Meta::TypeConstraints - added type constraint unions - added tests for this + - reorganized the type constraint hierarchy, thanks + to nothingmuch for his help and advice on this + - added some tests for this 0.04 Sun. April 16th, 2006 * Moose::Role diff --git a/lib/Moose.pm b/lib/Moose.pm index 2087c26..ddd0fc3 100644 --- a/lib/Moose.pm +++ b/lib/Moose.pm @@ -82,8 +82,8 @@ use Moose::Util::TypeConstraints; my $inherited_attr = $meta->find_attribute_by_name($1); (defined $inherited_attr) || confess "Could not find an attribute by the name of '$1' to inherit from"; - (scalar keys %options == 1 && exists $options{default}) - || confess "Inherited slot specifications can only alter the 'default' option"; + #(scalar keys %options == 1 && exists $options{default}) + # || confess "Inherited slot specifications can only alter the 'default' option"; my $new_attr = $inherited_attr->clone(%options); $meta->add_attribute($new_attr); } @@ -347,6 +347,41 @@ construction, and within any accessors. The C<$type_name> argument must be a string. The string can be either a class name, or a type defined using Moose's type defintion features. +=item I (1|0)> + +This will attempt to use coercion with the supplied type constraint to change +the value passed into any accessors of constructors. You B have supplied +a type constraint in order for this to work. See L +for an example usage. + +=item I $role_name> + +This will accept the name of a role which the value stored in this attribute +is expected to have consumed. + +=item I (1|0)> + +This marks the attribute as being required. This means a value must be supplied +during class construction, and the attribute can never be set to C with +an accessor. + +=item I (1|0)> + +This will tell the class to strore the value of this attribute as a weakened +reference. If an attribute is a weakened reference, it can B also be coerced. + +=item I (1|0)> + +This will tell the class to not create this slot until absolutely nessecary. +If an attribute is marked as lazy it B have a default supplied. + +=item I $code> + +The trigger option is a CODE reference which will be called after the value of +the attribute is set. The CODE ref will be passed the instance itself, the +updated value and the attribute meta-object (this is for more advanced fiddling +and can typically be ignored in most cases). + =back =item B sub { ... }> diff --git a/lib/Moose/Meta/Attribute.pm b/lib/Moose/Meta/Attribute.pm index 4cab769..1d9632a 100644 --- a/lib/Moose/Meta/Attribute.pm +++ b/lib/Moose/Meta/Attribute.pm @@ -191,7 +191,7 @@ sub generate_accessor_method { 'weaken($_[0]->{$attr_name});' : '') . ($self->has_trigger ? - '$self->trigger->($_[0], ' . $value_name . ');' + '$self->trigger->($_[0], ' . $value_name . ', $self);' : '') . ' }' . ($self->is_lazy ? @@ -225,7 +225,7 @@ sub generate_writer_method { 'weaken($_[0]->{$attr_name});' : '') . ($self->has_trigger ? - '$self->trigger->($_[0], ' . $value_name . ');' + '$self->trigger->($_[0], ' . $value_name . ', $self);' : '') . ' }'; my $sub = eval $code; diff --git a/lib/Moose/Meta/Class.pm b/lib/Moose/Meta/Class.pm index 8a3cf27..b9b54c7 100644 --- a/lib/Moose/Meta/Class.pm +++ b/lib/Moose/Meta/Class.pm @@ -48,7 +48,7 @@ sub new_object { my $self = $class->SUPER::new_object(%params); foreach my $attr ($class->compute_all_applicable_attributes()) { next unless $params{$attr->name} && $attr->can('has_trigger') && $attr->has_trigger; - $attr->trigger->($self, $params{$attr->name}); + $attr->trigger->($self, $params{$attr->name}, $attr); } return $self; } diff --git a/t/038_attribute_inherited_slot_specs.t b/t/038_attribute_inherited_slot_specs.t index df735b2..657a78f 100644 --- a/t/038_attribute_inherited_slot_specs.t +++ b/t/038_attribute_inherited_slot_specs.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 10; +use Test::More tests => 12; use Test::Exception; BEGIN { @@ -56,11 +56,10 @@ isnt(Foo->meta->get_attribute('bar'), Bar->meta->get_attribute('bar'), '... Foo and Bar have different copies of bar'); +ok(Bar->meta->get_attribute('bar')->has_type_constraint, + '... Bar::bar inherited the type constraint too'); - - - - - +is(Bar->meta->get_attribute('bar')->type_constraint->name, + 'Str', '... Bar::bar inherited the right type constraint too');