bump version to 0.12
[gitmo/MooseX-ClassAttribute.git] / lib / MooseX / ClassAttribute / Trait / Mixin / HasClassAttributes.pm
CommitLineData
63fcc508 1package MooseX::ClassAttribute::Trait::Mixin::HasClassAttributes;
aa639029 2
3use strict;
4use warnings;
5
eeee5ccb 6our $VERSION = '0.12';
f77be127 7
aa639029 8use namespace::autoclean;
9use Moose::Role;
10
11has _class_attribute_map => (
12 traits => ['Hash'],
13 is => 'ro',
88b7f2c8 14 isa => 'HashRef[Class::MOP::Mixin::AttributeCore]',
aa639029 15 handles => {
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',
21 },
22 default => sub { {} },
23 init_arg => undef,
24);
25
deaffdd0 26# deprecated
aa639029 27sub get_class_attribute_map {
28 return $_[0]->_class_attribute_map();
29}
30
deaffdd0 31sub add_class_attribute {
32 my $self = shift;
33 my $attribute = shift;
34
35 ( $attribute->isa('Class::MOP::Mixin::AttributeCore') )
36 || confess
37 "Your attribute must be an instance of Class::MOP::Mixin::AttributeCore (or a subclass)";
38
39 $self->_attach_class_attribute($attribute);
40
41 my $attr_name = $attribute->name;
42
43 $self->remove_class_attribute($attr_name)
44 if $self->has_class_attribute($attr_name);
45
46 my $order = ( scalar keys %{ $self->_attribute_map } );
47 $attribute->_set_insertion_order($order);
48
49 $self->_add_class_attribute( $attr_name => $attribute );
50
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
55 # Class::MOP!
56 $self->_post_add_class_attribute($attribute)
57 if $self->can('_post_add_class_attribute');
58
59 return $attribute;
60}
61
ad109c62 62sub remove_class_attribute {
63 my $self = shift;
64 my $name = shift;
65
66 ( defined $name && $name )
67 || confess 'You must provide an attribute name';
68
69 my $removed_attr = $self->get_class_attribute($name);
70 return unless $removed_attr;
71
72 $self->_remove_class_attribute($name);
73
74 return $removed_attr;
75}
76
aa639029 771;
04b89789 78
79__END__
80
81=pod
82
83=head1 NAME
84
85MooseX::ClassAttribute::Trait::Mixin::HasClassAttributes - A mixin trait for things which have class attributes
86
87=head1 DESCRIPTION
88
89This trait is like L<Class::MOP::Mixin::HasAttributes>, except that it works
90with class attributes instead of object attributes.
91
92See L<MooseX::ClassAttribute::Trait::Class> and
93L<MooseX::ClassAttribute::Trait::Role> for API details.
94
95=head1 AUTHOR
96
97Dave Rolsky, C<< <autarch@urth.org> >>
98
99=head1 BUGS
100
101See L<MooseX::ClassAttribute> for details.
102
103=head1 COPYRIGHT & LICENSE
104
0cee5df4 105Copyright 2007-2010 Dave Rolsky, All Rights Reserved.
04b89789 106
107This program is free software; you can redistribute it and/or modify
108it under the same terms as Perl itself.
109
110=cut