2 package Moose::Meta::Attribute;
7 use Scalar::Util 'weaken', 'reftype';
10 use Moose::Util::TypeConstraints ':no_export';
12 our $VERSION = '0.02';
14 use base 'Class::MOP::Attribute';
16 Moose::Meta::Attribute->meta->add_attribute(
17 Class::MOP::Attribute->new('coerce' => (
20 'has_coercion' => sub { $_[0]->coerce() ? 1 : 0 }
25 Moose::Meta::Attribute->meta->add_attribute(
26 Class::MOP::Attribute->new('weak_ref' => (
29 'has_weak_ref' => sub { $_[0]->weak_ref() ? 1 : 0 }
34 Moose::Meta::Attribute->meta->add_attribute(
35 Class::MOP::Attribute->new('type_constraint' => (
36 reader => 'type_constraint',
37 predicate => 'has_type_constraint',
41 Moose::Meta::Attribute->meta->add_before_method_modifier('new' => sub {
42 my (undef, undef, %options) = @_;
43 if (exists $options{coerce} && $options{coerce}) {
44 (exists $options{type_constraint})
45 || confess "You cannot have coercion without specifying a type constraint";
46 confess "You cannot have a weak reference to a coerced value"
47 if $options{weak_ref};
51 sub generate_accessor_method {
52 my ($self, $attr_name) = @_;
53 if ($self->has_type_constraint) {
54 if ($self->has_weak_ref) {
56 if (scalar(@_) == 2) {
57 (defined $self->type_constraint->constraint_code->($_[1]))
58 || confess "Attribute ($attr_name) does not pass the type contraint with '$_[1]'"
60 $_[0]->{$attr_name} = $_[1];
61 weaken($_[0]->{$attr_name});
67 if ($self->has_coercion) {
69 if (scalar(@_) == 2) {
70 my $val = $self->type_constraint->coercion_code->($_[1]);
71 (defined $self->type_constraint->constraint_code->($val))
72 || confess "Attribute ($attr_name) does not pass the type contraint with '$val'"
74 $_[0]->{$attr_name} = $val;
81 if (scalar(@_) == 2) {
82 (defined $self->type_constraint->constraint_code->($_[1]))
83 || confess "Attribute ($attr_name) does not pass the type contraint with '$_[1]'"
85 $_[0]->{$attr_name} = $_[1];
93 if ($self->has_weak_ref) {
95 if (scalar(@_) == 2) {
96 $_[0]->{$attr_name} = $_[1];
97 weaken($_[0]->{$attr_name});
104 $_[0]->{$attr_name} = $_[1] if scalar(@_) == 2;
111 sub generate_writer_method {
112 my ($self, $attr_name) = @_;
113 if ($self->has_type_constraint) {
114 if ($self->has_weak_ref) {
116 (defined $self->type_constraint->constraint_code->($_[1]))
117 || confess "Attribute ($attr_name) does not pass the type contraint with '$_[1]'"
119 $_[0]->{$attr_name} = $_[1];
120 weaken($_[0]->{$attr_name});
124 if ($self->has_coercion) {
126 my $val = $self->type_constraint->coercion_code->($_[1]);
127 (defined $self->type_constraint->constraint_code->($val))
128 || confess "Attribute ($attr_name) does not pass the type contraint with '$val'"
130 $_[0]->{$attr_name} = $val;
135 (defined $self->type_constraint->constraint_code->($_[1]))
136 || confess "Attribute ($attr_name) does not pass the type contraint with '$_[1]'"
138 $_[0]->{$attr_name} = $_[1];
144 if ($self->has_weak_ref) {
146 $_[0]->{$attr_name} = $_[1];
147 weaken($_[0]->{$attr_name});
151 return sub { $_[0]->{$attr_name} = $_[1] };
164 Moose::Meta::Attribute - The Moose attribute metaobject
170 This is a subclass of L<Class::MOP::Attribute> with Moose specific
179 =item B<generate_accessor_method>
181 =item B<generate_writer_method>
187 =item B<has_type_constraint>
189 =item B<type_constraint>
191 =item B<has_weak_ref>
197 =item B<has_coercion>
203 All complex software has bugs lurking in it, and this module is no
204 exception. If you find a bug please either email me, or add the bug
209 Stevan Little E<lt>stevan@iinteractive.comE<gt>
211 =head1 COPYRIGHT AND LICENSE
213 Copyright 2006 by Infinity Interactive, Inc.
215 L<http://www.iinteractive.com>
217 This library is free software; you can redistribute it and/or modify
218 it under the same terms as Perl itself.