package MooseX::Meta::Method::Accessor::Restricted; {
	use Moose::Role;
	use version;
	
	use UNIVERSAL ();

=head1 NAME

MooseX::Meta::Method::Accessor::Restricted - accessor role that implements public, private and protected attributes

=head1 VERSION

Version 0.1.0

=cut
	
	our $VERSION = version->new('0.1.0');
	our $AUTH = 'cpan:AANKHEN';

=head1 SYNOPSIS

	package Moose::Meta::Method::Accessor::Custom::Mine; {
		use Moose;
		
		extends 'Moose::Meta::Method::Accessor';
		with 'MooseX::Meta::Method::Accessor::Restricted';
	};

=head1 DESCRIPTION

This is the second part of the implementation of access restrictions on top of Moose.  Please see L<MooseX::Attribute::Restricted> for more detailed information and examples.

=head1 AUTHOR

Aankhen, C<< <aankhen-spamtrap{at}gmail{dot}com> >> (obviously, remove the -spamtrap)

=head1 COPYRIGHT AND LICENCE

Copyright 2007 Aankhen, all rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

	use Scalar::Util 'blessed';
	
	override '_inline_pre_body' => sub {
		my $self = shift;
		my $attr = $self->associated_attribute;

		my $pre = <<'EOC';
my $instance = $_[0];

my $calling_pkg = caller;
my $attr_pkg = $attr->associated_class->name;
my $attr_name = $attr->name;
EOC
		
		if ($attr->private) {
			return $pre . <<'EOC';
confess "$attr_name is a private attribute"
	unless $calling_pkg eq $attr_pkg;
EOC
		} elsif ($attr->protected) {
			my $class = $attr->associated_class;
			my $test = ($class->isa('Moose::Meta::Role')) ? 'UNIVERSAL::can($calling_pkg, \'does\') && $calling_pkg->does($attr_pkg)' : 'UNIVERSAL::isa($calling_pkg, $attr_pkg)';
			return qq{
				$pre
				
				confess "\$attr_name is a protected attribute"
					unless $test;
			};
		} else {
			return '';
		}
	};
};

1;
