Initial Commit

This commit is contained in:
Riley Schneider
2025-12-03 16:38:10 +01:00
parent c5e26bf594
commit b732d8d4b5
17680 changed files with 5977495 additions and 2 deletions

View File

@@ -0,0 +1,69 @@
package MooseX::ClassAttribute::Meta::Role::Attribute;
use strict;
use warnings;
our $VERSION = '0.29';
use namespace::autoclean;
use Moose;
use List::Util 1.45 'uniq';
extends 'Moose::Meta::Role::Attribute';
sub new {
my ( $class, $name, %options ) = @_;
$options{traits} = [
uniq( @{ $options{traits} || [] } ),
'MooseX::ClassAttribute::Trait::Attribute'
];
return $class->SUPER::new( $name, %options );
}
1;
# ABSTRACT: An attribute metaclass for class attributes in roles
__END__
=pod
=encoding UTF-8
=head1 NAME
MooseX::ClassAttribute::Meta::Role::Attribute - An attribute metaclass for class attributes in roles
=head1 VERSION
version 0.29
=head1 DESCRIPTION
This class overrides L<Moose::Meta::Role::Attribute> to support class
attribute declaration in roles.
=head1 BUGS
See L<MooseX::ClassAttribute> for details.
Bugs may be submitted through L<the RT bug tracker|http://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-ClassAttribute>
(or L<bug-moosex-classattribute@rt.cpan.org|mailto:bug-moosex-classattribute@rt.cpan.org>).
I am also usually active on IRC as 'drolsky' on C<irc://irc.perl.org>.
=head1 AUTHOR
Dave Rolsky <autarch@urth.org>
=head1 COPYRIGHT AND LICENCE
This software is Copyright (c) 2016 by Dave Rolsky.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
=cut

View File

@@ -0,0 +1,59 @@
package MooseX::ClassAttribute::Trait::Application;
use strict;
use warnings;
our $VERSION = '0.29';
use namespace::autoclean;
use Moose::Role;
after apply_attributes => sub {
shift->_apply_class_attributes(@_);
};
1;
# ABSTRACT: A trait that supports role application for roles with class attributes
__END__
=pod
=encoding UTF-8
=head1 NAME
MooseX::ClassAttribute::Trait::Application - A trait that supports role application for roles with class attributes
=head1 VERSION
version 0.29
=head1 DESCRIPTION
This trait is used to allow the application of roles containing class
attributes.
=head1 BUGS
See L<MooseX::ClassAttribute> for details.
Bugs may be submitted through L<the RT bug tracker|http://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-ClassAttribute>
(or L<bug-moosex-classattribute@rt.cpan.org|mailto:bug-moosex-classattribute@rt.cpan.org>).
I am also usually active on IRC as 'drolsky' on C<irc://irc.perl.org>.
=head1 AUTHOR
Dave Rolsky <autarch@urth.org>
=head1 COPYRIGHT AND LICENCE
This software is Copyright (c) 2016 by Dave Rolsky.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
=cut

View File

@@ -0,0 +1,94 @@
package MooseX::ClassAttribute::Trait::Application::ToClass;
use strict;
use warnings;
our $VERSION = '0.29';
use namespace::autoclean;
use Moose::Role;
with 'MooseX::ClassAttribute::Trait::Application';
around apply => sub {
my $orig = shift;
my $self = shift;
my $role = shift;
my $class = shift;
$class = Moose::Util::MetaRole::apply_metaroles(
for => $class,
class_metaroles => {
class => ['MooseX::ClassAttribute::Trait::Class'],
},
);
$self->$orig( $role, $class );
};
sub _apply_class_attributes {
my $self = shift;
my $role = shift;
my $class = shift;
my $attr_metaclass = $class->attribute_metaclass();
foreach my $attribute_name ( $role->get_class_attribute_list() ) {
if ( $class->has_class_attribute($attribute_name)
&& $class->get_class_attribute($attribute_name)
!= $role->get_class_attribute($attribute_name) ) {
next;
}
else {
$class->add_class_attribute(
$role->get_class_attribute($attribute_name)
->attribute_for_class($attr_metaclass) );
}
}
}
1;
# ABSTRACT: A trait that supports applying class attributes to classes
__END__
=pod
=encoding UTF-8
=head1 NAME
MooseX::ClassAttribute::Trait::Application::ToClass - A trait that supports applying class attributes to classes
=head1 VERSION
version 0.29
=head1 DESCRIPTION
This trait is used to allow the application of roles containing class
attributes to classes.
=head1 BUGS
See L<MooseX::ClassAttribute> for details.
Bugs may be submitted through L<the RT bug tracker|http://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-ClassAttribute>
(or L<bug-moosex-classattribute@rt.cpan.org|mailto:bug-moosex-classattribute@rt.cpan.org>).
I am also usually active on IRC as 'drolsky' on C<irc://irc.perl.org>.
=head1 AUTHOR
Dave Rolsky <autarch@urth.org>
=head1 COPYRIGHT AND LICENCE
This software is Copyright (c) 2016 by Dave Rolsky.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
=cut

