summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/MooseX/Privacy/Meta/Method/Private.pm26
-rw-r--r--lib/MooseX/Privacy/Meta/Method/Protected.pm29
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;
+