| 1 | package MooseX::RelatedClassRoles; | ||||||||||
| 2 | 1 | our $VERSION = '0.004'; | |||||||||
| 3 | |||||||||||
| 4 | # ABSTRACT: Apply roles to a class related to yours | ||||||||||
| 5 | 1 | ▶ | use MooseX::Role::Parameterized; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | parameter name => ( | |||||||||
| 8 | isa => 'Str', | ||||||||||
| 9 | required => 1, | ||||||||||
| 10 | ); | ||||||||||
| 11 | |||||||||||
| 12 | parameter class_accessor_name => ( | ||||||||||
| 13 | isa => 'Str', | ||||||||||
| 14 | lazy => 1, | ||||||||||
| 15 | 3 | ▶ | default => sub { $_[0]->name . '_class' }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 1 | ); | |||||||||
| 17 | |||||||||||
| 18 | parameter apply_method_name => ( | ||||||||||
| 19 | isa => 'Str', | ||||||||||
| 20 | lazy => 1, | ||||||||||
| 21 | 3 | ▶ | default => sub { 'apply_' . $_[0]->class_accessor_name . '_roles' }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 1 | ); | |||||||||
| 23 | |||||||||||
| 24 | # This is undocumented because you shouldn't use it unless you really know you | ||||||||||
| 25 | # have to. | ||||||||||
| 26 | 1 | parameter require_class_accessor => ( | |||||||||
| 27 | isa => 'Bool', | ||||||||||
| 28 | default => 1, | ||||||||||
| 29 | ); | ||||||||||
| 30 | |||||||||||
| 31 | 4 | ▶ | role { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 32 | 4 | my $p = shift; | |||||||||
| 33 | |||||||||||
| 34 | 4 | my $class_accessor_name = $p->class_accessor_name; | |||||||||
| 35 | 4 | my $apply_method_name = $p->apply_method_name; | |||||||||
| 36 | |||||||||||
| 37 | 4 | ▶ | requires $class_accessor_name | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | if $p->require_class_accessor; | ||||||||||
| 39 | |||||||||||
| 40 | 2 | ▶ | method $apply_method_name => sub { | ||||||||
|
Subroutine
apply_bar_class_roles: called CC=1 SCAR=0.0
| |||||||||||
| 41 | 2 | my $self = shift; | |||||||||
| 42 | 2 | my $meta = Moose::Meta::Class->create_anon_class( | |||||||||
| 43 | superclasses => [ $self->$class_accessor_name ], | ||||||||||
| 44 | roles => [ @_ ], | ||||||||||
| 45 | cache => 1, | ||||||||||
| 46 | ); | ||||||||||
| 47 | 2 | $self->$class_accessor_name($meta->name); | |||||||||
| 48 | 4 | }; | |||||||||
| 49 | 1 | }; | |||||||||
| 50 | |||||||||||
| 51 | 1 | ▶ | no MooseX::Role::Parameterized; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 52 | 1 | 1; | |||||||||
| 53 | |||||||||||
| 54 | |||||||||||
| 55 | |||||||||||
| 56 | |||||||||||
| 57 | =pod | ||||||||||
| 58 | |||||||||||
| 59 | =head1 NAME | ||||||||||
| 60 | |||||||||||
| 61 | MooseX::RelatedClassRoles - Apply roles to a class related to yours | ||||||||||
| 62 | |||||||||||
| 63 | =head1 VERSION | ||||||||||
| 64 | |||||||||||
| 65 | version 0.004 | ||||||||||
| 66 | |||||||||||
| 67 | =head1 SYNOPSIS | ||||||||||
| 68 | |||||||||||
| 69 | package My::Class; | ||||||||||
| 70 | use Moose; | ||||||||||
| 71 | |||||||||||
| 72 | has driver_class => ( | ||||||||||
| 73 | isa => 'MyApp::Driver', | ||||||||||
| 74 | ); | ||||||||||
| 75 | |||||||||||
| 76 | with 'MooseX::RelatedClassRoles' => { name => 'driver' }; | ||||||||||
| 77 | |||||||||||
| 78 | # ... | ||||||||||
| 79 | |||||||||||
| 80 | my $obj = My::Class->new(driver_class => "Some::Driver"); | ||||||||||
| 81 | $obj->apply_driver_class_roles("Other::Driver::Role"); | ||||||||||
| 82 | |||||||||||
| 83 | =head1 DESCRIPTION | ||||||||||
| 84 | |||||||||||
| 85 | Frequently, you have to use a class that provides some C<foo_class> accessor or | ||||||||||
| 86 | attribute as a method of dependency injection. Use this role when you'd rather | ||||||||||
| 87 | apply roles to make your custom C<foo_class> instead of manually setting up a | ||||||||||
| 88 | subclass. | ||||||||||
| 89 | |||||||||||
| 90 | =head1 PARAMETERS | ||||||||||
| 91 | |||||||||||
| 92 | =head2 name | ||||||||||
| 93 | |||||||||||
| 94 | A string naming the related class. C<driver> in the L</SYNOPSIS>. Required. | ||||||||||
| 95 | |||||||||||
| 96 | =head2 class_accessor_name | ||||||||||
| 97 | |||||||||||
| 98 | A string naming the related class accessor. C<driver_class> in the | ||||||||||
| 99 | L</SYNOPSIS>. Defaults to appending C<_class> to the C<name>. | ||||||||||
| 100 | |||||||||||
| 101 | =head2 apply_method_name | ||||||||||
| 102 | |||||||||||
| 103 | A string naming the role applying method. C<apply_driver_class_names> in the | ||||||||||
| 104 | L</SYNOPSIS>. Defaults to adding C<apply_> and C<_names> to the | ||||||||||
| 105 | C<class_accessor_name>. | ||||||||||
| 106 | |||||||||||
| 107 | =head1 BLAME | ||||||||||
| 108 | |||||||||||
| 109 | Florian Ragwitz (rafl) | ||||||||||
| 110 | |||||||||||
| 111 | =head1 AUTHOR | ||||||||||
| 112 | |||||||||||
| 113 | Hans Dieter Pearcey <hdp@cpan.org> | ||||||||||
| 114 | |||||||||||
| 115 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 116 | |||||||||||
| 117 | This software is copyright (c) 2009 by Hans Dieter Pearcey <hdp@cpan.org>. | ||||||||||
| 118 | |||||||||||
| 119 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 120 | the same terms as perl itself. | ||||||||||
| 121 | |||||||||||
| 122 | =cut | ||||||||||
| 123 | |||||||||||
| 124 | |||||||||||
| 125 | |||||||||||
| 126 | __END__ | ||||||||||