View File

@@ -0,0 +1,104 @@
package MooseX::ClassAttribute::Trait::Application::ToRole;
use strict;
use warnings;
our $VERSION = '0.29';
use Moose::Util::MetaRole;
use MooseX::ClassAttribute::Trait::Application::ToClass;
use namespace::autoclean;
use Moose::Role;
with 'MooseX::ClassAttribute::Trait::Application';
around apply => sub {
my $orig = shift;
my $self = shift;
my $role1 = shift;
my $role2 = shift;
$role2 = Moose::Util::MetaRole::apply_metaroles(
for => $role2,
role_metaroles => {
role => ['MooseX::ClassAttribute::Trait::Role'],
application_to_class =>
['MooseX::ClassAttribute::Trait::Application::ToClass'],
application_to_role =>
['MooseX::ClassAttribute::Trait::Application::ToRole'],
},
);
$self->$orig( $role1, $role2 );
};
sub _apply_class_attributes {
my $self = shift;
my $role1 = shift;
my $role2 = shift;
foreach my $attribute_name ( $role1->get_class_attribute_list() ) {
if ( $role2->has_class_attribute($attribute_name)
&& $role2->get_class_attribute($attribute_name)
!= $role1->get_class_attribute($attribute_name) ) {
require Moose;
Moose->throw_error( "Role '"
. $role1->name()
. "' has encountered a class attribute conflict "
. "during composition. This is fatal error and cannot be disambiguated."
);
}
else {
$role2->add_class_attribute(
$role1->get_class_attribute($attribute_name)->clone() );
}
}
}
1;
# ABSTRACT: A trait that supports applying class attributes to roles
__END__
=pod
=encoding UTF-8
=head1 NAME
MooseX::ClassAttribute::Trait::Application::ToRole - A trait that supports applying class attributes to roles
=head1 VERSION
version 0.29
=head1 DESCRIPTION
This trait is used to allow the application of roles containing class
attributes to roles.
=head1 BUGS
See L<MooseX::ClassAttribute> for details.
Bugs may be submitted through L<the RT bug tracker|http://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-ClassAttribute>
(or L<bug-moosex-classattribute@rt.cpan.org|mailto:bug-moosex-classattribute@rt.cpan.org>).
I am also usually active on IRC as 'drolsky' on C<irc://irc.perl.org>.
=head1 AUTHOR
Dave Rolsky <autarch@urth.org>
=head1 COPYRIGHT AND LICENCE
This software is Copyright (c) 2016 by Dave Rolsky.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
=cut

View File

