Make prereq requirements explicit in code
[gitmo/MooseX-ClassAttribute.git] / lib / MooseX / ClassAttribute.pm
1 package MooseX::ClassAttribute;
2
3 use strict;
4 use warnings;
5
6 # This module doesn't really need these pragmas - this is just for the benefit
7 # of prereq scanning.
8 require namespace::clean 0.20;
9 require namespace::autoclean 0.11;
10
11 use Moose 2.00 ();
12 use Moose::Exporter;
13 use Moose::Util;
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;
18
19 Moose::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 );
32
33 sub class_has {
34     my $meta = shift;
35     my $name = shift;
36
37     my $attrs = ref $name eq 'ARRAY' ? $name : [$name];
38
39     my %options = ( definition_context => _caller_info(), @_ );
40
41     $meta->add_class_attribute( $_, %options ) for @{$attrs};
42 }
43
44 # Copied from Moose::Util in 2.06
45 sub _caller_info {
46     my $level = @_ ? ( $_[0] + 1 ) : 2;
47     my %info;
48     @info{qw(package file line)} = caller($level);
49     return \%info;
50 }
51
52 1;
53
54 # ABSTRACT: Declare class attributes Moose-style
55
56 __END__
57
58 =pod
59
60 =head1 SYNOPSIS
61
62     package My::Class;
63
64     use Moose;
65     use MooseX::ClassAttribute;
66
67     class_has 'Cache' =>
68         ( is      => 'rw',
69           isa     => 'HashRef',
70           default => sub { {} },
71         );
72
73     __PACKAGE__->meta()->make_immutable();
74
75     no Moose;
76     no MooseX::ClassAttribute;
77
78     # then later ...
79
80     My::Class->Cache()->{thing} = ...;
81
82 =head1 DESCRIPTION
83
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()>.
86
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.
91
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.
95
96 =head1 FUNCTIONS
97
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.
100
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>
104 instead.
105
106 =head2 Implementation and Immutability
107
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.
111
112 Class attributes themselves do the
113 L<MooseX::ClassAttribute::Trait::Attribute> role.
114
115 =head2 Cooperation with Metaclasses and Traits
116
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.
120
121 =head2 Class Attributes in Roles
122
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
126 class.
127
128 =head1 DONATIONS
129
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
133 you'd care to offer.
134
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
138 interests me.
139
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
143 best.
144
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>
148
149 =head1 BUGS
150
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.
155
156 =cut