summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--lib/MooseX/Privacy/Meta/Class/Private.pm12
-rw-r--r--lib/MooseX/Privacy/Meta/Class/Protected.pm13
-rw-r--r--t/10_private_method.t25
-rw-r--r--t/11_protected_method.t26
4 files changed, 70 insertions, 6 deletions
diff --git a/lib/MooseX/Privacy/Meta/Class/Private.pm b/lib/MooseX/Privacy/Meta/Class/Private.pm
index 7cea57c..0482d08 100644
--- a/lib/MooseX/Privacy/Meta/Class/Private.pm
+++ b/lib/MooseX/Privacy/Meta/Class/Private.pm
@@ -15,12 +15,20 @@ has local_private_methods => (
);
sub add_private_method {
- my ( $self, $method_name, $code ) = @_;
+ my $self = shift;
+ my ( $method_name, $body );
+ if ( scalar @_ == 1 ) {
+ $method_name = $_[0]->name;
+ $body = $_[0]->body;
+ }
+ else {
+ ( $method_name, $body ) = @_;
+ }
$self->add_method(
$method_name,
MooseX::Privacy::Meta::Method::Private->wrap(
name => $method_name,
- body => $code,
+ body => $body,
package_name => $self->name
)
);
diff --git a/lib/MooseX/Privacy/Meta/Class/Protected.pm b/lib/MooseX/Privacy/Meta/Class/Protected.pm
index aa5936e..3b80010 100644
--- a/lib/MooseX/Privacy/Meta/Class/Protected.pm
+++ b/lib/MooseX/Privacy/Meta/Class/Protected.pm
@@ -15,12 +15,20 @@ has local_protected_methods => (
);
sub add_protected_method {
- my ( $self, $method_name, $code ) = @_;
+ my $self = shift;
+ my ( $method_name, $body );
+ if ( scalar @_ == 1 ) {
+ $method_name = $_[0]->name;
+ $body = $_[0]->body;
+ }
+ else {
+ ($method_name, $body) = @_;
+ }
$self->add_method(
$method_name,
MooseX::Privacy::Meta::Method::Protected->wrap(
name => $method_name,
- body => $code,
+ body => $body,
package_name => $self->name
)
);
@@ -29,3 +37,4 @@ sub add_protected_method {
1;
+
diff --git a/t/10_private_method.t b/t/10_private_method.t
index 6ed9055..dabc8c9 100644
--- a/t/10_private_method.t
+++ b/t/10_private_method.t
@@ -1,7 +1,7 @@
use strict;
use warnings;
-use Test::More tests => 7;
+use Test::More tests => 9;
use Test::Exception;
{
@@ -31,6 +31,16 @@ use Test::Exception;
return 'foobar' . $str;
};
+ sub add_public_method {
+ my $self = shift;
+ $self->meta->add_method(
+ 'public_foo',
+ sub {
+ $self->private_meta_method;
+ }
+ );
+ }
+
}
{
@@ -57,3 +67,16 @@ dies_ok { $bar->newbar() } "... can't call bar, method is private";
is scalar @{ $foo->meta->local_private_methods }, 2,
'... got two privates method';
+
+my $private_method = Class::MOP::Method->wrap(
+ sub { return 23 },
+ name => 'private_meta_method',
+ package_name => 'Foo'
+);
+
+$foo->meta->add_private_method($private_method);
+
+dies_ok { $foo->private_meta_method } '... can\'t call the private method';
+
+$foo->add_public_method;
+is $foo->public_foo, 23, '... call private method via public method';
diff --git a/t/11_protected_method.t b/t/11_protected_method.t
index 587979b..13e03e1 100644
--- a/t/11_protected_method.t
+++ b/t/11_protected_method.t
@@ -1,7 +1,7 @@
use strict;
use warnings;
-use Test::More tests => 5;
+use Test::More tests => 7;
use Test::Exception;
{
@@ -26,6 +26,17 @@ use Test::Exception;
my $self = shift;
return $self->bar;
}
+
+ sub add_public_method {
+ my $self = shift;
+ $self->meta->add_method(
+ 'public_foo',
+ sub {
+ $self->protected_meta_method;
+ }
+ );
+ }
+
}
my $foo = Foo->new();
@@ -38,3 +49,16 @@ is $bar->baz(), 'baz', "... got the good value from &bar";
is scalar @{ $foo->meta->local_protected_methods }, 1,
'... got one protected method';
+
+my $protected_method = Class::MOP::Method->wrap(
+ sub { return 23 },
+ name => 'protected_meta_method',
+ package_name => 'Foo'
+);
+
+$foo->meta->add_protected_method($protected_method);
+
+dies_ok { $foo->protected_meta_method } '... can\'t call the protected method';
+
+$bar->add_public_method;
+is $bar->public_foo, 23, '... call protected method via public method';