From: Dave Rolsky Date: Fri, 26 Jun 2009 21:50:49 +0000 (-0500) Subject: Lots of code cleanup in M::AH traits. X-Git-Tag: 0.89_02~105 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2edb73d94e20c23a982e2172bfb9aa1548eb8ad3;p=gitmo%2FMoose.git Lots of code cleanup in M::AH traits. Removed lots of custom before modifiers in favor of allowing traits to define a _default_default and _default_is. Made more methods private. Killed the ridiculous "default => sub { 5 }" idiom in the SYNOPSIS sections. --- diff --git a/lib/Moose/AttributeHelpers/Trait/Base.pm b/lib/Moose/AttributeHelpers/Trait/Base.pm index b839fa4..b32c4dd 100644 --- a/lib/Moose/AttributeHelpers/Trait/Base.pm +++ b/lib/Moose/AttributeHelpers/Trait/Base.pm @@ -44,31 +44,33 @@ has '+type_constraint' => ( required => 1 ); ## Methods called prior to instantiation -sub process_options_for_handles { - my ( $self, $options ) = @_; +before '_process_options' => sub { + my ( $self, $name, $options ) = @_; - if ( my $type = $self->helper_type ) { - ( exists $options->{isa} ) - || confess "You must define a type with the $type metaclass"; + $self->_check_helper_type( $options, $name ); - my $isa = $options->{isa}; + $options->{is} = $self->_default_is + if ! exists $options->{is} && $self->can('_default_is'); - unless ( blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint') ) { - $isa - = Moose::Util::TypeConstraints::find_or_create_type_constraint( - $isa); - } + $options->{default} = $self->_default_default + if ! exists $options->{default} && $self->can('_default_default'); +}; - ( $isa->is_a_type_of($type) ) - || confess - "The type constraint for a $type ($options->{isa}) must be a subtype of $type"; - } -} +sub _check_helper_type { + my ( $self, $options, $name ) = @_; -before '_process_options' => sub { - my ( $self, $name, $options ) = @_; - $self->process_options_for_handles( $options, $name ); -}; + my $type = $self->helper_type; + + $options->{isa} = $self->helper_type + unless exists $options->{isa}; + + my $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint( + $options->{isa} ); + + ( $isa->is_a_type_of($type) ) + || confess + "The type constraint for $name must be a subtype of $type but it's a $isa"; +} around '_canonicalize_handles' => sub { my $next = shift; @@ -91,9 +93,9 @@ around '_canonicalize_handles' => sub { ## methods called after instantiation -before 'install_accessors' => sub { (shift)->check_handles_values }; +before 'install_accessors' => sub { (shift)->_check_handles_values }; -sub check_handles_values { +sub _check_handles_values { my $self = shift; my $method_constructors = $self->method_constructors; @@ -145,16 +147,6 @@ __END__ Moose::AttributeHelpers::Trait::Base - base role for helpers -=head1 METHODS - -=head2 check_handles_values - -Confirms that handles has all valid possibilities in it. - -=head2 process_options_for_handles - -Ensures that the type constraint (C) matches the helper type. - =head1 BUGS All complex software has bugs lurking in it, and this module is no diff --git a/lib/Moose/AttributeHelpers/Trait/Bool.pm b/lib/Moose/AttributeHelpers/Trait/Bool.pm index 5da9db2..c4e4223 100644 --- a/lib/Moose/AttributeHelpers/Trait/Bool.pm +++ b/lib/Moose/AttributeHelpers/Trait/Bool.pm @@ -10,6 +10,8 @@ with 'Moose::AttributeHelpers::Trait::Base'; sub helper_type { 'Bool' } +sub _default_is { 'rw' } + # NOTE: # we don't use the method provider for this # module since many of the names of the provied @@ -23,15 +25,6 @@ has 'method_provider' => ( default => 'Moose::AttributeHelpers::MethodProvider::Bool' ); -before 'process_options_for_handles' => sub { - my ( $self, $options, $name ) = @_; - - # Set some default attribute options here unless already defined - if ( ( my $type = $self->helper_type ) && !exists $options->{isa} ) { - $options->{isa} = $type; - } -}; - no Moose::Role; # register the alias ... @@ -57,7 +50,7 @@ Moose::AttributeHelpers::Bool metaclass => 'Bool', is => 'rw', isa => 'Bool', - default => sub { 0 }, + default => 0, handles => { illuminate => 'set', darken => 'unset', diff --git a/lib/Moose/AttributeHelpers/Trait/Collection/Bag.pm b/lib/Moose/AttributeHelpers/Trait/Collection/Bag.pm index 84285ac..3f96d08 100644 --- a/lib/Moose/AttributeHelpers/Trait/Collection/Bag.pm +++ b/lib/Moose/AttributeHelpers/Trait/Collection/Bag.pm @@ -22,17 +22,7 @@ subtype 'Bag' => as 'HashRef[Int]'; sub helper_type { 'Bag' } -before 'process_options_for_handles' => sub { - my ( $self, $options, $name ) = @_; - - # Set some default attribute options here unless already defined - if ( ( my $type = $self->helper_type ) && !exists $options->{isa} ) { - $options->{isa} = $type; - } - - $options->{default} = sub { +{} } - unless exists $options->{default}; -}; +sub _default_default { sub { {} } } no Moose::Role; no Moose::Util::TypeConstraints; diff --git a/lib/Moose/AttributeHelpers/Trait/Counter.pm b/lib/Moose/AttributeHelpers/Trait/Counter.pm index a4cef7a..71965c4 100644 --- a/lib/Moose/AttributeHelpers/Trait/Counter.pm +++ b/lib/Moose/AttributeHelpers/Trait/Counter.pm @@ -17,21 +17,12 @@ has 'method_provider' => ( default => 'Moose::AttributeHelpers::MethodProvider::Counter', ); -sub helper_type { 'Num' } - -before 'process_options_for_handles' => sub { - my ( $self, $options, $name ) = @_; +sub _default_default { 0 } +sub _default_is { 'ro' } - # Set some default attribute options here unless already defined - if ( ( my $type = $self->helper_type ) && !exists $options->{isa} ) { - $options->{isa} = $type; - } - - $options->{is} = 'ro' unless exists $options->{is}; - $options->{default} = 0 unless exists $options->{default}; -}; +sub helper_type { 'Num' } -after 'check_handles_values' => sub { +after '_check_handles_values' => sub { my $self = shift; my $handles = $self->handles; @@ -74,7 +65,7 @@ Moose::AttributeHelpers::Counter metaclass => 'Counter', is => 'ro', isa => 'Num', - default => sub { 0 }, + default => 0, handles => { inc_counter => 'inc', dec_counter => 'dec', @@ -83,8 +74,8 @@ Moose::AttributeHelpers::Counter ); my $page = MyHomePage->new(); - $page->inc_counter; # same as $page->counter($page->counter + 1); - $page->dec_counter; # same as $page->counter($page->counter - 1); + $page->inc_counter; # same as $page->counter( $page->counter + 1 ); + $page->dec_counter; # same as $page->counter( $page->counter - 1 ); =head1 DESCRIPTION @@ -111,14 +102,6 @@ above. This allows for a very basic counter definition: =item B -=item B - -Run before its superclass method. - -=item B - -Run after its superclass method. - =back =head1 PROVIDED METHODS diff --git a/lib/Moose/AttributeHelpers/Trait/Number.pm b/lib/Moose/AttributeHelpers/Trait/Number.pm index 4059aa5..178d275 100644 --- a/lib/Moose/AttributeHelpers/Trait/Number.pm +++ b/lib/Moose/AttributeHelpers/Trait/Number.pm @@ -78,7 +78,7 @@ Moose::AttributeHelpers::Number metaclass => 'Number', is => 'ro', isa => 'Int', - default => sub { 5 }, + default => 5, handles => { set => 'set', add => 'add', diff --git a/lib/Moose/AttributeHelpers/Trait/String.pm b/lib/Moose/AttributeHelpers/Trait/String.pm index 3bdbc0b..54c9658 100644 --- a/lib/Moose/AttributeHelpers/Trait/String.pm +++ b/lib/Moose/AttributeHelpers/Trait/String.pm @@ -19,19 +19,10 @@ has 'method_provider' => ( sub helper_type { 'Str' } -before 'process_options_for_handles' => sub { - my ( $self, $options, $name ) = @_; +sub _default_default { q{} } +sub _default_is { 'rw' } - # Set some default attribute options here unless already defined - if ( ( my $type = $self->helper_type ) && !exists $options->{isa} ) { - $options->{isa} = $type; - } - - $options->{is} = 'rw' unless exists $options->{is}; - $options->{default} = '' unless exists $options->{default}; -}; - -after 'check_handles_values' => sub { +after '_check_handles_values' => sub { my $self = shift; my $handles = $self->handles; @@ -72,7 +63,7 @@ Moose::AttributeHelpers::String metaclass => 'String', is => 'rw', isa => 'Str', - default => sub { '' }, + default => q{}, handles => { add_text => 'append', replace_text => 'replace',