1 package MooseX::ClassAttribute;
7 our $AUTHORITY = 'cpan:DROLSKY';
11 use MooseX::ClassAttribute::Role::Meta::Class;
13 Moose::Exporter->setup_import_methods
14 ( with_caller => [ 'class_has' ] );
25 Moose::Util::MetaRole::apply_metaclass_roles
26 ( for_class => $p{for_class},
27 metaclass_roles => [ 'MooseX::ClassAttribute::Role::Meta::Class' ],
37 my $attrs = ref $name eq 'ARRAY' ? $name : [$name];
41 ->add_class_attribute( $_, %options )
53 MooseX::ClassAttribute - Declare class attributes Moose-style
60 use MooseX::ClassAttribute;
65 default => sub { {} },
68 __PACKAGE__->meta()->make_immutable();
69 MooseX::ClassAttribute::container_class()->meta()->make_immutable();
72 no MooseX::ClassAttribute;
76 My::Class->Cache()->{thing} = ...;
81 This module allows you to declare class attributes in exactly the same
82 way as you declare object attributes, except using C<class_has()>
83 instead of C<has()>. It is also possible to make these attributes
84 immutable (and faster) just as you can with normal Moose attributes.
86 You can use any feature of Moose's attribute declarations, including
87 overriding a parent's attributes, delegation (C<handles>), and
88 attribute metaclasses, and it should just work.
90 The accessors methods for class attribute may be called on the class
91 directly, or on objects of that class. Passing a class attribute to
92 the constructor will not set it.
96 This class exports one function when you use it, C<class_has()>. This
97 works exactly like Moose's C<has()>, but it declares class attributes.
99 One little nit is that if you include C<no Moose> in your class, you
100 won't remove the C<class_has()> function. To do that you must include
101 C<no MooseX::ClassAttribute> as well.
103 If you want to use this module to create class attributes in I<other>
104 classes, you can call the C<process_class_attribute()> function like
107 MooseX::ClassAttribute::process_class_attribute( $package, ... );
109 The first argument is the package which will have the class attribute,
110 and the remaining arguments are the same as those passed to
113 =head2 Implementation and Immutability
115 Underneath the hood, this class creates one new class for each class
116 which has class attributes and sets up delegating methods in the class
117 for which you're creating class attributes. You don't need to worry
118 about this too much, except when it comes to making a class immutable.
120 Since the class attributes are not really stored in your class, you
121 need to make the container class immutable as well as your own ...
123 __PACKAGE__->meta()->make_immutable();
124 MooseX::ClassAttribute::container_class()->meta()->make_immutable();
126 I<This may change in the future!>
130 Dave Rolsky, C<< <autarch@urth.org> >>
134 Please report any bugs or feature requests to
135 C<bug-moosex-classattribute@rt.cpan.org>, or through the web interface
136 at L<http://rt.cpan.org>. I will be notified, and then you'll
137 automatically be notified of progress on your bug as I make changes.
139 =head1 COPYRIGHT & LICENSE
141 Copyright 2007 Dave Rolsky, All Rights Reserved.
143 This program is free software; you can redistribute it and/or modify
144 it under the same terms as Perl itself.