Add predicates to the informational Mouse::Attribute attributes
[gitmo/Mouse.git] / lib / Mouse / Attribute.pm
1 #!/usr/bin/env perl
2 package Mouse::Attribute;
3 use strict;
4 use warnings;
5
6 use Carp 'confess';
7
8 sub new {
9     my $class = shift;
10     my %args  = @_;
11
12     $args{init_arg} = $args{name}
13         unless exists $args{init_arg};
14     $args{is} ||= '';
15
16     bless \%args, $class;
17 }
18
19 sub name            { $_[0]->{name}            }
20 sub class           { $_[0]->{class}           }
21 sub default         { $_[0]->{default}         }
22 sub predicate       { $_[0]->{predicate}       }
23 sub clearer         { $_[0]->{clearer}         }
24 sub handles         { $_[0]->{handles}         }
25 sub weak_ref        { $_[0]->{weak_ref}        }
26 sub init_arg        { $_[0]->{init_arg}        }
27 sub type_constraint { $_[0]->{type_constraint} }
28
29 sub has_name            { exists $_[0]->{name}            }
30 sub has_class           { exists $_[0]->{class}           }
31 sub has_default         { exists $_[0]->{default}         }
32 sub has_predicate       { exists $_[0]->{predicate}       }
33 sub has_clearer         { exists $_[0]->{clearer}         }
34 sub has_handles         { exists $_[0]->{handles}         }
35 sub has_weak_ref        { exists $_[0]->{weak_ref}        }
36 sub has_init_arg        { exists $_[0]->{init_arg}        }
37 sub has_type_constraint { exists $_[0]->{type_constraint} }
38
39 sub generate_accessor {
40     my $attribute = shift;
41
42     my $name       = $attribute->{name};
43     my $key        = $attribute->{init_arg};
44     my $default    = $attribute->{default};
45     my $trigger    = $attribute->{trigger};
46     my $type       = $attribute->{type_constraint};
47
48     my $constraint = sub {
49         return unless $type;
50
51         my $checker = Mouse::TypeRegistry->optimized_constraints->{$type};
52         return $checker if $checker;
53
54         confess "Unable to parse type constraint '$type'";
55     }->();
56
57     my $accessor = 'sub {
58         my $self = shift;';
59
60     if ($attribute->{is} eq 'rw') {
61         $accessor .= 'if (@_) {
62             local $_ = $_[0];';
63
64         if ($constraint) {
65             $accessor .= 'Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_") unless $constraint->();'
66         }
67
68         $accessor .= '$self->{$key} = $_;';
69
70         if ($attribute->{weak_ref}) {
71             $accessor .= 'Scalar::Util::weaken($self->{$key});';
72         }
73
74         if ($trigger) {
75             $accessor .= '$trigger->($self, $_, $attribute);';
76         }
77
78         $accessor .= '}';
79     }
80     else {
81     }
82
83     if ($attribute->{lazy}) {
84         $accessor .= '$self->{$key} = ';
85         $accessor .= ref($attribute->{default}) eq 'CODE'
86                    ? '$default->($self)'
87                    : '$default';
88         $accessor .= ' if !exists($self->{$key});';
89     }
90
91     $accessor .= 'return $self->{$key}
92     }';
93
94     return eval $accessor;
95 }
96
97 sub generate_predicate {
98     my $attribute = shift;
99     my $key = $attribute->{init_arg};
100
101     my $predicate = 'sub { exists($_[0]->{$key}) }';
102
103     return eval $predicate;
104 }
105
106 sub generate_clearer {
107     my $attribute = shift;
108     my $key = $attribute->{init_arg};
109
110     my $predicate = 'sub { delete($_[0]->{$key}) }';
111
112     return eval $predicate;
113 }
114
115 sub generate_handles {
116     my $attribute = shift;
117     my $reader = $attribute->{name};
118
119     my %method_map;
120
121     for my $local_method (keys %{ $attribute->{handles} }) {
122         my $remote_method = $attribute->{handles}{$local_method};
123
124         my $method = 'sub {
125             my $self = shift;
126             $self->$reader->$remote_method(@_)
127         }';
128
129         $method_map{$local_method} = eval $method;
130     }
131
132     return \%method_map;
133 }
134
135 sub create {
136     my ($self, $class, $name, %args) = @_;
137
138     confess "You must specify a default for lazy attribute '$name'"
139         if $args{lazy} && !exists($args{default});
140
141     confess "Trigger is not allowed on read-only attribute '$name'"
142         if $args{trigger} && $args{is} ne 'rw';
143
144     confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
145         if ref($args{default})
146         && ref($args{default}) ne 'CODE';
147
148     $args{handles} = { map { $_ => $_ } @{ $args{handles} } }
149         if $args{handles}
150         && ref($args{handles}) eq 'ARRAY';
151
152     confess "You must pass a HASH or ARRAY to handles"
153         if exists($args{handles})
154         && ref($args{handles}) ne 'HASH';
155
156     $args{type_constraint} = delete $args{isa};
157
158     my $attribute = $self->new(%args, name => $name, class => $class);
159     my $meta = $class->meta;
160
161     $meta->add_attribute($attribute);
162
163     # install an accessor
164     if ($attribute->{is} eq 'rw' || $attribute->{is} eq 'ro') {
165         my $accessor = $attribute->generate_accessor;
166         no strict 'refs';
167         *{ $class . '::' . $name } = $accessor;
168     }
169
170     for my $method (qw/predicate clearer/) {
171         if (exists $attribute->{$method}) {
172             my $generator = "generate_$method";
173             my $coderef = $attribute->$generator;
174             no strict 'refs';
175             *{ $class . '::' . $attribute->{$method} } = $coderef;
176         }
177     }
178
179     if ($attribute->{handles}) {
180         my $method_map = $attribute->generate_handles;
181         for my $method_name (keys %$method_map) {
182             no strict 'refs';
183             *{ $class . '::' . $method_name } = $method_map->{$method_name};
184         }
185     }
186
187     return $attribute;
188 }
189
190 1;
191
192 __END__
193
194 =head1 NAME
195
196 Mouse::Attribute - attribute metaclass
197
198 =head1 METHODS
199
200 =head2 new %args -> Mouse::Attribute
201
202 Instantiates a new Mouse::Attribute. Does nothing else.
203
204 =head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute
205
206 Creates a new attribute in OwnerClass. Accessors and helper methods are
207 installed. Some error checking is done.
208
209 =head2 name -> AttributeName
210
211 =head2 class -> OwnerClass
212
213 =head2 default -> Value
214
215 =head2 predicate -> MethodName
216
217 =head2 clearer -> MethodName
218
219 =head2 handles -> { LocalName => RemoteName }
220
221 =head2 weak_ref -> Bool
222
223 =head2 init_arg -> Str
224
225 Informational methods.
226
227 =head2 generate_accessor -> CODE
228
229 Creates a new code reference for the attribute's accessor.
230
231 =head2 generate_predicate -> CODE
232
233 Creates a new code reference for the attribute's predicate.
234
235 =head2 generate_clearer -> CODE
236
237 Creates a new code reference for the attribute's clearer.
238
239 =head2 generate_handles -> { MethodName => CODE }
240
241 Creates a new code reference for each of the attribute's handles methods.
242
243 =cut
244