Mostly working implementation, but mixing with MX::AH isn't working
[gitmo/MooseX-ClassAttribute.git] / lib / MooseX / ClassAttribute.pm
CommitLineData
4dee0fd3 1package MooseX::ClassAttribute;
2
4dee0fd3 3use strict;
54a288bd 4use warnings;
4dee0fd3 5
bb70fe3a 6our $VERSION = '0.05';
0f24a39d 7our $AUTHORITY = 'cpan:DROLSKY';
8
bb70fe3a 9use Moose ();
10use Moose::Exporter;
11use MooseX::ClassAttribute::Role::Meta::Class;
0f24a39d 12
bb70fe3a 13Moose::Exporter->setup_import_methods
14 ( with_caller => [ 'class_has' ] );
a124b299 15
0f24a39d 16
bb70fe3a 17sub init_meta
0f24a39d 18{
bb70fe3a 19 shift;
20 my %p = @_;
54a288bd 21
bb70fe3a 22 Moose->init_meta(%p);
8d655404 23
bb70fe3a 24 return
25 Moose::Util::MetaRole::apply_metaclass_roles
26 ( for_class => $p{for_class},
27 metaclass_roles => [ 'MooseX::ClassAttribute::Role::Meta::Class' ],
28 );
8d655404 29}
30
bb70fe3a 31sub class_has
8d655404 32{
bb70fe3a 33 my $caller = shift;
34 my $name = shift;
35 my %options = @_;
8d655404 36
bb70fe3a 37 my $attrs = ref $name eq 'ARRAY' ? $name : [$name];
54a288bd 38
bb70fe3a 39 Class::MOP::Class
40 ->initialize($caller)
41 ->add_class_attribute( $_, %options )
42 for @{ $attrs };
0f24a39d 43}
44
4dee0fd3 451;
46
47__END__
48
49=pod
50
51=head1 NAME
52
54a288bd 53MooseX::ClassAttribute - Declare class attributes Moose-style
4dee0fd3 54
4dee0fd3 55=head1 SYNOPSIS
56
54a288bd 57 package My::Class;
4dee0fd3 58
54a288bd 59 use Moose;
4dee0fd3 60 use MooseX::ClassAttribute;
61
54a288bd 62 class_has 'Cache' =>
63 ( is => 'rw',
64 isa => 'HashRef',
65 default => sub { {} },
66 );
67
68 __PACKAGE__->meta()->make_immutable();
ac5d97b7 69 MooseX::ClassAttribute::container_class()->meta()->make_immutable();
54a288bd 70
71 no Moose;
72 no MooseX::ClassAttribute;
73
74 # then later ...
75
76 My::Class->Cache()->{thing} = ...;
77
78
79=head1 DESCRIPTION
80
81This module allows you to declare class attributes in exactly the same
82way as you declare object attributes, except using C<class_has()>
83instead of C<has()>. It is also possible to make these attributes
84immutable (and faster) just as you can with normal Moose attributes.
85
86You can use any feature of Moose's attribute declarations, including
87overriding a parent's attributes, delegation (C<handles>), and
88attribute metaclasses, and it should just work.
89
7dc1418a 90The accessors methods for class attribute may be called on the class
91directly, or on objects of that class. Passing a class attribute to
92the constructor will not set it.
93
54a288bd 94=head1 FUNCTIONS
95
96This class exports one function when you use it, C<class_has()>. This
97works exactly like Moose's C<has()>, but it declares class attributes.
98
170db2d9 99One little nit is that if you include C<no Moose> in your class, you
54a288bd 100won't remove the C<class_has()> function. To do that you must include
101C<no MooseX::ClassAttribute> as well.
102
8d655404 103If you want to use this module to create class attributes in I<other>
104classes, you can call the C<process_class_attribute()> function like
105this:
106
107 MooseX::ClassAttribute::process_class_attribute( $package, ... );
108
109The first argument is the package which will have the class attribute,
110and the remaining arguments are the same as those passed to
111C<class_has()>.
112
54a288bd 113=head2 Implementation and Immutability
114
115Underneath the hood, this class creates one new class for each class
116which has class attributes and sets up delegating methods in the class
117for which you're creating class attributes. You don't need to worry
118about this too much, except when it comes to making a class immutable.
4dee0fd3 119
54a288bd 120Since the class attributes are not really stored in your class, you
ac5d97b7 121need to make the container class immutable as well as your own ...
4dee0fd3 122
54a288bd 123 __PACKAGE__->meta()->make_immutable();
ac5d97b7 124 MooseX::ClassAttribute::container_class()->meta()->make_immutable();
4dee0fd3 125
54a288bd 126I<This may change in the future!>
4dee0fd3 127
128=head1 AUTHOR
129
130Dave Rolsky, C<< <autarch@urth.org> >>
131
132=head1 BUGS
133
54a288bd 134Please report any bugs or feature requests to
135C<bug-moosex-classattribute@rt.cpan.org>, or through the web interface
136at L<http://rt.cpan.org>. I will be notified, and then you'll
137automatically be notified of progress on your bug as I make changes.
4dee0fd3 138
139=head1 COPYRIGHT & LICENSE
140
141Copyright 2007 Dave Rolsky, All Rights Reserved.
142
143This program is free software; you can redistribute it and/or modify
144it under the same terms as Perl itself.
145
146=cut