File Coverage

lib/UR/Role/PrototypeWithParams.pm
Criterion Covered Total %
statement 26 28 92.8
branch 3 6 50.0
condition 1 3 33.3
subroutine 9 9 100.0
pod 2 2 100.0
total 41 48 85.4


line stmt bran cond sub pod time code
1             package UR::Role::PrototypeWithParams;
2              
3 266     266   1118 use strict;
  266         379  
  266         7971  
4 266     266   966 use warnings;
  266         341  
  266         68792  
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 78     78 1 160 my($class, %params) = @_;
18 78 50 33     257 unless (exists($params{prototype}) and exists($params{role_params})) {
19 0         0 Carp::croak('prototype and role_params are required args to create()');
20             }
21 78         105 my $self = {};
22 78         220 @$self{'prototype', 'role_params'} = delete @params{'prototype','role_params'};
23 78 50       143 if (%params) {
24 0         0 Carp::croak('Unrecognized params to create(): ' . Data::Dumper::Dumper(\%params));
25             }
26              
27 78         204 return bless $self, $class;
28             }
29              
30             sub __role__ {
31 4     4   8 my $self = shift;
32 4         8 return $self;
33             }
34              
35             sub instantiate_role_instance {
36 52     52 1 70 my($self, $class_name) = @_;
37 52         109 my %create_args = ( role_name => $self->role_name, class_name => $class_name );
38 52 50       124 $create_args{role_params} = $self->role_params if $self->role_params;
39 52         281 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 254     254   567 $_[0]->{$accessor_name};
47             };
48 266     266   1128 no strict 'refs';
  266         353  
  266         23090  
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 1879     1879   3951 shift->{prototype}->$accessor_name(@_);
60             };
61 266     266   1005 no strict 'refs';
  266         367  
  266         10992  
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 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 to use in the C section
78             of a class description. See the "Parameterized Roles" section in L.
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 object.
96              
97             =back
98              
99             =head1 SEE ALSO
100              
101             L, L, L