blib/lib/UR/Role/PrototypeWithParams.pm untested

filter: Statementstmt 0.0% 0 / 35 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 54 SCAR 30.0
CC
4
Cov
0%
create
12.0
instantiate_role_instance
6.0
__role__
2.0
CRAP 20.0
1 package UR::Role::PrototypeWithParams;
2  
3 use strict;
4 use warnings;
5  
6 our $VERSION = "0.46"; # UR $VERSION;;
7  
8 # A plain-perl class to represent a role prototype bound to a set of params.
9 # It exists ephemerally as a class is composing its roles when using this
10 # syntax:
11 #
12 # class The::Class {
13 # roles => [ The::Role->create(param => 'value') ],
14 # };
15  
16 sub create {
17 my($class, %params) = @_;
18 unless (exists($params{prototype}) and exists($params{role_params})) {
19 Carp::croak('prototype and role_params are required args to create()');
20 }
21 my $self = {};
22 @$self{'prototype', 'role_params'} = delete @params{'prototype','role_params'};
23 if (%params) {
24 Carp::croak('Unrecognized params to create(): ' . Data::Dumper::Dumper(\%params));
25 }
26  
27 return bless $self, $class;
28 }
29  
30 sub __role__ {
31 my $self = shift;
32 return $self;
33 }
34  
35 sub instantiate_role_instance {
36 my($self, $class_name) = @_;
37 my %create_args = ( role_name => $self->role_name, class_name => $class_name );
38 $create_args{role_params} = $self->role_params if $self->role_params;
39 return UR::Role::Instance->create(%create_args);
40 }
41  
42  
43 # direct accessors
44 foreach my $accessor_name ( qw( prototype role_params ) ) {
45 my $sub = sub {
46 $_[0]->{$accessor_name};
47 };
48 no strict 'refs';
49 *$accessor_name = $sub;
50 }
51  
52 # accessors that delegate to the role prototype
53 foreach my $accessor_name ( qw( role_name methods overloads has requires attributes_have excludes
54 id_by_property_names has_property_names property_data method_names
55 meta_properties_to_compose_into_classes method_modifiers ),
56 UR::Role::Prototype::meta_properties_to_compose_into_classes()
57 ) {
58 my $sub = sub {
59 shift->{prototype}->$accessor_name(@_);
60 };
61 no strict 'refs';
62 *$accessor_name = $sub;
63 }
64  
65 1;
66  
67 =pod
68  
69 =head1 NAME
70  
71 UR::Role::PrototypeWithParams - Binds a set of params to a role
72  
73 =head1 DESCRIPTION
74  
75 Objects of this class are returned when calling C<create()> on a role's class.
76 They exist temporarily as a class is being defined as a means of binding a
77 set of role params to a L<UR::Role::Prototype> to use in the C<roles> section
78 of a class description. See the "Parameterized Roles" section in L<UR::Role>.
79  
80 =head2 Methods
81  
82 =over 4
83  
84 =item create(prototype => $role_proto, role_params => $hashref)
85  
86 The constructor. Both arguments are required.
87  
88 =item __role__()
89  
90 Returns itself. Used by the role composition mechanism to trigger autoloading
91 the role's module when role names are given as strings in a class definition.
92  
93 =item instantiate_role_instance($class_name)
94  
95 Return a L<UR::Role::Instance> object.
96  
97 =back
98  
99 =head1 SEE ALSO
100  
101 L<UR::Role>, L<UR::Role::Prototype>, L<UR::Role::Instance>