Add predicates to the informational Mouse::Attribute attributes
[gitmo/Mouse.git] / lib / Mouse / Attribute.pm
CommitLineData
c3398f5b 1#!/usr/bin/env perl
2package Mouse::Attribute;
3use strict;
4use warnings;
5
6use Carp 'confess';
7
8sub new {
9 my $class = shift;
10 my %args = @_;
11
7ee01d77 12 $args{init_arg} = $args{name}
13 unless exists $args{init_arg};
c3398f5b 14 $args{is} ||= '';
15
16 bless \%args, $class;
17}
18
186657a9 19sub name { $_[0]->{name} }
20sub class { $_[0]->{class} }
21sub default { $_[0]->{default} }
22sub predicate { $_[0]->{predicate} }
23sub clearer { $_[0]->{clearer} }
24sub handles { $_[0]->{handles} }
25sub weak_ref { $_[0]->{weak_ref} }
26sub init_arg { $_[0]->{init_arg} }
27sub type_constraint { $_[0]->{type_constraint} }
c3398f5b 28
ccea8101 29sub has_name { exists $_[0]->{name} }
30sub has_class { exists $_[0]->{class} }
31sub has_default { exists $_[0]->{default} }
32sub has_predicate { exists $_[0]->{predicate} }
33sub has_clearer { exists $_[0]->{clearer} }
34sub has_handles { exists $_[0]->{handles} }
35sub has_weak_ref { exists $_[0]->{weak_ref} }
36sub has_init_arg { exists $_[0]->{init_arg} }
37sub has_type_constraint { exists $_[0]->{type_constraint} }
38
c3398f5b 39sub generate_accessor {
40 my $attribute = shift;
41
a707f587 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 }->();
c3398f5b 56
57 my $accessor = 'sub {
58 my $self = shift;';
59
60 if ($attribute->{is} eq 'rw') {
61 $accessor .= 'if (@_) {
a707f587 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} = $_;';
c3398f5b 69
70 if ($attribute->{weak_ref}) {
71 $accessor .= 'Scalar::Util::weaken($self->{$key});';
72 }
73
74 if ($trigger) {
a707f587 75 $accessor .= '$trigger->($self, $_, $attribute);';
c3398f5b 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
97sub 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
106sub 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
115sub 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
135sub 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
186657a9 156 $args{type_constraint} = delete $args{isa};
157
c3398f5b 158 my $attribute = $self->new(%args, name => $name, class => $class);
159 my $meta = $class->meta;
160
b2500191 161 $meta->add_attribute($attribute);
162
c3398f5b 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
c3398f5b 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
1901;
191
192__END__
193
194=head1 NAME
195
196Mouse::Attribute - attribute metaclass
197
198=head1 METHODS
199
200=head2 new %args -> Mouse::Attribute
201
202Instantiates a new Mouse::Attribute. Does nothing else.
203
204=head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute
205
206Creates a new attribute in OwnerClass. Accessors and helper methods are
207installed. 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
225Informational methods.
226
227=head2 generate_accessor -> CODE
228
229Creates a new code reference for the attribute's accessor.
230
231=head2 generate_predicate -> CODE
232
233Creates a new code reference for the attribute's predicate.
234
235=head2 generate_clearer -> CODE
236
237Creates a new code reference for the attribute's clearer.
238
239=head2 generate_handles -> { MethodName => CODE }
240
241Creates a new code reference for each of the attribute's handles methods.
242
243=cut
244