1 package MooseX::ClassAttribute;
6 # This module doesn't really need these pragmas - this is just for the benefit
8 use namespace::clean 0.20 ();
9 use namespace::autoclean 0.11 ();
14 use MooseX::ClassAttribute::Trait::Class;
15 use MooseX::ClassAttribute::Trait::Role;
16 use MooseX::ClassAttribute::Trait::Application::ToClass;
17 use MooseX::ClassAttribute::Trait::Application::ToRole;
19 Moose::Exporter->setup_import_methods(
20 with_meta => ['class_has'],
22 class => ['MooseX::ClassAttribute::Trait::Class'],
25 role => ['MooseX::ClassAttribute::Trait::Role'],
26 application_to_class =>
27 ['MooseX::ClassAttribute::Trait::Application::ToClass'],
28 application_to_role =>
29 ['MooseX::ClassAttribute::Trait::Application::ToRole'],
37 my $attrs = ref $name eq 'ARRAY' ? $name : [$name];
39 my %options = ( definition_context => _caller_info(), @_ );
41 $meta->add_class_attribute( $_, %options ) for @{$attrs};
44 # Copied from Moose::Util in 2.06
46 my $level = @_ ? ( $_[0] + 1 ) : 2;
48 @info{qw(package file line)} = caller($level);
54 # ABSTRACT: Declare class attributes Moose-style
65 use MooseX::ClassAttribute;
70 default => sub { {} },
73 __PACKAGE__->meta()->make_immutable();
76 no MooseX::ClassAttribute;
80 My::Class->Cache()->{thing} = ...;
84 This module allows you to declare class attributes in exactly the same
85 way as object attributes, using C<class_has()> instead of C<has()>.
87 You can use any feature of Moose's attribute declarations, including
88 overriding a parent's attributes, delegation (C<handles>), attribute traits,
89 etc. All features should just work. The one exception is the "required" flag,
90 which is not allowed for class attributes.
92 The accessor methods for class attribute may be called on the class
93 directly, or on objects of that class. Passing a class attribute to
94 the constructor will not set that attribute.
98 This class exports one function when you use it, C<class_has()>. This
99 works exactly like Moose's C<has()>, but it declares class attributes.
101 One little nit is that if you include C<no Moose> in your class, you won't
102 remove the C<class_has()> function. To do that you must include C<no
103 MooseX::ClassAttribute> as well. Or you can just use L<namespace::autoclean>
106 =head2 Implementation and Immutability
108 This module will add a role to your class's metaclass, See
109 L<MooseX::ClassAttribute::Trait::Class> for details. This role
110 provides introspection methods for class attributes.
112 Class attributes themselves do the
113 L<MooseX::ClassAttribute::Trait::Attribute> role.
115 =head2 Cooperation with Metaclasses and Traits
117 This module should work with most attribute metaclasses and traits,
118 but it's possible that conflicts could occur. This module has been
119 tested to work with Moose's native traits.
121 =head2 Class Attributes in Roles
123 You can add a class attribute to a role. When that role is applied to a class,
124 the class will have the relevant class attributes added. Note that attribute
125 defaults will be calculated when the class attribute is composed into the
130 If you'd like to thank me for the work I've done on this module,
131 please consider making a "donation" to me via PayPal. I spend a lot of
132 free time creating free software, and would appreciate any support
135 Please note that B<I am not suggesting that you must do this> in order
136 for me to continue working on this particular software. I will
137 continue to do so, inasmuch as I have in the past, for as long as it
140 Similarly, a donation made in this way will probably not make me work
141 on this software much more, unless I get so many donations that I can
142 consider working on free software full time, which seems unlikely at
145 To donate, log into PayPal and send money to autarch@urth.org or use
146 the button on this page:
147 L<http://www.urth.org/~autarch/fs-donation.html>
151 Please report any bugs or feature requests to
152 C<bug-moosex-classattribute@rt.cpan.org>, or through the web interface
153 at L<http://rt.cpan.org>. I will be notified, and then you'll
154 automatically be notified of progress on your bug as I make changes.