1 package MooseX::ClassAttribute::Trait::Mixin::HasClassAttributes;
8 use namespace::autoclean;
11 has _class_attribute_map => (
14 isa => 'HashRef[Class::MOP::Mixin::AttributeCore]',
16 '_add_class_attribute' => 'set',
17 'has_class_attribute' => 'exists',
18 'get_class_attribute' => 'get',
19 '_remove_class_attribute' => 'delete',
20 'get_class_attribute_list' => 'keys',
22 default => sub { {} },
27 sub get_class_attribute_map {
28 return $_[0]->_class_attribute_map();
31 sub add_class_attribute {
33 my $attribute = shift;
35 ( $attribute->isa('Class::MOP::Mixin::AttributeCore') )
37 "Your attribute must be an instance of Class::MOP::Mixin::AttributeCore (or a subclass)";
39 $self->_attach_class_attribute($attribute);
41 my $attr_name = $attribute->name;
43 $self->remove_class_attribute($attr_name)
44 if $self->has_class_attribute($attr_name);
46 my $order = ( scalar keys %{ $self->_attribute_map } );
47 $attribute->_set_insertion_order($order);
49 $self->_add_class_attribute( $attr_name => $attribute );
51 # This method is called to allow for installing accessors. Ideally, we'd
52 # use method overriding, but then the subclass would be responsible for
53 # making the attribute, which would end up with lots of code
54 # duplication. Even more ideally, we'd use augment/inner, but this is
56 $self->_post_add_class_attribute($attribute)
57 if $self->can('_post_add_class_attribute');
62 sub remove_class_attribute {
66 ( defined $name && $name )
67 || confess 'You must provide an attribute name';
69 my $removed_attr = $self->get_class_attribute($name);
70 return unless $removed_attr;
72 $self->_remove_class_attribute($name);
85 MooseX::ClassAttribute::Trait::Mixin::HasClassAttributes - A mixin trait for things which have class attributes
89 This trait is like L<Class::MOP::Mixin::HasAttributes>, except that it works
90 with class attributes instead of object attributes.
92 See L<MooseX::ClassAttribute::Trait::Class> and
93 L<MooseX::ClassAttribute::Trait::Role> for API details.
97 Dave Rolsky, C<< <autarch@urth.org> >>
101 See L<MooseX::ClassAttribute> for details.
103 =head1 COPYRIGHT & LICENSE
105 Copyright 2007-2010 Dave Rolsky, All Rights Reserved.
107 This program is free software; you can redistribute it and/or modify
108 it under the same terms as Perl itself.