diff options
author | franck cuny <franck@lumberjaph.net> | 2010-02-10 14:10:29 +0100 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-02-10 14:10:29 +0100 |
commit | 747d1c90e721d4d2632a4ee9529e820586a29d79 (patch) | |
tree | 2b4f974c5bb5ede4ff5d243443bce7fc405ac00f | |
parent | meta class, with private and protected roles (diff) | |
download | moosex-privacy-747d1c90e721d4d2632a4ee9529e820586a29d79.tar.gz |
generate private and protected methods
-rw-r--r-- | lib/MooseX/Privacy/Meta/Method/Private.pm | 26 | ||||
-rw-r--r-- | lib/MooseX/Privacy/Meta/Method/Protected.pm | 29 |
2 files changed, 55 insertions, 0 deletions
diff --git a/lib/MooseX/Privacy/Meta/Method/Private.pm b/lib/MooseX/Privacy/Meta/Method/Private.pm new file mode 100644 index 0000000..9bcf68d --- /dev/null +++ b/lib/MooseX/Privacy/Meta/Method/Private.pm @@ -0,0 +1,26 @@ +package MooseX::Privacy::Meta::Method::Private; + +use Moose; +extends 'Moose::Meta::Method'; + +use Carp; + +sub new { + my $class = shift; + my %args = @_; + + my $method = delete $args{body}; + my $private_code = sub { + croak "The " + . $args{package_name} . "::" + . $args{name} + . " name method is private" + unless ( scalar caller() ) eq $args{package_name}; + + goto &{$method}; + }; + $args{body} = $private_code; + $class->SUPER::wrap(%args); +} + +1; diff --git a/lib/MooseX/Privacy/Meta/Method/Protected.pm b/lib/MooseX/Privacy/Meta/Method/Protected.pm new file mode 100644 index 0000000..3d4b88a --- /dev/null +++ b/lib/MooseX/Privacy/Meta/Method/Protected.pm @@ -0,0 +1,29 @@ +package MooseX::Privacy::Meta::Method::Protected; + +use Moose; +extends 'Moose::Meta::Method'; + +use Carp; + +sub new { + my $class = shift; + my %args = @_; + + my $method = delete $args{body}; + my $protected_code = sub { + my $caller = caller(); + croak "The " + . $args{package_name} . "::" + . $args{name} + . " name method is private" + unless $caller eq $args{package_name} + || $caller->isa( $args{package_name} ); + + goto &{$method}; + }; + $args{body} = $protected_code; + $class->SUPER::wrap(%args); +} + +1; + |