@@ -0,0 +1,268 @@
package MooseX::ClassAttribute::Trait::Attribute;
use strict;
use warnings;
our $VERSION = '0.29';
use namespace::autoclean;
use Moose::Role;
# This is the worst role evar! Really, this should be a subclass,
# because it overrides a lot of behavior. However, as a subclass it
# won't cooperate with _other_ subclasses.
around _process_options => sub {
my $orig = shift;
my $class = shift;
my $name = shift;
my $options = shift;
confess 'A class attribute cannot be required'
if $options->{required};
return $class->$orig( $name, $options );
};
after attach_to_class => sub {
my $self = shift;
my $meta = shift;
$self->_initialize($meta)
unless $self->is_lazy();
};
before detach_from_class => sub {
my $self = shift;
my $meta = shift;
$self->clear_value($meta);
};
sub _initialize {
my $self = shift;
my $metaclass = shift;
if ( $self->has_default() ) {
$self->set_value(
undef,
$self->default( $self->associated_class() )
);
}
elsif ( $self->has_builder() ) {
$self->set_value( undef, $self->_call_builder( $metaclass->name() ) );
}
}
around default => sub {
my $orig = shift;
my $self = shift;
my $default = $self->$orig();
if ( $self->is_default_a_coderef() && @_ ) {
return $default->(@_);
}
return $default;
};
around _call_builder => sub {
shift;
my $self = shift;
my $class = shift;
my $builder = $self->builder();
return $class->$builder()
if $class->can( $self->builder );
confess( "$class does not support builder method '"
. $self->builder
. "' for attribute '"
. $self->name
. "'" );
};
around set_value => sub {
shift;
my $self = shift;
shift; # ignoring instance or class name
my $value = shift;
$self->associated_class()
->set_class_attribute_value( $self->name() => $value );
};
around get_value => sub {
shift;
my $self = shift;
return $self->associated_class()
->get_class_attribute_value( $self->name() );
};
around has_value => sub {
shift;
my $self = shift;
return $self->associated_class()
->has_class_attribute_value( $self->name() );
};
around clear_value => sub {
shift;
my $self = shift;
return $self->associated_class()
->clear_class_attribute_value( $self->name() );
};
if ( $Moose::VERSION < 1.99 ) {
around inline_get => sub {
shift;
my $self = shift;
return $self->associated_class()
->_inline_get_class_slot_value( $self->slots() );
};
around inline_set => sub {
shift;
my $self = shift;
shift;
my $value = shift;
my $meta = $self->associated_class();
my $code
= $meta->_inline_set_class_slot_value( $self->slots(), $value )
. ";";
$code .= $meta->_inline_weaken_class_slot_value(
$self->slots(),
$value
)
. " if ref $value;"
if $self->is_weak_ref();
return $code;
};
around inline_has => sub {
shift;
my $self = shift;
return $self->associated_class()
->_inline_is_class_slot_initialized( $self->slots() );
};
around inline_clear => sub {
shift;
my $self = shift;
return $self->associated_class()
->_inline_deinitialize_class_slot( $self->slots() );
};
}
else {
around _inline_instance_get => sub {
shift;
my $self = shift;
return $self->associated_class()
->_inline_get_class_slot_value( $self->slots() );
};
around _inline_instance_set => sub {
shift;
my $self = shift;
shift;
my $value = shift;
return $self->associated_class()
->_inline_set_class_slot_value( $self->slots(), $value );
};
around _inline_instance_has => sub {
shift;
my $self = shift;
return $self->associated_class()
->_inline_is_class_slot_initialized( $self->slots() );
};
around _inline_instance_clear => sub {
shift;
my $self = shift;
return $self->associated_class()
->_inline_deinitialize_class_slot( $self->slots() );
};
around _inline_weaken_value => sub {
shift;
my $self = shift;
shift;
my $value = shift;
return unless $self->is_weak_ref();
return (
$self->associated_class->_inline_weaken_class_slot_value(
$self->slots(), $value
),
'if ref ' . $value . ';',
);
};
}
1;
# ABSTRACT: A trait for class attributes
__END__
=pod
=encoding UTF-8
=head1 NAME
MooseX::ClassAttribute::Trait::Attribute - A trait for class attributes
=head1 VERSION
version 0.29
=head1 DESCRIPTION
This role modifies the behavior of class attributes in various
ways. It really should be a subclass of C<Moose::Meta::Attribute>, but
if it were then it couldn't be combined with other attribute
metaclasses, like C<MooseX::AttributeHelpers>.
There are no new public methods implemented by this role. All it does
is change the behavior of a number of existing methods.
=head1 BUGS
See L<MooseX::ClassAttribute> for details.
Bugs may be submitted through L<the RT bug tracker|http://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-ClassAttribute>
(or L<bug-moosex-classattribute@rt.cpan.org|mailto:bug-moosex-classattribute@rt.cpan.org>).
I am also usually active on IRC as 'drolsky' on C<irc://irc.perl.org>.
=head1 AUTHOR
Dave Rolsky <autarch@urth.org>
=head1 COPYRIGHT AND LICENCE
This software is Copyright (c) 2016 by Dave Rolsky.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
=cut

