Beta Shell
v2.0 ยท web2.us.cloudlogin.co
[FM]
[CMD]
[PHP]
[DB]
[INFO]
[SEC]
File Manager
~
/
usr
/
share
/
doc
/
perl-Moose
/
t
/
recipes
Upload
21 items
Name
Size
Perms
Modified
Actions
[ .. / .. ]
basics_bankaccount_methodmodifiersandsubclassing.t
3.71 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
basics_binarytree_attributefeatures.t
3.83 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
basics_company_subtypes.t
9.54 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
basics_datetime_extendingnonmooseparent.t
1.16 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
basics_document_augmentandinner.t
1.45 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
basics_genome_overloadingsubtypesandcoercion.t
5.2 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
basics_http_subtypesandcoercion.t
3.24 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
basics_point_attributesandsubclassing.t
5.35 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
extending_debugging_baseclassrole.t
755 B
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
extending_mooseish_moosesugar.t
983 B
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
legacy_debugging_baseclassreplacement.t
953 B
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
legacy_labeled_attributemetaclass.t
1.52 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
meta_globref_instancemetaclass.t
3.23 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
meta_labeled_attributetrait.t
1.43 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
meta_privateorpublic_methodmetaclass.t
1.93 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
meta_table_metaclasstrait.t
798 B
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
roles_applicationtoinstance.t
1.73 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
roles_comparable_codereuse.t
5.16 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
roles_restartable_advancedcomposition.t
2 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
Editing: meta_privateorpublic_methodmetaclass.t
(1.93 KB)
Path: /usr/share/doc/perl-Moose/t/recipes/meta_privateorpublic_methodmetaclass.t
Back
#!/usr/bin/perl -w use strict; use Test::More 'no_plan'; use Test::Fatal; $| = 1; # =begin testing SETUP { package MyApp::Meta::Method::PrivateOrPublic; use Moose; use Moose::Util::TypeConstraints; extends 'Moose::Meta::Method'; has '_policy' => ( is => 'ro', isa => enum( [ qw( public private ) ] ), default => 'public', init_arg => 'policy', ); sub new { my $class = shift; my %options = @_; my $self = $class->SUPER::wrap(%options); $self->{_policy} = $options{policy}; $self->_add_policy_wrapper; return $self; } sub _add_policy_wrapper { my $self = shift; return if $self->is_public; my $name = $self->name; my $package = $self->package_name; my $real_body = $self->body; my $body = sub { die "The $package\::$name method is private" unless ( scalar caller() ) eq $package; goto &{$real_body}; }; $self->{body} = $body; } sub is_public { $_[0]->_policy eq 'public' } sub is_private { $_[0]->_policy eq 'private' } package MyApp::User; use Moose; has 'password' => ( is => 'rw' ); __PACKAGE__->meta()->add_method( '_reset_password', MyApp::Meta::Method::PrivateOrPublic->new( name => '_reset_password', package_name => __PACKAGE__, body => sub { $_[0]->password('reset') }, policy => 'private', ) ); } # =begin testing { package main; use strict; use warnings; use Test::Fatal; my $user = MyApp::User->new( password => 'foo!' ); like( exception { $user->_reset_password }, qr/The MyApp::User::_reset_password method is private/, '_reset_password method dies if called outside MyApp::User class'); { package MyApp::User; sub run_reset { $_[0]->_reset_password } } $user->run_reset; is( $user->password, 'reset', 'password has been reset' ); } 1;