Commit | Line | Data |
63fcc508 |
1 | package MooseX::ClassAttribute::Trait::Role; |
88b7f2c8 |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use MooseX::ClassAttribute::Meta::Role::Attribute; |
7 | use Scalar::Util qw( blessed ); |
8 | |
9 | use namespace::autoclean; |
10 | use Moose::Role; |
11 | |
63fcc508 |
12 | with 'MooseX::ClassAttribute::Trait::Mixin::HasClassAttributes'; |
88b7f2c8 |
13 | |
14 | around add_class_attribute => sub { |
15 | my $orig = shift; |
16 | my $self = shift; |
17 | my $attr = ( |
18 | blessed $_[0] && $_[0]->isa('Class::MOP::Mixin::AttributeCore') |
19 | ? $_[0] |
20 | : MooseX::ClassAttribute::Meta::Role::Attribute->new(@_) |
21 | ); |
22 | |
23 | $self->$orig($attr); |
24 | |
25 | return $attr; |
26 | }; |
27 | |
28 | sub _attach_class_attribute { |
29 | my ( $self, $attribute ) = @_; |
30 | |
31 | $attribute->attach_to_role($self); |
32 | } |
33 | |
34 | 1; |
35 | |
0d0bf8c3 |
36 | # ABSTRACT: A trait for roles with class attributes |
37 | |
88b7f2c8 |
38 | __END__ |
39 | |
40 | =pod |
41 | |
88b7f2c8 |
42 | =head1 SYNOPSIS |
43 | |
44 | for my $attr ( HasClassAttributes->meta()->get_all_class_attributes() ) |
45 | { |
46 | print $attr->name(); |
47 | } |
48 | |
49 | =head1 DESCRIPTION |
50 | |
04b89789 |
51 | This role adds awareness of class attributes to a role metaclass object. It |
52 | provides a set of introspection methods that largely parallel the existing |
53 | attribute methods, except they operate on class attributes. |
88b7f2c8 |
54 | |
55 | =head1 METHODS |
56 | |
57 | Every method provided by this role has an analogous method in |
58 | C<Class::MOP::Class> or C<Moose::Meta::Class> for regular attributes. |
59 | |
60 | =head2 $meta->has_class_attribute($name) |
61 | |
62 | =head2 $meta->get_class_attribute($name) |
63 | |
64 | =head2 $meta->get_class_attribute_list() |
65 | |
04b89789 |
66 | These methods are exactly like their counterparts in |
67 | L<MooseX::ClassAttribute::Trait::Class>. |
88b7f2c8 |
68 | |
69 | =head2 $meta->add_class_attribute(...) |
70 | |
71 | This accepts the same options as the L<Moose::Meta::Attribute> |
72 | C<add_attribute()> method. However, if an attribute is specified as |
73 | "required" an error will be thrown. |
74 | |
75 | =head2 $meta->remove_class_attribute($name) |
76 | |
04b89789 |
77 | If the named class attribute exists, it is removed from the role. |
88b7f2c8 |
78 | |
88b7f2c8 |
79 | =head1 BUGS |
80 | |
81 | See L<MooseX::ClassAttribute> for details. |
82 | |
88b7f2c8 |
83 | =cut |