View File

@@ -0,0 +1,329 @@
package MooseX::ClassAttribute::Trait::Class;
use strict;
use warnings;
our $VERSION = '0.29';
use MooseX::ClassAttribute::Trait::Attribute;
use Scalar::Util qw( blessed );
use namespace::autoclean;
use Moose::Role;
with 'MooseX::ClassAttribute::Trait::Mixin::HasClassAttributes';
has _class_attribute_values => (
traits => ['Hash'],
is => 'ro',
isa => 'HashRef',
handles => {
'get_class_attribute_value' => 'get',
'set_class_attribute_value' => 'set',
'has_class_attribute_value' => 'exists',
'clear_class_attribute_value' => 'delete',
},
lazy => 1,
default => sub { $_[0]->_class_attribute_values_hashref() },
init_arg => undef,
);
around add_class_attribute => sub {
my $orig = shift;
my $self = shift;
my $attr = (
blessed $_[0] && $_[0]->isa('Class::MOP::Attribute')
? $_[0]
: $self->_process_class_attribute(@_)
);
$self->$orig($attr);
return $attr;
};
sub _post_add_class_attribute {
my $self = shift;
my $attr = shift;
my $name = $attr->name();
my $e = do {
local $@;
eval { $attr->install_accessors() };
$@;
};
if ($e) {
$self->remove_attribute($name);
die $e;
}
}
sub _attach_class_attribute {
my ( $self, $attribute ) = @_;
$attribute->attach_to_class($self);
}
# It'd be nice if I didn't have to replicate this for class
# attributes, since it's basically just a copy of
# Moose::Meta::Class->_process_attribute
sub _process_class_attribute {
my $self = shift;
my $name = shift;
my @args = @_;
@args = %{ $args[0] } if scalar @args == 1 && ref( $args[0] ) eq 'HASH';
if ( $name =~ /^\+(.*)/ ) {
return $self->_process_inherited_class_attribute( $1, @args );
}
else {
return $self->_process_new_class_attribute( $name, @args );
}
}
sub _process_new_class_attribute {
my $self = shift;
my $name = shift;
my %p = @_;
if ( $p{traits} ) {
push @{ $p{traits} }, 'MooseX::ClassAttribute::Trait::Attribute';
}
else {
$p{traits} = ['MooseX::ClassAttribute::Trait::Attribute'];
}
return Moose::Meta::Attribute->interpolate_class_and_new( $name, %p );
}
sub _process_inherited_class_attribute {
my $self = shift;
my $name = shift;
my %p = @_;
my $inherited_attr = $self->find_class_attribute_by_name($name);
( defined $inherited_attr )
|| confess
"Could not find an attribute by the name of '$name' to inherit from";
return $inherited_attr->clone_and_inherit_options(%p);
}
around remove_class_attribute => sub {
my $orig = shift;
my $self = shift;
my $removed_attr = $self->$orig(@_)
or return;
$removed_attr->remove_accessors();
$removed_attr->detach_from_class();
return $removed_attr;
};
sub get_all_class_attributes {
my $self = shift;
my %attrs = map {
my $meta = Class::MOP::class_of($_);
$meta && $meta->can('_class_attribute_map')
? %{ $meta->_class_attribute_map() }
: ()
}
reverse $self->linearized_isa;
return values %attrs;
}
sub compute_all_applicable_class_attributes {
warn
'The compute_all_applicable_class_attributes method has been deprecated.'
. " Use get_all_class_attributes instead.\n";
shift->compute_all_applicable_class_attributes(@_);
}
sub find_class_attribute_by_name {
my $self = shift;
my $name = shift;
foreach my $class ( $self->linearized_isa() ) {
my $meta = Class::MOP::class_of($class)
or next;
return $meta->get_class_attribute($name)
if $meta->can('has_class_attribute')
&& $meta->has_class_attribute($name);
}
return;
}
sub _class_attribute_values_hashref {
my $self = shift;
no strict 'refs';
return \%{ $self->_class_attribute_var_name() };
}
sub _class_attribute_var_name {
my $self = shift;
return $self->name() . q'::__ClassAttributeValues';
}
sub _inline_class_slot_access {
my $self = shift;
my $name = shift;
return
'$'
. $self->_class_attribute_var_name . '{"'
. quotemeta($name) . '"}';
}
sub _inline_get_class_slot_value {
my $self = shift;
my $name = shift;
return $self->_inline_class_slot_access($name);
}
sub _inline_set_class_slot_value {
my $self = shift;
my $name = shift;
my $val_name = shift;
return $self->_inline_class_slot_access($name) . ' = ' . $val_name;
}
sub _inline_is_class_slot_initialized {
my $self = shift;
my $name = shift;
return 'exists ' . $self->_inline_class_slot_access($name);
}
sub _inline_deinitialize_class_slot {
my $self = shift;
my $name = shift;
return 'delete ' . $self->_inline_class_slot_access($name);
}
sub _inline_weaken_class_slot_value {
my $self = shift;
my $name = shift;
return
'Scalar::Util::weaken( '
. $self->_inline_class_slot_access($name) . ')';
}
1;
# ABSTRACT: A trait for classes with class attributes
__END__
=pod
=encoding UTF-8
=head1 NAME
MooseX::ClassAttribute::Trait::Class - A trait for classes with class attributes
=head1 VERSION
version 0.29
=head1 SYNOPSIS
for my $attr ( HasClassAttributes->meta()->get_all_class_attributes() )
{
print $attr->name();
}
=head1 DESCRIPTION
This role adds awareness of class attributes to a metaclass object. It
provides a set of introspection methods that largely parallel the
existing attribute methods, except they operate on class attributes.
=head1 METHODS
Every method provided by this role has an analogous method in
C<Class::MOP::Class> or C<Moose::Meta::Class> for regular attributes.
=head2 $meta->has_class_attribute($name)
=head2 $meta->get_class_attribute($name)
=head2 $meta->get_class_attribute_list()
These methods operate on the current metaclass only.
=head2 $meta->add_class_attribute(...)
This accepts the same options as the L<Moose::Meta::Attribute>
C<add_attribute()> method. However, if an attribute is specified as
"required" an error will be thrown.
=head2 $meta->remove_class_attribute($name)
If the named class attribute exists, it is removed from the class,
along with its accessor methods.
=head2 $meta->get_all_class_attributes()
This method returns a list of attribute objects for the class and all
its parent classes.
=head2 $meta->find_class_attribute_by_name($name)
This method looks at the class and all its parent classes for the
named class attribute.
=head2 $meta->get_class_attribute_value($name)
=head2 $meta->set_class_attribute_value($name, $value)
=head2 $meta->set_class_attribute_value($name)
=head2 $meta->clear_class_attribute_value($name)
These methods operate on the storage for class attribute values, which
is attached to the metaclass object.
There's really no good reason for you to call these methods unless
you're doing some deep hacking. They are named as public methods
solely because they are used by other meta roles and classes in this
distribution.
=head1 BUGS
See L<MooseX::ClassAttribute> for details.
Bugs may be submitted through L<the RT bug tracker|http://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-ClassAttribute>
(or L<bug-moosex-classattribute@rt.cpan.org|mailto:bug-moosex-classattribute@rt.cpan.org>).
I am also usually active on IRC as 'drolsky' on C<irc://irc.perl.org>.
=head1 AUTHOR
Dave Rolsky <autarch@urth.org>
=head1 COPYRIGHT AND LICENCE
This software is Copyright (c) 2016 by Dave Rolsky.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
=cut

