Commit | Line | Data |
4dee0fd3 |
1 | package MooseX::ClassAttribute; |
2 | |
4dee0fd3 |
3 | use strict; |
54a288bd |
4 | use warnings; |
4dee0fd3 |
5 | |
cd8784bb |
6 | # This module doesn't really need these pragmas - this is just for the benefit |
7 | # of prereq scanning. |
3d3d1cd7 |
8 | use namespace::clean 0.20 (); |
9 | use namespace::autoclean 0.11 (); |
cd8784bb |
10 | |
3e505970 |
11 | use Moose 2.00 (); |
bb70fe3a |
12 | use Moose::Exporter; |
aafd9839 |
13 | use Moose::Util; |
63fcc508 |
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; |
0f24a39d |
18 | |
7ce5e557 |
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 | ); |
8d655404 |
32 | |
9b2bd146 |
33 | sub 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 |
45 | sub _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 |
52 | 1; |
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 | |
84 | This module allows you to declare class attributes in exactly the same |
169f4cc7 |
85 | way as object attributes, using C<class_has()> instead of C<has()>. |
54a288bd |
86 | |
87 | You can use any feature of Moose's attribute declarations, including |
0388e669 |
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. |
54a288bd |
91 | |
169f4cc7 |
92 | The accessor methods for class attribute may be called on the class |
7dc1418a |
93 | directly, or on objects of that class. Passing a class attribute to |
0388e669 |
94 | the constructor will not set that attribute. |
7dc1418a |
95 | |
54a288bd |
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 | |
6c69490f |
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. |
54a288bd |
105 | |
106 | =head2 Implementation and Immutability |
107 | |
169f4cc7 |
108 | This module will add a role to your class's metaclass, See |
63fcc508 |
109 | L<MooseX::ClassAttribute::Trait::Class> for details. This role |
169f4cc7 |
110 | provides introspection methods for class attributes. |
111 | |
112 | Class attributes themselves do the |
63fcc508 |
113 | L<MooseX::ClassAttribute::Trait::Attribute> role. |
4dee0fd3 |
114 | |
169f4cc7 |
115 | =head2 Cooperation with Metaclasses and Traits |
4dee0fd3 |
116 | |
169f4cc7 |
117 | This module should work with most attribute metaclasses and traits, |
118 | but it's possible that conflicts could occur. This module has been |
0388e669 |
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. |
4dee0fd3 |
127 | |
7a4a3b1e |
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 | |
4dee0fd3 |
149 | =head1 BUGS |
150 | |
54a288bd |
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. |
4dee0fd3 |
155 | |
4dee0fd3 |
156 | =cut |