From 64cab1daa8d6fe0182cef602be235a89f95987c7 Mon Sep 17 00:00:00 2001 From: franck cuny Date: Mon, 1 Mar 2010 22:17:49 +0100 Subject: start to implement private and protected attributes --- lib/MooseX/Privacy/Meta/Attribute/Private.pm | 21 +++++++++++++++++++++ lib/MooseX/Privacy/Meta/Attribute/Protected.pm | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 lib/MooseX/Privacy/Meta/Attribute/Private.pm create mode 100644 lib/MooseX/Privacy/Meta/Attribute/Protected.pm (limited to 'lib/MooseX/Privacy/Meta') 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; -- cgit 1.4.1