2 package Mouse::Attribute;
12 $args{init_arg} = $args{name}
13 unless exists $args{init_arg};
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} }
29 sub generate_accessor {
30 my $attribute = shift;
32 my $key = $attribute->{init_arg};
33 my $default = $attribute->{default};
34 my $trigger = $attribute->{trigger};
39 if ($attribute->{is} eq 'rw') {
40 $accessor .= 'if (@_) {
41 $self->{$key} = $_[0];';
43 if ($attribute->{weak_ref}) {
44 $accessor .= 'Scalar::Util::weaken($self->{$key});';
48 $accessor .= '$trigger->($self, $_[0], $attribute);';
56 if ($attribute->{lazy}) {
57 $accessor .= '$self->{$key} = ';
58 $accessor .= ref($attribute->{default}) eq 'CODE'
61 $accessor .= ' if !exists($self->{$key});';
64 $accessor .= 'return $self->{$key}
67 return eval $accessor;
70 sub generate_predicate {
71 my $attribute = shift;
72 my $key = $attribute->{init_arg};
74 my $predicate = 'sub { exists($_[0]->{$key}) }';
76 return eval $predicate;
79 sub generate_clearer {
80 my $attribute = shift;
81 my $key = $attribute->{init_arg};
83 my $predicate = 'sub { delete($_[0]->{$key}) }';
85 return eval $predicate;
88 sub generate_handles {
89 my $attribute = shift;
90 my $reader = $attribute->{name};
94 for my $local_method (keys %{ $attribute->{handles} }) {
95 my $remote_method = $attribute->{handles}{$local_method};
99 $self->$reader->$remote_method(@_)
102 $method_map{$local_method} = eval $method;
109 my ($self, $class, $name, %args) = @_;
111 confess "You must specify a default for lazy attribute '$name'"
112 if $args{lazy} && !exists($args{default});
114 confess "Trigger is not allowed on read-only attribute '$name'"
115 if $args{trigger} && $args{is} ne 'rw';
117 confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
118 if ref($args{default})
119 && ref($args{default}) ne 'CODE';
121 $args{handles} = { map { $_ => $_ } @{ $args{handles} } }
123 && ref($args{handles}) eq 'ARRAY';
125 confess "You must pass a HASH or ARRAY to handles"
126 if exists($args{handles})
127 && ref($args{handles}) ne 'HASH';
129 $args{type_constraint} = delete $args{isa};
131 my $attribute = $self->new(%args, name => $name, class => $class);
132 my $meta = $class->meta;
134 $meta->add_attribute($attribute);
136 # install an accessor
137 if ($attribute->{is} eq 'rw' || $attribute->{is} eq 'ro') {
138 my $accessor = $attribute->generate_accessor;
140 *{ $class . '::' . $name } = $accessor;
143 for my $method (qw/predicate clearer/) {
144 if (exists $attribute->{$method}) {
145 my $generator = "generate_$method";
146 my $coderef = $attribute->$generator;
148 *{ $class . '::' . $attribute->{$method} } = $coderef;
152 if ($attribute->{handles}) {
153 my $method_map = $attribute->generate_handles;
154 for my $method_name (keys %$method_map) {
156 *{ $class . '::' . $method_name } = $method_map->{$method_name};
169 Mouse::Attribute - attribute metaclass
173 =head2 new %args -> Mouse::Attribute
175 Instantiates a new Mouse::Attribute. Does nothing else.
177 =head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute
179 Creates a new attribute in OwnerClass. Accessors and helper methods are
180 installed. Some error checking is done.
182 =head2 name -> AttributeName
184 =head2 class -> OwnerClass
186 =head2 default -> Value
188 =head2 predicate -> MethodName
190 =head2 clearer -> MethodName
192 =head2 handles -> { LocalName => RemoteName }
194 =head2 weak_ref -> Bool
196 =head2 init_arg -> Str
198 Informational methods.
200 =head2 generate_accessor -> CODE
202 Creates a new code reference for the attribute's accessor.
204 =head2 generate_predicate -> CODE
206 Creates a new code reference for the attribute's predicate.
208 =head2 generate_clearer -> CODE
210 Creates a new code reference for the attribute's clearer.
212 =head2 generate_handles -> { MethodName => CODE }
214 Creates a new code reference for each of the attribute's handles methods.