Merge branch 'master' into attribute_helpers
[gitmo/Moose.git] / lib / Moose / AttributeHelpers / Trait / Base.pm
index e237372..07f19c1 100644 (file)
@@ -3,23 +3,20 @@ package Moose::AttributeHelpers::Trait::Base;
 use Moose::Role;
 use Moose::Util::TypeConstraints;
 
-our $VERSION   = '0.84';
+our $VERSION   = '0.85';
 $VERSION = eval $VERSION;
 our $AUTHORITY = 'cpan:STEVAN';
 
-requires 'helper_type';
+requires '_helper_type';
 
-# these next two are the possible methods
-# you can use in the 'handles' map.
+# these next two are the possible methods you can use in the 'handles'
+# map.
 
-# provide a Class or Role which we can
-# collect the method providers from
+# provide a Class or Role which we can collect the method providers
+# from
 
-# requires_attr 'method_provider'
-
-# or you can provide a HASH ref of anon subs
-# yourself. This will also collect and store
-# the methods from a method_provider as well
+# or you can provide a HASH ref of anon subs yourself. This will also
+# collect and store the methods from a method_provider as well
 has 'method_constructors' => (
     is      => 'ro',
     isa     => 'HashRef',
@@ -37,67 +34,72 @@ has 'method_constructors' => (
     },
 );
 
-# extend the parents stuff to make sure
-# certain bits are now required ...
-has '+default'         => (required => 1);
-has '+type_constraint' => (required => 1);
+has '+default'         => ( required => 1 );
+has '+type_constraint' => ( required => 1 );
 
-## Methods called prior to instantiation
+# 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} = $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;
     my $self    = shift;
     my $handles = $self->handles;
+
     return unless $handles;
-    unless ('HASH' eq ref $handles) {
+
+    unless ( 'HASH' eq ref $handles ) {
         $self->throw_error(
-            "The 'handles' option must be a HASH reference, not $handles"
-        );
+            "The 'handles' option must be a HASH reference, not $handles" );
     }
+
     return map {
         my $to = $handles->{$_};
-        $to = [ $to ] unless ref $to;
+        $to = [$to] unless ref $to;
         $_ => $to
     } keys %$handles;
 };
 
-## methods called after instantiation
+# 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;
 
     my %handles = $self->_canonicalize_handles;
 
-    for my $original_method (values %handles) {
+    for my $original_method ( values %handles ) {
         my $name = $original_method->[0];
-        (exists $method_constructors->{$name})
+        ( exists $method_constructors->{$name} )
             || confess "$name is an unsupported method type";
     }
 
@@ -105,9 +107,9 @@ sub check_handles_values {
 
 around '_make_delegation_method' => sub {
     my $next = shift;
-    my ($self, $handle_name, $method_to_call) = @_;
+    my ( $self, $handle_name, $method_to_call ) = @_;
 
-    my ($name, $curried_args) = @$method_to_call;
+    my ( $name, $curried_args ) = @$method_to_call;
 
     $curried_args ||= [];
 
@@ -124,7 +126,7 @@ around '_make_delegation_method' => sub {
         $handle_name,
         sub {
             my $instance = shift;
-            return $code->($instance, @$curried_args, @_);
+            return $code->( $instance, @$curried_args, @_ );
         },
     );
 };
@@ -140,16 +142,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<isa>) matches the helper type.
-
 =head1 BUGS
 
 All complex software has bugs lurking in it, and this module is no