File Coverage

blib/lib/MooseX/RelatedClassRoles.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 15 15 100.0


line stmt bran cond sub pod time code
1             package MooseX::RelatedClassRoles;
2             our $VERSION = '0.004';
3              
4             # ABSTRACT: Apply roles to a class related to yours
5 1     1   5336 use MooseX::Role::Parameterized;
  1         79271  
  1         5  
6              
7             parameter name => (
8             isa => 'Str',
9             required => 1,
10             );
11              
12             parameter class_accessor_name => (
13             isa => 'Str',
14             lazy => 1,
15             default => sub { $_[0]->name . '_class' },
16             );
17              
18             parameter apply_method_name => (
19             isa => 'Str',
20             lazy => 1,
21             default => sub { 'apply_' . $_[0]->class_accessor_name . '_roles' },
22             );
23              
24             # This is undocumented because you shouldn't use it unless you really know you
25             # have to.
26             parameter require_class_accessor => (
27             isa => 'Bool',
28             default => 1,
29             );
30              
31             role {
32             my $p = shift;
33              
34             my $class_accessor_name = $p->class_accessor_name;
35             my $apply_method_name = $p->apply_method_name;
36              
37             requires $class_accessor_name
38             if $p->require_class_accessor;
39              
40             method $apply_method_name => sub {
41 2     2   4883 my $self = shift;
        2      
        2      
        2      
42 2         32 my $meta = Moose::Meta::Class->create_anon_class(
43             superclasses => [ $self->$class_accessor_name ],
44             roles => [ @_ ],
45             cache => 1,
46             );
47 2         4195 $self->$class_accessor_name($meta->name);
48             };
49             };
50              
51 1     1   33978 no MooseX::Role::Parameterized;
  1         2  
  1         11  
52             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__
127