View File

@@ -0,0 +1,124 @@
package MooseX::ClassAttribute::Trait::Mixin::HasClassAttributes;
use strict;
use warnings;
our $VERSION = '0.29';
use namespace::autoclean;
use Moose::Role;
has _class_attribute_map => (
traits => ['Hash'],
is => 'ro',
isa => 'HashRef[Class::MOP::Mixin::AttributeCore]',
handles => {
'_add_class_attribute' => 'set',
'has_class_attribute' => 'exists',
'get_class_attribute' => 'get',
'_remove_class_attribute' => 'delete',
'get_class_attribute_list' => 'keys',
},
default => sub { {} },
init_arg => undef,
);
# deprecated
sub get_class_attribute_map {
return $_[0]->_class_attribute_map();
}
sub add_class_attribute {
my $self = shift;
my $attribute = shift;
( $attribute->isa('Class::MOP::Mixin::AttributeCore') )
|| confess
"Your attribute must be an instance of Class::MOP::Mixin::AttributeCore (or a subclass)";
$self->_attach_class_attribute($attribute);
my $attr_name = $attribute->name;
$self->remove_class_attribute($attr_name)
if $self->has_class_attribute($attr_name);
my $order = ( scalar keys %{ $self->_attribute_map } );
$attribute->_set_insertion_order($order);
$self->_add_class_attribute( $attr_name => $attribute );
# This method is called to allow for installing accessors. Ideally, we'd
# use method overriding, but then the subclass would be responsible for
# making the attribute, which would end up with lots of code
# duplication. Even more ideally, we'd use augment/inner, but this is
# Class::MOP!
$self->_post_add_class_attribute($attribute)
if $self->can('_post_add_class_attribute');
return $attribute;
}
sub remove_class_attribute {
my $self = shift;
my $name = shift;
( defined $name && $name )
|| confess 'You must provide an attribute name';
my $removed_attr = $self->get_class_attribute($name);
return unless $removed_attr;
$self->_remove_class_attribute($name);
return $removed_attr;
}
1;
# ABSTRACT: A mixin trait for things which have class attributes
__END__
=pod
=encoding UTF-8
=head1 NAME
MooseX::ClassAttribute::Trait::Mixin::HasClassAttributes - A mixin trait for things which have class attributes
=head1 VERSION
version 0.29
=head1 DESCRIPTION
This trait is like L<Class::MOP::Mixin::HasAttributes>, except that it works
with class attributes instead of object attributes.
See L<MooseX::ClassAttribute::Trait::Class> and
L<MooseX::ClassAttribute::Trait::Role> for API details.
=head1 BUGS
See L<MooseX::ClassAttribute> for details.
Bugs may be submitted through L<the RT bug tracker|http://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-ClassAttribute>
(or L<bug-moosex-classattribute@rt.cpan.org|mailto:bug-moosex-classattribute@rt.cpan.org>).
I am also usually active on IRC as 'drolsky' on C<irc://irc.perl.org>.
=head1 AUTHOR
Dave Rolsky <autarch@urth.org>
=head1 COPYRIGHT AND LICENCE
This software is Copyright (c) 2016 by Dave Rolsky.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
=cut

