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

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 160 SCAR 53.5
CC
14
Cov
0%
_visit_params_with_values_in_struct
56.0
_search_for_invocant_role_instance
30.0
_constructor
6.0
CRAP 210.0
1 package UR::Role::Param;
2  
3 use strict;
4 use warnings;
5  
6 use Carp qw();
7 use Scalar::Util qw(blessed);
8  
9 our $VERSION = "0.46"; # UR $VERSION;;
10  
11 my %all_params;
12  
13 sub _constructor {
14 my($class, %params) = @_;
15 foreach my $param_name ( qw( role_name name varref state ) ) {
16 Carp::croak("$param_name is a required param") unless exists $params{$param_name};
17 }
18 $all_params{$params{role_name}}->{$params{name}} = bless \%params, $class;
19 }
20  
21 sub new {
22 my $class = shift;
23 return $class->_constructor(@_, state => 'unbound');
24 }
25
26 sub TIESCALAR {
27 my $class = shift;
28 return $class->_constructor(@_, state => 'bound');
29 }
30  
31 sub name { shift->{name} }
32 sub role_name { shift->{role_name} }
33 sub varref { shift->{varref} }
34 sub state { shift->{state} }
35  
36 sub FETCH {
37 my $self = shift;
38 my $param_name = $self->name;
39  
40 my $role_instance = $self->_search_for_invocant_role_instance();
41 unless ($role_instance) {
42 Carp::confess("Role param '$param_name' is not bound to a value in this call frame");
43 }
44 my $params = $role_instance->role_params();
45 return $params->{$param_name};
46 }
47  
48 sub STORE {
49 my $self = shift;
50 my $name = $self->name;
51 Carp::croak("Role param '$name' is read-only");
52 }
53  
54 sub _search_for_invocant_role_instance {
55 my $self = shift;
56  
57 local $@;
58 for (my $frame = 1; ; $frame++) {
59 my($role_package, $invocant) = do {
60 package DB;
61 my @caller = caller($frame);
62 last unless $caller[3];
63 my($function_package) = $caller[3] =~ m/^(.*)::\w+$/;
64 next unless $function_package;
65 eval { ($function_package, $DB::args[0]) };
66 };
67 my $invocant_class = blessed($invocant) || (!ref($invocant) && $invocant);
68 next unless $invocant_class;
69  
70 my $role_instance = UR::Role::Instance->get(role_name => $role_package, 'class_name isa' => $invocant_class);
71 return $role_instance if $role_instance;
72 }
73 return;
74 }
75  
76 sub clone_self {
77 # used by Clone:PP (UR::Util::deep_copy), to prevent recursing into
78 # these Param objects. Otherwise, the cloning done when a Role's
79 # property data is cloned before merging it into the class would point
80 # this object's varref to an anonymous scalar other than the original
81 # variable with The RoleParam attribute, and the cloning process doesn't
82 # properly re-tie the RoleParam variables afterward.
83 return $_[0];
84 }
85  
86 sub param_names_for_role {
87 my($class, $role_name) = @_;
88 return keys(%{ $all_params{$role_name} });
89 }
90  
91 sub replace_unbound_params_in_struct_with_values {
92 my($class, $struct, @role_objects) = @_;
93  
94 my %role_params = map { $_->role_name => $_->role_params } @role_objects;
95  
96 my $replacer = sub {
97 my $ref = shift;
98  
99 my $self = $$ref;
100 my $role_params = $role_params{$self->role_name};
101 $$ref = $role_params->{$self->name}; # replaces value in structure
102  
103 # replace the role param variable
104 my $role_param_ref = $self->varref;
105 unless (tied($$role_param_ref)) {
106 tie $$role_param_ref, 'UR::Role::Param',
107 name => $self->name,
108 role_name => $self->role_name,
109 varref => $self->varref;
110 }
111 };
112  
113 _visit_params_with_values_in_struct($struct, $replacer);
114 }
115  
116 sub _is_unbound_param {
117 my $val = shift;
118 return (blessed($val) && $val->isa(__PACKAGE__) && $val->state eq 'unbound');
119 }
120  
121 sub _visit_params_with_values_in_struct {
122 my($struct, $cb) = @_;
123  
124 return unless my $reftype = ref($struct);
125 if ($reftype eq 'HASH') {
126 while(my($key, $val) = each %$struct) {
127 if (_is_unbound_param($val)) {
128 $cb->(\$struct->{$key});
129 } else {
130 _visit_params_with_values_in_struct($val, $cb);
131 }
132 }
133 } elsif ($reftype eq 'ARRAY') {
134 for(my $i = 0; $i < @$struct; $i++) {
135 my $val = $struct->[$i];
136 if (_is_unbound_param($val)) {
137 $cb->(\$struct->[$i]);
138 } else {
139 _visit_params_with_values_in_struct($val, $cb);
140 }
141 }
142 } elsif ($reftype eq 'SCALAR') {
143 _visit_params_with_values_in_struct($struct, $cb);
144 }
145 }
146  
147 1;
148  
149 =pod
150  
151 =head1 NAME
152  
153 UR::Role::Param - Role parameters as package variables
154  
155 =head1 SYNOPSIS
156  
157 package ProjectNamespace::LoggingRole;
158 use ProjectNamespace;
159  
160 our $logging_object : RoleParam(logging_obejct);
161 role ProjectNamespace::SomeParameterizedRole { };
162  
163 sub log {
164 my($self, $message) = @_;
165 $logging_object->log($message);
166 }
167  
168 package ThingThatLogs;
169 my $logger = create_a_logging_object();
170 class ThingThatLogs {
171 roles => [ ProjectNamespace::SomeParameterizedRole->create(logging_object => $logger) ],
172 };
173  
174 =head1 DESCRIPTION
175  
176 Roles can be configured by declaring variables with the C<RoleParam> attribute.
177 These variables acquire values by calling C<create()> on the role's name and
178 giving values for all the role's parameters. More information about declaring
179 and using these parameters is described in the "Parameterized Roles" section of
180 L<UR::Role>.
181  
182 When the variables are initially declared, their value is initialized to a
183 reference to a UR::Role::Param. This represents a placeholder value to be
184 filled in later. The value may be used in a role definition or in any
185 subroutine.
186  
187 When the role is composed into a class, the placeholder values are replaced
188 with the actual values given in the C<create()> call on the role's name. The
189 original RoleParam variable is then tied to the UR::Role::Param class; it's
190 C<FETCH> method returns the proper value by searching the call stack for the
191 first method whose invocant class has composed the role where the FETCH
192 originated from. It returns the value given when the role was composed
193 into the class.
194  
195 These role param variables are read-only.
196  
197 Each variable with the RoleParam attribute becomes a required argument when
198 the role is instantiated .
199  
200 =head1 SEE ALSO
201  
202 L<UR::Role>, L<UR::Role::Prototype>, L<UR::Role::Instance>