From: Stevan Little Date: Fri, 1 Jun 2007 16:12:49 +0000 (+0000) Subject: adding Role/Class support for method providers X-Git-Tag: 0.18_01~74 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8e3fab6d99875597bfae0e77329ec7516a7ff5df;p=gitmo%2FMooseX-AttributeHelpers.git adding Role/Class support for method providers --- diff --git a/lib/MooseX/AttributeHelpers/Base.pm b/lib/MooseX/AttributeHelpers/Base.pm index 533c083..e195198 100644 --- a/lib/MooseX/AttributeHelpers/Base.pm +++ b/lib/MooseX/AttributeHelpers/Base.pm @@ -8,18 +8,45 @@ our $AUTHORITY = 'cpan:STEVAN'; extends 'Moose::Meta::Attribute'; -has 'method_constructors' => ( - is => 'ro', - isa => 'HashRef', - default => sub { {} } -); - +# this is the method map you define ... has 'provides' => ( is => 'ro', isa => 'HashRef', required => 1, ); + +# these next two are the possible methods +# you can use in the 'provides' map. + +# provide a Class or Role which we can +# collect the method providers from +has 'method_provider' => ( + is => 'ro', + isa => 'ClassName', + predicate => 'has_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 +has 'method_constructors' => ( + is => 'ro', + isa => 'HashRef', + lazy => 1, + default => sub { + my $self = shift; + return +{} unless $self->has_method_provider; + # or grab them from the role/class + my $method_provider = $self->method_provider->meta; + return +{ + map { + $_ => $method_provider->get_method($_) + } $method_provider->get_method_list + }; + } +); + # extend the parents stuff to make sure # certain bits are now required ... has '+$!default' => (required => 1); @@ -60,7 +87,9 @@ before '_process_options' => sub { # 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"; diff --git a/lib/MooseX/AttributeHelpers/Counter.pm b/lib/MooseX/AttributeHelpers/Counter.pm index 0ab0d71..0f196ad 100644 --- a/lib/MooseX/AttributeHelpers/Counter.pm +++ b/lib/MooseX/AttributeHelpers/Counter.pm @@ -5,24 +5,15 @@ use Moose; our $VERSION = '0.01'; our $AUTHORITY = 'cpan:STEVAN'; -extends 'MooseX::AttributeHelpers::Base'; +use MooseX::AttributeHelpers::MethodProvider::Counter; -sub helper_type { 'Num' } +extends 'MooseX::AttributeHelpers::Base'; -has '+method_constructors' => ( - default => sub { - return +{ - inc => sub { - my $attr = shift; - return sub { $attr->set_value($_[0], $attr->get_value($_[0]) + 1) }; - }, - dec => sub { - my $attr = shift; - return sub { $attr->set_value($_[0], $attr->get_value($_[0]) - 1) }; - }, - } - } +has '+method_provider' => ( + default => 'MooseX::AttributeHelpers::MethodProvider::Counter' ); + +sub helper_type { 'Num' } no Moose; diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/Counter.pm b/lib/MooseX/AttributeHelpers/MethodProvider/Counter.pm new file mode 100644 index 0000000..ca85b04 --- /dev/null +++ b/lib/MooseX/AttributeHelpers/MethodProvider/Counter.pm @@ -0,0 +1,21 @@ + +package MooseX::AttributeHelpers::MethodProvider::Counter; +use Moose::Role; + +sub inc { + my $attr = shift; + return sub { $attr->set_value($_[0], $attr->get_value($_[0]) + 1) }; +} + +sub dec { + my $attr = shift; + return sub { $attr->set_value($_[0], $attr->get_value($_[0]) - 1) }; +} + +1; + +__END__ + +=pod + +=cut \ No newline at end of file