X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FAttributeHelpers%2FTrait%2FBase.pm;h=44bb4f96ba962986343adb770ed3cb519753b080;hb=4a35a0ad2542b236a6ea02022d55c4193a58a321;hp=6da6eae57431137c633ebf48540432c96528cd65;hpb=dbd51f3079bc5debaa79062c558ac4f47e8d374b;p=gitmo%2FMooseX-AttributeHelpers.git diff --git a/lib/MooseX/AttributeHelpers/Trait/Base.pm b/lib/MooseX/AttributeHelpers/Trait/Base.pm index 6da6eae..44bb4f9 100644 --- a/lib/MooseX/AttributeHelpers/Trait/Base.pm +++ b/lib/MooseX/AttributeHelpers/Trait/Base.pm @@ -3,7 +3,8 @@ package MooseX::AttributeHelpers::Trait::Base; use Moose::Role; use Moose::Util::TypeConstraints; -our $VERSION = '0.04'; +our $VERSION = '0.17'; +$VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; requires 'helper_type'; @@ -15,6 +16,11 @@ has 'provides' => ( default => sub {{}} ); +has 'curries' => ( + is => 'ro', + isa => 'HashRef', + default => sub {{}} +); # these next two are the possible methods # you can use in the 'provides' map. @@ -46,7 +52,7 @@ has 'method_constructors' => ( # extend the parents stuff to make sure # certain bits are now required ... -has '+$!default' => (required => 1); +has '+default' => (required => 1); has '+type_constraint' => (required => 1); ## Methods called prior to instantiation @@ -76,7 +82,7 @@ before '_process_options' => sub { ## methods called after instantiation -# this confirms that provides has +# this confirms that provides (and curries) has # all valid possibilities in it sub check_provides_values { my $self = shift; @@ -87,6 +93,33 @@ sub check_provides_values { (exists $method_constructors->{$key}) || confess "$key is an unsupported method type"; } + + foreach my $key (keys %{$self->curries}) { + (exists $method_constructors->{$key}) + || confess "$key is an unsupported method type"; + } +} + +sub _curry { + my $self = shift; + my $code = shift; + + my @args = @_; + return sub { + my $self = shift; + $code->($self, @args, @_) + }; +} + +sub _curry_sub { + my $self = shift; + my $body = shift; + my $code = shift; + + return sub { + my $self = shift; + $code->($self, $body, @_) + }; } after 'install_accessors' => sub { @@ -108,6 +141,41 @@ after 'install_accessors' => sub { my $class_name = $class->name; + while (my ($constructor, $constructed) = each %{$attr->curries}) { + my $method_code; + while (my ($curried_name, $curried_arg) = each(%$constructed)) { + if ($class->has_method($curried_name)) { + confess + "The method ($curried_name) already ". + "exists in class (" . $class->name . ")"; + } + my $body = $method_constructors->{$constructor}->( + $attr, + $attr_reader, + $attr_writer, + ); + + if (ref $curried_arg eq 'ARRAY') { + $method_code = $attr->_curry($body, @$curried_arg); + } + elsif (ref $curried_arg eq 'CODE') { + $method_code = $attr->_curry_sub($body, $curried_arg); + } + else { + confess "curries parameter must be ref type HASH or CODE"; + } + + my $method = MooseX::AttributeHelpers::Meta::Method::Curried->wrap( + $method_code, + package_name => $class_name, + name => $curried_name, + ); + + $attr->associate_method($method); + $class->add_method($curried_name => $method); + } + } + foreach my $key (keys %{$attr->provides}) { my $method_name = $attr->provides->{$key}; @@ -134,6 +202,8 @@ after 'install_accessors' => sub { after 'remove_accessors' => sub { my $attr = shift; my $class = $attr->associated_class; + + # provides accessors foreach my $key (keys %{$attr->provides}) { my $method_name = $attr->provides->{$key}; my $method = $class->get_method($method_name); @@ -141,6 +211,15 @@ after 'remove_accessors' => sub { if blessed($method) && $method->isa('MooseX::AttributeHelpers::Meta::Method::Provided'); } + + # curries accessors + foreach my $key (keys %{$attr->curries}) { + my $method_name = $attr->curries->{$key}; + my $method = $class->get_method($method_name); + $class->remove_method($method_name) + if blessed($method) && + $method->isa('MooseX::AttributeHelpers::Meta::Method::Provided'); + } }; no Moose::Role;