2 package Moose::Meta::Attribute;
7 use Scalar::Util 'weaken', 'reftype';
10 our $VERSION = '0.02';
12 use base 'Class::MOP::Attribute';
14 __PACKAGE__->meta->add_attribute('coerce' => (reader => 'coerce'));
15 __PACKAGE__->meta->add_attribute('weak_ref' => (reader => 'weak_ref'));
16 __PACKAGE__->meta->add_attribute('type_constraint' => (
17 reader => 'type_constraint',
18 predicate => 'has_type_constraint',
21 sub has_coercion { (shift)->coerce() ? 1 : 0 }
22 sub has_weak_ref { (shift)->weak_ref() ? 1 : 0 }
24 __PACKAGE__->meta->add_before_method_modifier('new' => sub {
25 my (undef, undef, %options) = @_;
26 if (exists $options{coerce} && $options{coerce}) {
27 (exists $options{type_constraint})
28 || confess "You cannot have coercion without specifying a type constraint";
29 confess "You cannot have a weak reference to a coerced value"
30 if $options{weak_ref};
34 sub generate_accessor_method {
35 my ($self, $attr_name) = @_;
36 if ($self->has_type_constraint) {
37 if ($self->has_weak_ref) {
39 if (scalar(@_) == 2) {
40 (defined $self->type_constraint->check($_[1]))
41 || confess "Attribute ($attr_name) does not pass the type contraint with '$_[1]'"
43 $_[0]->{$attr_name} = $_[1];
44 weaken($_[0]->{$attr_name});
50 if ($self->has_coercion) {
52 if (scalar(@_) == 2) {
53 my $val = $self->type_constraint->coercion->coerce($_[1]);
54 (defined $self->type_constraint->check($val))
55 || confess "Attribute ($attr_name) does not pass the type contraint with '$val'"
57 $_[0]->{$attr_name} = $val;
64 if (scalar(@_) == 2) {
65 (defined $self->type_constraint->check($_[1]))
66 || confess "Attribute ($attr_name) does not pass the type contraint with '$_[1]'"
68 $_[0]->{$attr_name} = $_[1];
76 if ($self->has_weak_ref) {
78 if (scalar(@_) == 2) {
79 $_[0]->{$attr_name} = $_[1];
80 weaken($_[0]->{$attr_name});
87 $_[0]->{$attr_name} = $_[1] if scalar(@_) == 2;
94 sub generate_writer_method {
95 my ($self, $attr_name) = @_;
96 if ($self->has_type_constraint) {
97 if ($self->has_weak_ref) {
99 (defined $self->type_constraint->check($_[1]))
100 || confess "Attribute ($attr_name) does not pass the type contraint with '$_[1]'"
102 $_[0]->{$attr_name} = $_[1];
103 weaken($_[0]->{$attr_name});
107 if ($self->has_coercion) {
109 my $val = $self->type_constraint->coercion->coerce($_[1]);
110 (defined $self->type_constraint->check($val))
111 || confess "Attribute ($attr_name) does not pass the type contraint with '$val'"
113 $_[0]->{$attr_name} = $val;
118 (defined $self->type_constraint->check($_[1]))
119 || confess "Attribute ($attr_name) does not pass the type contraint with '$_[1]'"
121 $_[0]->{$attr_name} = $_[1];
127 if ($self->has_weak_ref) {
129 $_[0]->{$attr_name} = $_[1];
130 weaken($_[0]->{$attr_name});
134 return sub { $_[0]->{$attr_name} = $_[1] };
147 Moose::Meta::Attribute - The Moose attribute metaobject
153 This is a subclass of L<Class::MOP::Attribute> with Moose specific
162 =item B<generate_accessor_method>
164 =item B<generate_writer_method>
170 =item B<has_type_constraint>
172 =item B<type_constraint>
174 =item B<has_weak_ref>
180 =item B<has_coercion>
186 All complex software has bugs lurking in it, and this module is no
187 exception. If you find a bug please either email me, or add the bug
192 Stevan Little E<lt>stevan@iinteractive.comE<gt>
194 =head1 COPYRIGHT AND LICENSE
196 Copyright 2006 by Infinity Interactive, Inc.
198 L<http://www.iinteractive.com>
200 This library is free software; you can redistribute it and/or modify
201 it under the same terms as Perl itself.