Throw an error if the constructor receives a class attribute.
[gitmo/MooseX-ClassAttribute.git] / lib / MooseX / ClassAttribute.pm
1 package MooseX::ClassAttribute;
2
3 use warnings;
4 use strict;
5
6 our $VERSION = '0.01';
7 our $AUTHORITY = 'cpan:DROLSKY';
8
9 use Moose;
10 use MooseX::ClassAttribute::Meta::Method::Accessor;
11
12 extends 'Moose::Meta::Attribute';
13
14
15 sub accessor_metaclass { 'MooseX::ClassAttribute::Meta::Method::Accessor' }
16
17 # This is called when an object is constructed.
18 sub initialize_instance_slot
19 {
20     my ( $self, $meta_instance, $instance, $params ) = @_;
21
22     return unless $self->has_init_arg();
23
24     my $init_arg = $self->init_arg();
25
26     confess "Cannot set a class attribute via the constructor ($init_arg)"
27         if exists $params->{$init_arg};
28
29     return;
30 }
31
32
33 # This is the bit of magic that lets you specify the metaclass as
34 # 'ClassAttribute' rather than the full name when creating an
35 # attribute.
36 package Moose::Meta::Attribute::Custom::ClassAttribute;
37
38 sub register_implementation { 'MooseX::ClassAttribute' }
39
40
41 1;
42
43 __END__
44
45 =pod
46
47 =head1 NAME
48
49 MooseX::ClassAttribute - The fantastic new MooseX::ClassAttribute!
50
51 =head1 SYNOPSIS
52
53 Quick summary of what the module does.
54
55 Perhaps a little code snippet.
56
57     use MooseX::ClassAttribute;
58
59     my $foo = MooseX::ClassAttribute->new();
60
61     ...
62
63 =head1 METHODS
64
65 This class provides the following methods
66
67 =head1 AUTHOR
68
69 Dave Rolsky, C<< <autarch@urth.org> >>
70
71 =head1 BUGS
72
73 Please report any bugs or feature requests to C<bug-moosex-classattribute@rt.cpan.org>,
74 or through the web interface at L<http://rt.cpan.org>.  I will be
75 notified, and then you'll automatically be notified of progress on
76 your bug as I make changes.
77
78 =head1 COPYRIGHT & LICENSE
79
80 Copyright 2007 Dave Rolsky, All Rights Reserved.
81
82 This program is free software; you can redistribute it and/or modify
83 it under the same terms as Perl itself.
84
85 =cut