View File

@@ -0,0 +1,116 @@
package MooseX::ClassAttribute::Trait::Role;
use strict;
use warnings;
our $VERSION = '0.29';
use MooseX::ClassAttribute::Meta::Role::Attribute;
use Scalar::Util qw( blessed );
use namespace::autoclean;
use Moose::Role;
with 'MooseX::ClassAttribute::Trait::Mixin::HasClassAttributes';
around add_class_attribute => sub {
my $orig = shift;
my $self = shift;
my $attr = (
blessed $_[0] && $_[0]->isa('Class::MOP::Mixin::AttributeCore')
? $_[0]
: MooseX::ClassAttribute::Meta::Role::Attribute->new(@_)
);
$self->$orig($attr);
return $attr;
};
sub _attach_class_attribute {
my ( $self, $attribute ) = @_;
$attribute->attach_to_role($self);
}
sub composition_class_roles {
return 'MooseX::ClassAttribute::Trait::Role::Composite';
}
1;
# ABSTRACT: A trait for roles with class attributes
__END__
=pod
=encoding UTF-8
=head1 NAME
MooseX::ClassAttribute::Trait::Role - A trait for roles with class attributes
=head1 VERSION
version 0.29
=head1 SYNOPSIS
for my $attr ( HasClassAttributes->meta()->get_all_class_attributes() )
{
print $attr->name();
}
=head1 DESCRIPTION
This role adds awareness of class attributes to a role metaclass object. It
provides a set of introspection methods that largely parallel the existing
attribute methods, except they operate on class attributes.
=head1 METHODS
Every method provided by this role has an analogous method in
C<Class::MOP::Class> or C<Moose::Meta::Class> for regular attributes.
=head2 $meta->has_class_attribute($name)
=head2 $meta->get_class_attribute($name)
=head2 $meta->get_class_attribute_list()
These methods are exactly like their counterparts in
L<MooseX::ClassAttribute::Trait::Class>.
=head2 $meta->add_class_attribute(...)
This accepts the same options as the L<Moose::Meta::Attribute>
C<add_attribute()> method. However, if an attribute is specified as
"required" an error will be thrown.
=head2 $meta->remove_class_attribute($name)
If the named class attribute exists, it is removed from the role.
=head1 BUGS
See L<MooseX::ClassAttribute> for details.
Bugs may be submitted through L<the RT bug tracker|http://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-ClassAttribute>
(or L<bug-moosex-classattribute@rt.cpan.org|mailto:bug-moosex-classattribute@rt.cpan.org>).
I am also usually active on IRC as 'drolsky' on C<irc://irc.perl.org>.
=head1 AUTHOR
Dave Rolsky <autarch@urth.org>
=head1 COPYRIGHT AND LICENCE
This software is Copyright (c) 2016 by Dave Rolsky.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
=cut

