Can't specify prereq version with require
[gitmo/MooseX-ClassAttribute.git] / lib / MooseX / ClassAttribute.pm
CommitLineData
4dee0fd3 1package MooseX::ClassAttribute;
2
4dee0fd3 3use strict;
54a288bd 4use warnings;
4dee0fd3 5
cd8784bb 6# This module doesn't really need these pragmas - this is just for the benefit
7# of prereq scanning.
3d3d1cd7 8use namespace::clean 0.20 ();
9use namespace::autoclean 0.11 ();
cd8784bb 10
3e505970 11use Moose 2.00 ();
bb70fe3a 12use Moose::Exporter;
aafd9839 13use Moose::Util;
63fcc508 14use MooseX::ClassAttribute::Trait::Class;
15use MooseX::ClassAttribute::Trait::Role;
16use MooseX::ClassAttribute::Trait::Application::ToClass;
17use MooseX::ClassAttribute::Trait::Application::ToRole;
0f24a39d 18
7ce5e557 19Moose::Exporter->setup_import_methods(
20 with_meta => ['class_has'],
21 class_metaroles => {
22 class => ['MooseX::ClassAttribute::Trait::Class'],
23 },
24 role_metaroles => {
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'],
30 },
31);
8d655404 32
9b2bd146 33sub class_has {
cd8784bb 34 my $meta = shift;
35 my $name = shift;
8d655404 36
bb70fe3a 37 my $attrs = ref $name eq 'ARRAY' ? $name : [$name];
54a288bd 38
b0871254 39 my %options = ( definition_context => _caller_info(), @_ );
aafd9839 40
9b2bd146 41 $meta->add_class_attribute( $_, %options ) for @{$attrs};
0f24a39d 42}
43
b0871254 44# Copied from Moose::Util in 2.06
45sub _caller_info {
cd8784bb 46 my $level = @_ ? ( $_[0] + 1 ) : 2;
b0871254 47 my %info;
48 @info{qw(package file line)} = caller($level);
49 return \%info;
50}
51
4dee0fd3 521;
53
0d0bf8c3 54# ABSTRACT: Declare class attributes Moose-style
55
4dee0fd3 56__END__
57
58=pod
59
4dee0fd3 60=head1 SYNOPSIS
61
54a288bd 62 package My::Class;
4dee0fd3 63
54a288bd 64 use Moose;
4dee0fd3 65 use MooseX::ClassAttribute;
66
54a288bd 67 class_has 'Cache' =>
68 ( is => 'rw',
69 isa => 'HashRef',
70 default => sub { {} },
71 );
72
73 __PACKAGE__->meta()->make_immutable();
54a288bd 74
75 no Moose;
76 no MooseX::ClassAttribute;
77
78 # then later ...
79
80 My::Class->Cache()->{thing} = ...;
81
54a288bd 82=head1 DESCRIPTION
83
84This module allows you to declare class attributes in exactly the same
169f4cc7 85way as object attributes, using C<class_has()> instead of C<has()>.
54a288bd 86
87You can use any feature of Moose's attribute declarations, including
0388e669 88overriding a parent's attributes, delegation (C<handles>), attribute traits,
89etc. All features should just work. The one exception is the "required" flag,
90which is not allowed for class attributes.
54a288bd 91
169f4cc7 92The accessor methods for class attribute may be called on the class
7dc1418a 93directly, or on objects of that class. Passing a class attribute to
0388e669 94the constructor will not set that attribute.
7dc1418a 95
54a288bd 96=head1 FUNCTIONS
97
98This class exports one function when you use it, C<class_has()>. This
99works exactly like Moose's C<has()>, but it declares class attributes.
100
6c69490f 101One little nit is that if you include C<no Moose> in your class, you won't
102remove the C<class_has()> function. To do that you must include C<no
103MooseX::ClassAttribute> as well. Or you can just use L<namespace::autoclean>
104instead.
54a288bd 105
106=head2 Implementation and Immutability
107
169f4cc7 108This module will add a role to your class's metaclass, See
63fcc508 109L<MooseX::ClassAttribute::Trait::Class> for details. This role
169f4cc7 110provides introspection methods for class attributes.
111
112Class attributes themselves do the
63fcc508 113L<MooseX::ClassAttribute::Trait::Attribute> role.
4dee0fd3 114
169f4cc7 115=head2 Cooperation with Metaclasses and Traits
4dee0fd3 116
169f4cc7 117This module should work with most attribute metaclasses and traits,
118but it's possible that conflicts could occur. This module has been
0388e669 119tested to work with Moose's native traits.
120
121=head2 Class Attributes in Roles
122
123You can add a class attribute to a role. When that role is applied to a class,
124the class will have the relevant class attributes added. Note that attribute
125defaults will be calculated when the class attribute is composed into the
126class.
4dee0fd3 127
7a4a3b1e 128=head1 DONATIONS
129
130If you'd like to thank me for the work I've done on this module,
131please consider making a "donation" to me via PayPal. I spend a lot of
132free time creating free software, and would appreciate any support
133you'd care to offer.
134
135Please note that B<I am not suggesting that you must do this> in order
136for me to continue working on this particular software. I will
137continue to do so, inasmuch as I have in the past, for as long as it
138interests me.
139
140Similarly, a donation made in this way will probably not make me work
141on this software much more, unless I get so many donations that I can
142consider working on free software full time, which seems unlikely at
143best.
144
145To donate, log into PayPal and send money to autarch@urth.org or use
146the button on this page:
147L<http://www.urth.org/~autarch/fs-donation.html>
148
4dee0fd3 149=head1 BUGS
150
54a288bd 151Please report any bugs or feature requests to
152C<bug-moosex-classattribute@rt.cpan.org>, or through the web interface
153at L<http://rt.cpan.org>. I will be notified, and then you'll
154automatically be notified of progress on your bug as I make changes.
4dee0fd3 155
4dee0fd3 156=cut