From: Stevan Little Date: Mon, 7 May 2007 14:47:56 +0000 (+0000) Subject: foo X-Git-Tag: 0.18_01~86 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8ba40fb0b088c42192cad9bd39f45cfef520a975;p=gitmo%2FMooseX-AttributeHelpers.git foo --- diff --git a/lib/MooseX/AttributeHelpers/Base.pm b/lib/MooseX/AttributeHelpers/Base.pm index 09ae2f1..267ffbe 100644 --- a/lib/MooseX/AttributeHelpers/Base.pm +++ b/lib/MooseX/AttributeHelpers/Base.pm @@ -1,6 +1,7 @@ package MooseX::AttributeHelpers::Base; use Moose; +use Moose::Util::TypeConstraints; our $VERSION = '0.01'; our $AUTHORITY = 'cpan:STEVAN'; @@ -24,40 +25,57 @@ has 'provides' => ( has '+$!default' => (required => 1); has '+type_constraint' => (required => 1); -# this confirms that provides has -# all valid possibilities in it -sub _check_provides { - my $self = shift; - my $method_constructors = $self->method_constructors; - foreach my $key (keys %{$self->provides}) { - (exists $method_constructors->{$key}) - || confess "$key is an unsupported method type"; - } -} +## Methods called prior to instantiation + +sub helper_type { () } -# this provides an opportunity to -# manipulate the %options to handle -# some of the provides features -# correctly. -sub _process_options_for_provides { +sub process_options_for_provides { my ($self, $options) = @_; - # ... + if (my $type = $self->helper_type) { + (exists $options->{isa}) + || confess "You must define a type with the $type metaclass"; + + my $isa = $options->{isa}; + + unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) { + $isa = find_type_constraint($isa); + } + + ($isa->is_a_type_of($type)) + || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type"; + } + + # this can be augmented by subclasses .. + inner(); } before '_process_options' => sub { my ($self, $name, $options) = @_; if (exists $options->{provides}) { - $self->_process_options_for_provides($options); + $self->process_options_for_provides($options); } }; +## methods called after instantiation + +# this confirms that provides has +# all valid possibilities in it +sub check_provides_values { + my $self = shift; + my $method_constructors = $self->method_constructors; + foreach my $key (keys %{$self->provides}) { + (exists $method_constructors->{$key}) + || confess "$key is an unsupported method type"; + } +} + after 'install_accessors' => sub { my $attr = shift; my $class = $attr->associated_class; # before we install them, lets # make sure they are valid - $attr->_check_provides; + $attr->check_provides_values; my $method_constructors = $attr->method_constructors; @@ -70,6 +88,7 @@ after 'install_accessors' => sub { }; no Moose; +no Moose::Util::TypeConstraints; 1; diff --git a/lib/MooseX/AttributeHelpers/Collection.pm b/lib/MooseX/AttributeHelpers/Collection.pm deleted file mode 100644 index 392cd92..0000000 --- a/lib/MooseX/AttributeHelpers/Collection.pm +++ /dev/null @@ -1,40 +0,0 @@ - -package MooseX::AttributeHelpers::Collection; - -our $VERSION = '0.01'; -our $AUTHORITY = 'cpan:STEVAN'; - -1; - -__END__ - -=pod - -=head1 NAME - -=head1 SYNOPSIS - -=head1 DESCRIPTION - -=head1 METHODS - -=head1 BUGS - -All complex software has bugs lurking in it, and this module is no -exception. If you find a bug please either email me, or add the bug -to cpan-RT. - -=head1 AUTHOR - -Stevan Little Estevan@iinteractive.comE - -=head1 COPYRIGHT AND LICENSE - -Copyright 2007 by Infinity Interactive, Inc. - -L - -This library is free software; you can redistribute it and/or modify -it under the same terms as Perl itself. - -=cut \ No newline at end of file diff --git a/lib/MooseX/AttributeHelpers/Collection/Array.pm b/lib/MooseX/AttributeHelpers/Collection/Array.pm index 1bd9976..e9e9491 100644 --- a/lib/MooseX/AttributeHelpers/Collection/Array.pm +++ b/lib/MooseX/AttributeHelpers/Collection/Array.pm @@ -1,13 +1,14 @@ package MooseX::AttributeHelpers::Collection::Array; use Moose; -use Moose::Util::TypeConstraints; our $VERSION = '0.01'; our $AUTHORITY = 'cpan:STEVAN'; extends 'MooseX::AttributeHelpers::Base'; +sub helper_type { 'ArrayRef' } + has '+method_constructors' => ( default => sub { return +{ @@ -53,23 +54,7 @@ has '+method_constructors' => ( } ); -sub _process_options_for_provides { - my ($self, $options) = @_; - (exists $options->{isa}) - || confess "You must define a type with the Array metaclass"; - - my $isa = $options->{isa}; - - unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) { - $isa = find_type_constraint($isa); - } - - ($isa->is_a_type_of('ArrayRef')) - || confess "The type constraint for a Array ($options->{isa}) must be a subtype of ArrayRef"; -} - no Moose; -no Moose::Util::TypeConstraints;; # register the alias ... package Moose::Meta::Attribute::Custom::Collection::Array; diff --git a/lib/MooseX/AttributeHelpers/Collection/Hash.pm b/lib/MooseX/AttributeHelpers/Collection/Hash.pm index 1131ae8..c0a165f 100644 --- a/lib/MooseX/AttributeHelpers/Collection/Hash.pm +++ b/lib/MooseX/AttributeHelpers/Collection/Hash.pm @@ -1,13 +1,14 @@ package MooseX::AttributeHelpers::Collection::Hash; use Moose; -use Moose::Util::TypeConstraints; our $VERSION = '0.01'; our $AUTHORITY = 'cpan:STEVAN'; extends 'MooseX::AttributeHelpers::Base'; +sub helper_type { 'HashRef' } + has '+method_constructors' => ( default => sub { return +{ @@ -31,23 +32,7 @@ has '+method_constructors' => ( } ); -sub _process_options_for_provides { - my ($self, $options) = @_; - (exists $options->{isa}) - || confess "You must define a type with the Hash metaclass"; - - my $isa = $options->{isa}; - - unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) { - $isa = find_type_constraint($isa); - } - - ($isa->is_a_type_of('HashRef')) - || confess "The type constraint for a Hash ($options->{isa}) must be a subtype of HashRef"; -} - no Moose; -no Moose::Util::TypeConstraints; # register the alias ... package Moose::Meta::Attribute::Custom::Collection::Hash; diff --git a/lib/MooseX/AttributeHelpers/Counter.pm b/lib/MooseX/AttributeHelpers/Counter.pm index 8851056..86c1317 100644 --- a/lib/MooseX/AttributeHelpers/Counter.pm +++ b/lib/MooseX/AttributeHelpers/Counter.pm @@ -8,6 +8,8 @@ our $AUTHORITY = 'cpan:STEVAN'; extends 'MooseX::AttributeHelpers::Base'; +sub helper_type { 'Num' } + has '+method_constructors' => ( default => sub { return +{ @@ -22,21 +24,6 @@ has '+method_constructors' => ( } } ); - -sub _process_options_for_provides { - my ($self, $options) = @_; - (exists $options->{isa}) - || confess "You must define a type with the Counter metaclass"; - - my $isa = $options->{isa}; - - unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) { - $isa = find_type_constraint($isa); - } - - ($isa->is_a_type_of('Num')) - || confess "The type constraint for a Counter ($options->{isa}) must be a subtype of Num"; -} no Moose; no Moose::Util::TypeConstraints;