1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
package UR::Role;
use strict;
use warnings;
use UR;
use UR::Object::Type::InternalAPI;
use UR::Util;
use UR::AttributeHandlers;
use UR::Role::Param;
use UR::Role::Prototype;
use UR::Role::PrototypeWithParams;
use UR::Role::Instance;
use Scalar::Util qw(blessed);
use List::MoreUtils qw(any);
use Sub::Install;
use Sub::Name;
use Carp;
our @CARP_NOT = qw(UR::Object::Type);
use Exporter qw(import);
our @EXPORT_OK = qw(before after around);
our $VERSION = "0.47"; # UR $VERSION;
Class::Autouse->sugar(\&_define_role);
sub define {
my $class = shift;
UR::Role::Prototype->define(@_);
}
sub _define_role {
my($role_name, $func, @params) = @_;
if (defined($func) and $func eq "role" and @params > 1 and $role_name ne "UR::Role") {
my @role_params;
if (@params == 2 and ref($params[1]) eq 'HASH') {
@role_params = %{ $params[1] };
}
elsif (@params == 2 and ref($params[1]) eq 'ARRAY') {
@role_params = @{ $params[1] };
}
else {
@role_params = @params[1..$#params];
}
my $role = UR::Role::Prototype->define(role_name => $role_name, @role_params);
unless ($role) {
Carp::croak "error defining role $role_name!";
}
return sub { $role_name };
} else {
return;
}
}
foreach my $type ( qw(before after around) ) {
my $modifier_class = join('::', 'UR::Role::MethodModifier', ucfirst($type));
my $sub = Sub::Name::subname $type => sub {
my $subname = shift;
my $code = shift;
my $package = caller;
my $b = $modifier_class->create(
role_name => $package,
name => $subname,
code => $code,
);
$code;
};
Sub::Install::install_sub({
into => __PACKAGE__,
as => $type,
code => $sub,
});
}
1;
__END__
=pod
=head1 NAME
UR::Role - Roles in UR, an alternative to inheritance
=head1 SYNOPSIS
package My::Role;
role My::Role {
id_by => [
role_id_property => { is => 'String' },
],
has => [
role_property => { is => 'String' },
another_prop => { is => 'Integer' },
},
requires => ['class_method'],
excludes => ['Bad::Role'],
};
sub role_method { ... }
package My::Class;
class My::Class {
has => [
class_property => { is => 'Integer ' },
],
roles => ['My::Role'],
};
sub class_method { ... }
my $obj = My::Class->new();
$obj->does('My::Role'); # true
=head1 DESCRIPTION
Roles are used to encapsulate a piece of behavior to be used in other classes.
They have properties and methods that get melded into any class that composes
them. A Role can require any composing class to implement a list of methods
or properties.
Roles are not classes. They can not be instantiated or inherited from. They
are composed into a class by listing their names in the C<roles> attribute of
a class definition.
=head2 Defining a Role
Roles are defined with the C<role> keyword. Their definition looks very
similar to a class definition as described in L<UR::Object::Type::Initializer>.
In particular, Roles have a C<has> section to define properties, and accept
many class-meta attributes such as 'id_generator', 'valid_signals', and 'doc'.
Roles may implement operator overloading via the 'use overload' mechanism.
Roles also have unique attributes to declare restrictions on their use.
=over 4
=item requires
A listref of property and method names that must appear in any class composing
the Role. Properties and methods defined in other roles or parent classes
can satisfy a requirement.
=item excludes
A listref of Role names that may not be composed together with this Role.
This is useful to declare incompatibilities between roles.
=back
=head2 Composing a Role
Compose one or more Roles into a class using the 'roles' attribute in a class
definition.
class My::Class {
roles => ['My::Role', 'Other::Role'],
is => ['Parent::Class'],
has => ['prop_a','prop_b'],
};
Properties and meta-attributes from the Roles get copied into the composing
class. Subroutines defined in the Roles' namespaces are imported into the
class's namespace. Operator overloads defined in the Roles are applied to
the class.
=head3 Property and meta-attribute conflicts
An exception is thrown if multiple Roles are composed together that define
the same property, even if the composing class defines the same property in
an attempt to override them.
A class may declare a property with the same name that a role also declares.
The definition in the class overrides whatever appears in the role. An
exception is thrown if a role declares an ID property in the 'id_by' section
and the consuming class redeclares it in the 'has' section as a normal
property.
=head3 Method conflicts
An exception is thrown if multiple Roles are composed together that
define the same subroutine, or if the composing class (or any of its parent
classes) defines the same subroutine as any of the roles.
If the class wants to override a subroutine defined in one of its roles,
the override must be declared with the "Overrides" attribute.
sub overridden_method : Overrides(My::Role, Other::Role) { ... }
All the conflicting role names must be listed in the override, separated by
commas. The class will probably implement whatever behavior is required,
maybe by calling one role's method or the other, both methods, neither,
or anything else.
To call a function in a role, the function's fully qualified name, including
the role's package, must be used.
=head3 Overload conflicts
Like with method conflicts, an exception is thrown if multiple Roles are
composed together that overload the same operator unless the composing
class also overloads that same operator.
An exception is also thrown if composed roles define incompatible 'fallback'
behavior. If a role does not specify 'fallback', or explicitly sets it to
C<undef>, it is compatible with other values. A Role that sets its 'fallback'
value to true or false is only compatible with other roles' values of undef
or the same true or false value.
=head2 __import__
Each time a Role is composed into a class, its C<__import__()> method is
called. C<__import__()> is passed two arguments:
=over 4
=item *
The name of the role
=item *
The class metadata object composing the role.
=back
This happens after the class is completely constructed.
=head2 Parameterized Roles
Scalar variables with the C<RoleParam> attribute are designated as role
params. Values can be supplied when a role composes the role as a means to
provide more flexibility and genericity for a role.
package ObjectDisplayer;
use ProjectNamespace;
our $target_type : RoleParam(target_type);
role ObjectDisplayer {
has => [
target_object => { is => $target_type },
],
};
package ShowCars;
class ShowCars {
roles => [ ObjectDisplayer->create(target_type => 'Car' ],
};
When the role is composed, the call to C<create()> in the class definition
creates a L<UR::Role::Instance> to represent the ObjectDisplayer role being
composed into the ShowCars class with the params C<{ target_type => 'car' }>.
Values for the role param values in the role definition are swapped out with
the provided values as the role's properties are composed into the class.
At run-time, these role param variables are tied with the L<UR::Role::Param>
class. Its C<FETCH> method searches the call stack for the first function
whose invocant composes the role where the variable's value is being fetched
from. The proper param value is returned.
An exception is thrown if a class composes a role and either provides unknown
role params or omits values for existing params.
=head2 Method Modifiers
Roles can hook into methods defined in consuming classes by using the "before",
"after" and "around" method modifiers.
use UR;
package RoleWithModifiers;
use UR::Role qw(before after around);
role RoleWithModifiers { };
before 'do_something' => sub {
my($self, @params) = @_;
print "Calling do_something with params ",join(',',@params),"\n";
};
after 'do_something' => sub {
my($rv, $self, @params) = @_;
print "Result from do_something: $rv\n";
};
around 'do_something' => sub {
my($orig, $self, @params) = @_;
print "Wrapped call to do_something params ",join(',',@params),"\n";
my $rv = $self->$orig(@params);
print "The wrapped call to do_something returned $rv\n";
return 123;
};
package ClassUsingRole;
class ClassUsingRole { roles => 'RoleWithModifiers' };
sub do_something {
print "In original do_something\n";
return 'abc';
}
my $rv = ClassUsingRole->create()->do_something();
print "The call to do_something returned $rv\n";
Running this code will generate the following output:
Wrapped call to do_something params
Calling do_something with params
In original do_something
Result from do_something: abc
The wrapped call to do_something returned abc
The call to do_something returned 123
Method modifiers are applied in the order they appear in the role's
implementation.
=over 4
=item before(@params)
A C<before> modifier runs before the named method. It receives all the
arguments and C<wantarray> context as the original method call. It cannot
affect the parameters to the original method call, and its return value is
ignored.
=item after($rv, @params)
The first argument to an C<after> modifier is the return value of the original
method call, the remaining arguments and C<wantarray> context are the same as
the original method call. If the original method was called in list context,
then C<$rv> will be an arrayref containing the list of return values. This
modifier's return value is ignored.
=item around($orig, @params)
An C<around> modifier is run in place of the original method, and receives
a coderef of the original method as its first argument. Around modifiers
can munge arguments and return values, and control when and whether the
original method is called.
=back
=head1 SEE ALSO
L<UR>, L<UR::Object::Type::Initializer>, L<UR::Role::Instance>, L<UR::Role::Param>
=cut
|