summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-03-01 22:17:49 +0100
committerfranck cuny <franck@lumberjaph.net>2010-03-01 22:17:49 +0100
commit64cab1daa8d6fe0182cef602be235a89f95987c7 (patch)
treebb5850cb365540931e02b1f08c445421169a9cd4
parenttrait for protected and private attributes (diff)
downloadmoosex-privacy-64cab1daa8d6fe0182cef602be235a89f95987c7.tar.gz
start to implement private and protected attributes
Diffstat (limited to '')
-rw-r--r--lib/MooseX/Privacy/Meta/Attribute/Private.pm21
-rw-r--r--lib/MooseX/Privacy/Meta/Attribute/Protected.pm21
-rw-r--r--lib/MooseX/Privacy/Trait/Private.pm15
-rw-r--r--lib/MooseX/Privacy/Trait/Protected.pm15
4 files changed, 72 insertions, 0 deletions
diff --git a/lib/MooseX/Privacy/Meta/Attribute/Private.pm b/lib/MooseX/Privacy/Meta/Attribute/Private.pm
new file mode 100644
index 0000000..84f9d4f
--- /dev/null
+++ b/lib/MooseX/Privacy/Meta/Attribute/Private.pm
@@ -0,0 +1,21 @@
+package MooseX::Privacy::Meta::Attribute::Private;
+
+use Moose::Role;
+use Carp qw/confess/;
+
+sub _generate_accessor_method {
+    my $attr         = (shift)->associated_attribute;
+    my $package_name = $attr->associated_class->name;
+
+    return sub {
+        my $self   = shift;
+        my $caller = ( scalar caller() );
+        confess "Attribute " . $attr->name . " is private"
+            unless $caller eq $package_name;
+        $attr->set_value( $self, $_[0] ) if scalar(@_) == 1;
+        $attr->set_value( $self, [@_] ) if scalar(@_) > 1;
+        $attr->get_value($self);
+    };
+}
+
+1;
diff --git a/lib/MooseX/Privacy/Meta/Attribute/Protected.pm b/lib/MooseX/Privacy/Meta/Attribute/Protected.pm
new file mode 100644
index 0000000..408ab50
--- /dev/null
+++ b/lib/MooseX/Privacy/Meta/Attribute/Protected.pm
@@ -0,0 +1,21 @@
+package MooseX::Privacy::Meta::Attribute::Protected;
+
+use Moose::Role;
+use Carp qw/confess/;
+
+sub _generate_accessor_method {
+    my $attr         = (shift)->associated_attribute;
+    my $package_name = $attr->associated_class->name;
+
+    return sub {
+        my $self   = shift;
+        my $caller = ( scalar caller() );
+        confess "Attribute " . $attr->name . " is protected"
+            unless $caller eq $package_name || $caller->isa($package_name);
+        $attr->set_value( $self, $_[0] ) if scalar(@_) == 1;
+        $attr->set_value( $self, [@_] ) if scalar(@_) > 1;
+        $attr->get_value($self);
+    };
+}
+
+1;
diff --git a/lib/MooseX/Privacy/Trait/Private.pm b/lib/MooseX/Privacy/Trait/Private.pm
new file mode 100644
index 0000000..c588032
--- /dev/null
+++ b/lib/MooseX/Privacy/Trait/Private.pm
@@ -0,0 +1,15 @@
+package MooseX::Privacy::Trait::Private;
+
+use Moose::Role;
+
+around accessor_metaclass => sub {
+    my ( $orig, $self, @rest ) = @_;
+
+    return Moose::Meta::Class->create_anon_class(
+        superclasses => [ $self->$orig(@_) ],
+        roles        => ['MooseX::Privacy::Meta::Attribute::Private'],
+        cache        => 1
+    )->name;
+};
+
+1;
diff --git a/lib/MooseX/Privacy/Trait/Protected.pm b/lib/MooseX/Privacy/Trait/Protected.pm
new file mode 100644
index 0000000..7a8037c
--- /dev/null
+++ b/lib/MooseX/Privacy/Trait/Protected.pm
@@ -0,0 +1,15 @@
+package MooseX::Privacy::Trait::Protected;
+
+use Moose::Role;
+
+around accessor_metaclass => sub {
+    my ( $orig, $self, @rest ) = @_;
+
+    return Moose::Meta::Class->create_anon_class(
+        superclasses => [ $self->$orig(@_) ],
+        roles        => ['MooseX::Privacy::Meta::Attribute::Protected'],
+        cache        => 1
+    )->name;
+};
+
+1;