From fb5f12ee5364f957e1cd20928f5dfe47e207c2a6 Mon Sep 17 00:00:00 2001 From: franck cuny Date: Tue, 8 Jun 2010 09:57:56 +0200 Subject: rewrite meta API: use _net_api_ in name, add find_net_api_method_by_name --- lib/MooseX/Net/API/Meta/Method/APIMethod.pm | 70 ++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 15 deletions(-) (limited to 'lib/MooseX/Net/API') diff --git a/lib/MooseX/Net/API/Meta/Method/APIMethod.pm b/lib/MooseX/Net/API/Meta/Method/APIMethod.pm index ac57edf..791c8f8 100644 --- a/lib/MooseX/Net/API/Meta/Method/APIMethod.pm +++ b/lib/MooseX/Net/API/Meta/Method/APIMethod.pm @@ -7,7 +7,7 @@ use MooseX::Net::API::Error; use MooseX::Net::API::Meta::Method; use MooseX::Types::Moose qw/Str ArrayRef/; -has local_api_methods => ( +has local_net_api_methods => ( traits => ['Array'], is => 'rw', isa => ArrayRef [Str], @@ -15,15 +15,34 @@ has local_api_methods => ( default => sub { [] }, auto_deref => 1, handles => { - find_api_method_by_name => 'grep', - add_api_method => 'push', - get_all_api_methods => 'elements', + _find_net_api_method_by_name => 'first', + _add_net_api_method => 'push', + get_all_net_api_methods => 'elements', }, ); +sub find_net_api_method_by_name { + my ($meta, $name) = @_; + my $method_name = $meta->_find_net_api_method_by_name(sub{/$name/}); + return unless $method_name; + my $method = $meta->find_method_by_name($method_name); + if ($method->isa('Class::MOP::Method::Wrapped')) { + return $method->get_original_method; + }else{ + return $method; + } +} + +sub remove_net_api_method { + my ($meta, $name) = @_; + my @methods = grep { !/$name/ } $meta->get_all_api_methods; + $meta->local_api_methods(\@methods); + $meta->remove_method($name); +} + before add_net_api_method => sub { my ($meta, $name) = @_; - if (my @method = $meta->find_api_method_by_name(sub {/^$name$/})) { + if ($meta->find_net_api_method_by_name(sub {/^$name$/})) { die MooseX::Net::API::Error->new( reason => "method '$name' is already declared in " . $meta->name); } @@ -32,9 +51,10 @@ before add_net_api_method => sub { sub add_net_api_method { my ($meta, $name, %options) = @_; - # XXX accept blessed method + # XXX accept blessed method ? my $code = delete $options{code}; + $meta->add_method( $name, MooseX::Net::API::Meta::Method->wrap( @@ -44,11 +64,11 @@ sub add_net_api_method { %options ), ); - $meta->add_api_method($name); + $meta->_add_net_api_method($name); } after add_net_api_method => sub { - my ($meta, $name, %options) = @_; + my ($meta, $name) = @_; $meta->add_before_method_modifier( $name, sub { @@ -60,15 +80,35 @@ after add_net_api_method => sub { ); }; -sub remove_net_api_method { - my ($meta, $name) = @_; - my @methods = grep { !/$name/ } $meta->get_all_api_methods; - $meta->local_api_methods(\@methods); - $meta->remove_method($name); -} - 1; =head1 SYNOPSIS + my $api_client = MyAPI->new; + + my @methods = $api_client->meta->get_all_api_methods(); + + my $method = $api_client->meta->find_net_api_method_by_name('users'); + + $api_client->meta->remove_net_api_method($method); + + $api_client->meta->add_net_api_method('users', sub {...}, + description => 'this method does...',); + =head1 DESCRIPTION + +=method get_all_net_api_methods + +Return a list of net api methods + +=method find_net_api_method_by_name + +Return a net api method + +=method remove_net_api_method + +Remove a net api method + +=method add_net_api_method + +Add a net api method -- cgit 1.4.1