View File

@@ -0,0 +1,119 @@
package MooseX::ClassAttribute::Trait::Role::Composite;
use strict;
use warnings;
our $VERSION = '0.29';
use Moose::Util::MetaRole;
use Moose::Util qw(does_role);
use namespace::autoclean;
use Moose::Role;
with 'MooseX::ClassAttribute::Trait::Role';
sub _merge_class_attributes {
my $self = shift;
my @all_attributes;
foreach my $role ( @{ $self->get_roles() } ) {
if ( does_role( $role, 'MooseX::ClassAttribute::Trait::Role' ) ) {
push @all_attributes,
map { $role->get_class_attribute($_) }
$role->get_class_attribute_list();
}
}
my %seen;
foreach my $attribute (@all_attributes) {
my $name = $attribute->name();
if ( exists $seen{$name} ) {
next if $seen{$name} == $attribute;
require Moose;
Moose->throw_error( "Role '"
. $self->name()
. "' has encountered a class attribute conflict "
. "during composition. This is a fatal error and "
. "cannot be disambiguated." );
}
$seen{$name} = $attribute;
}
foreach my $attribute (@all_attributes) {
$self->add_class_attribute( $attribute->clone() );
}
return keys %seen;
}
around apply_params => sub {
my $orig = shift;
my $self = shift;
$self->$orig(@_);
$self = Moose::Util::MetaRole::apply_metaroles(
for => $self,
role_metaroles => {
application_to_class =>
['MooseX::ClassAttribute::Trait::Application::ToClass'],
application_to_role =>
['MooseX::ClassAttribute::Trait::Application::ToRole'],
},
);
$self->_merge_class_attributes();
return $self;
};
1;
# ABSTRACT: A trait that supports applying multiple roles at once
__END__
=pod
=encoding UTF-8
=head1 NAME
MooseX::ClassAttribute::Trait::Role::Composite - A trait that supports applying multiple roles at once
=head1 VERSION
version 0.29
=head1 DESCRIPTION
This trait is used to allow the application of multiple roles (one
or more of which contain class attributes) to a class or role.
=head1 BUGS
See L<MooseX::ClassAttribute> for details.
Bugs may be submitted through L<the RT bug tracker|http://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-ClassAttribute>
(or L<bug-moosex-classattribute@rt.cpan.org|mailto:bug-moosex-classattribute@rt.cpan.org>).
I am also usually active on IRC as 'drolsky' on C<irc://irc.perl.org>.
=head1 AUTHOR
Dave Rolsky <autarch@urth.org>
=head1 COPYRIGHT AND LICENCE
This software is Copyright (c) 2016 by Dave Rolsky.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
=cut