File Coverage

blib/lib/MooseX/ClassAttribute.pm
Criterion Covered Total %
statement 43 43 100.0
branch 2 4 50.0
condition n/a
subroutine 13 13 100.0
pod 0 1 0.0
total 58 61 95.0


line stmt bran cond sub pod time code
1             package MooseX::ClassAttribute;
2              
3 8     8   2865378 use strict;
  8         12  
  8         224  
4 8     8   34 use warnings;
  8         10  
  8         351  
5              
6             our $VERSION = '0.29';
7              
8             # This module doesn't really need these pragmas - this is just for the benefit
9             # of prereq scanning.
10 8     8   3257 use namespace::clean 0.20 ();
  8         36362  
  8         203  
11 8     8   3607 use namespace::autoclean 0.11 ();
  8         8536  
  8         198  
12              
13 8     8   43 use Moose 2.00 ();
  8         97  
  8         125  
14 8     8   30 use Moose::Exporter;
  8         9  
  8         55  
15 8     8   289 use Moose::Util;
  8         9  
  8         61  
16 8     8   4140 use MooseX::ClassAttribute::Trait::Class;
  8         18  
  8         278  
17 8     8   3294 use MooseX::ClassAttribute::Trait::Role;
  8         21  
  8         270  
18 8     8   3696 use MooseX::ClassAttribute::Trait::Application::ToClass;
  8         20  
  8         292  
19 8     8   3240 use MooseX::ClassAttribute::Trait::Application::ToRole;
  8         22  
  8         1430  
20              
21             Moose::Exporter->setup_import_methods(
22             with_meta => ['class_has'],
23             class_metaroles => {
24             class => ['MooseX::ClassAttribute::Trait::Class'],
25             },
26             role_metaroles => {
27             role => ['MooseX::ClassAttribute::Trait::Role'],
28             application_to_class =>
29             ['MooseX::ClassAttribute::Trait::Application::ToClass'],
30             application_to_role =>
31             ['MooseX::ClassAttribute::Trait::Application::ToRole'],
32             },
33             );
34              
35             sub class_has {
36 69     69 0 38767 my $meta = shift;
37 69         93 my $name = shift;
38              
39 69 50       189 my $attrs = ref $name eq 'ARRAY' ? $name : [$name];
40              
41 69         149 my %options = ( definition_context => _caller_info(), @_ );
42              
43 69         69 $meta->add_class_attribute( $_, %options ) for @{$attrs};
  69         328  
44             }
45              
46             # Copied from Moose::Util in 2.06
47             sub _caller_info {
48 69 50   69   136 my $level = @_ ? ( $_[0] + 1 ) : 2;
49 69         65 my %info;
50 69         519 @info{qw(package file line)} = caller($level);
51 69         283 return \%info;
52             }
53              
54             1;
55              
56             # ABSTRACT: Declare class attributes Moose-style
57              
58             __END__
59              
60             =pod
61              
62             =encoding UTF-8
63              
64             =head1 NAME
65              
66             MooseX::ClassAttribute - Declare class attributes Moose-style
67              
68             =head1 VERSION
69              
70             version 0.29
71              
72             =head1 SYNOPSIS
73              
74             package My::Class;
75              
76             use Moose;
77             use MooseX::ClassAttribute;
78              
79             class_has 'Cache' =>
80             ( is => 'rw',
81             isa => 'HashRef',
82             default => sub { {} },
83             );
84              
85             __PACKAGE__->meta()->make_immutable();
86              
87             no Moose;
88             no MooseX::ClassAttribute;
89              
90             # then later ...
91              
92             My::Class->Cache()->{thing} = ...;
93              
94             =head1 DESCRIPTION
95              
96             This module allows you to declare class attributes in exactly the same
97             way as object attributes, using C<class_has()> instead of C<has()>.
98              
99             You can use any feature of Moose's attribute declarations, including
100             overriding a parent's attributes, delegation (C<handles>), attribute traits,
101             etc. All features should just work. The one exception is the "required" flag,
102             which is not allowed for class attributes.
103              
104             The accessor methods for class attribute may be called on the class
105             directly, or on objects of that class. Passing a class attribute to
106             the constructor will not set that attribute.
107              
108             =head1 FUNCTIONS
109              
110             This class exports one function when you use it, C<class_has()>. This
111             works exactly like Moose's C<has()>, but it declares class attributes.
112              
113             One little nit is that if you include C<no Moose> in your class, you won't
114             remove the C<class_has()> function. To do that you must include C<no
115             MooseX::ClassAttribute> as well. Or you can just use L<namespace::autoclean>
116             instead.
117              
118             =head2 Implementation and Immutability
119              
120             This module will add a role to your class's metaclass, See
121             L<MooseX::ClassAttribute::Trait::Class> for details. This role
122             provides introspection methods for class attributes.
123              
124             Class attributes themselves do the
125             L<MooseX::ClassAttribute::Trait::Attribute> role.
126              
127             =head2 Cooperation with Metaclasses and Traits
128              
129             This module should work with most attribute metaclasses and traits,
130             but it's possible that conflicts could occur. This module has been
131             tested to work with Moose's native traits.
132              
133             =head2 Class Attributes in Roles
134              
135             You can add a class attribute to a role. When that role is applied to a class,
136             the class will have the relevant class attributes added. Note that attribute
137             defaults will be calculated when the class attribute is composed into the
138             class.
139              
140             =head1 SUPPORT
141              
142             Bugs may be submitted through L<the RT bug tracker|http://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-ClassAttribute>
143             (or L<bug-moosex-classattribute@rt.cpan.org|mailto:bug-moosex-classattribute@rt.cpan.org>).
144              
145             I am also usually active on IRC as 'drolsky' on C<irc://irc.perl.org>.
146              
147             =head1 DONATIONS
148              
149             If you'd like to thank me for the work I've done on this module, please
150             consider making a "donation" to me via PayPal. I spend a lot of free time
151             creating free software, and would appreciate any support you'd care to offer.
152              
153             Please note that B<I am not suggesting that you must do this> in order for me
154             to continue working on this particular software. I will continue to do so,
155             inasmuch as I have in the past, for as long as it interests me.
156              
157             Similarly, a donation made in this way will probably not make me work on this
158             software much more, unless I get so many donations that I can consider working
159             on free software full time (let's all have a chuckle at that together).
160              
161             To donate, log into PayPal and send money to autarch@urth.org, or use the
162             button at L<http://www.urth.org/~autarch/fs-donation.html>.
163              
164             =head1 AUTHOR
165              
166             Dave Rolsky <autarch@urth.org>
167              
168             =head1 CONTRIBUTORS
169              
170             =for stopwords Andrew Rodland Karen Etheridge Rafael Kitover Robert Buels Shawn M Moore
171              
172             =over 4
173              
174             =item *
175              
176             Andrew Rodland <andrew@cleverdomain.org>
177              
178             =item *
179              
180             Karen Etheridge <ether@cpan.org>
181              
182             =item *
183              
184             Rafael Kitover <rkitover@cpan.org>
185              
186             =item *
187              
188             Robert Buels <rmb32@cornell.edu>
189              
190             =item *
191              
192             Shawn M Moore <sartak@gmail.com>
193              
194             =back
195              
196             =head1 COPYRIGHT AND LICENCE
197              
198             This software is Copyright (c) 2016 by Dave Rolsky.
199              
200             This is free software, licensed under:
201              
202             The Artistic License 2.0 (GPL Compatible)
203              
204             =cut