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} }
28 sub generate_accessor {
29 my $attribute = shift;
31 my $key = $attribute->{init_arg};
32 my $default = $attribute->{default};
33 my $trigger = $attribute->{trigger};
38 if ($attribute->{is} eq 'rw') {
39 $accessor .= 'if (@_) {
40 $self->{$key} = $_[0];';
42 if ($attribute->{weak_ref}) {
43 $accessor .= 'Scalar::Util::weaken($self->{$key});';
47 $accessor .= '$trigger->($self, $_[0], $attribute);';
55 if ($attribute->{lazy}) {
56 $accessor .= '$self->{$key} = ';
57 $accessor .= ref($attribute->{default}) eq 'CODE'
60 $accessor .= ' if !exists($self->{$key});';
63 $accessor .= 'return $self->{$key}
66 return eval $accessor;
69 sub generate_predicate {
70 my $attribute = shift;
71 my $key = $attribute->{init_arg};
73 my $predicate = 'sub { exists($_[0]->{$key}) }';
75 return eval $predicate;
78 sub generate_clearer {
79 my $attribute = shift;
80 my $key = $attribute->{init_arg};
82 my $predicate = 'sub { delete($_[0]->{$key}) }';
84 return eval $predicate;
87 sub generate_handles {
88 my $attribute = shift;
89 my $reader = $attribute->{name};
93 for my $local_method (keys %{ $attribute->{handles} }) {
94 my $remote_method = $attribute->{handles}{$local_method};
98 $self->$reader->$remote_method(@_)
101 $method_map{$local_method} = eval $method;
108 my ($self, $class, $name, %args) = @_;
110 confess "You must specify a default for lazy attribute '$name'"
111 if $args{lazy} && !exists($args{default});
113 confess "Trigger is not allowed on read-only attribute '$name'"
114 if $args{trigger} && $args{is} ne 'rw';
116 confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
117 if ref($args{default})
118 && ref($args{default}) ne 'CODE';
120 $args{handles} = { map { $_ => $_ } @{ $args{handles} } }
122 && ref($args{handles}) eq 'ARRAY';
124 confess "You must pass a HASH or ARRAY to handles"
125 if exists($args{handles})
126 && ref($args{handles}) ne 'HASH';
128 my $attribute = $self->new(%args, name => $name, class => $class);
129 my $meta = $class->meta;
131 $meta->add_attribute($attribute);
133 # install an accessor
134 if ($attribute->{is} eq 'rw' || $attribute->{is} eq 'ro') {
135 my $accessor = $attribute->generate_accessor;
137 *{ $class . '::' . $name } = $accessor;
140 for my $method (qw/predicate clearer/) {
141 if (exists $attribute->{$method}) {
142 my $generator = "generate_$method";
143 my $coderef = $attribute->$generator;
145 *{ $class . '::' . $attribute->{$method} } = $coderef;
149 if ($attribute->{handles}) {
150 my $method_map = $attribute->generate_handles;
151 for my $method_name (keys %$method_map) {
153 *{ $class . '::' . $method_name } = $method_map->{$method_name};
166 Mouse::Attribute - attribute metaclass
170 =head2 new %args -> Mouse::Attribute
172 Instantiates a new Mouse::Attribute. Does nothing else.
174 =head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute
176 Creates a new attribute in OwnerClass. Accessors and helper methods are
177 installed. Some error checking is done.
179 =head2 name -> AttributeName
181 =head2 class -> OwnerClass
183 =head2 default -> Value
185 =head2 predicate -> MethodName
187 =head2 clearer -> MethodName
189 =head2 handles -> { LocalName => RemoteName }
191 =head2 weak_ref -> Bool
193 =head2 init_arg -> Str
195 Informational methods.
197 =head2 generate_accessor -> CODE
199 Creates a new code reference for the attribute's accessor.
201 =head2 generate_predicate -> CODE
203 Creates a new code reference for the attribute's predicate.
205 =head2 generate_clearer -> CODE
207 Creates a new code reference for the attribute's clearer.
209 =head2 generate_handles -> { MethodName => CODE }
211 Creates a new code reference for each of the attribute's handles methods.