2 package Mouse::Attribute;
12 $args{init_arg} ||= $args{name};
18 sub name { $_[0]->{name} }
19 sub class { $_[0]->{class} }
20 sub default { $_[0]->{default} }
21 sub predicate { $_[0]->{predicate} }
22 sub clearer { $_[0]->{clearer} }
23 sub handles { $_[0]->{handles} }
24 sub weak_ref { $_[0]->{weak_ref} }
25 sub init_arg { $_[0]->{init_arg} }
27 sub generate_accessor {
28 my $attribute = shift;
30 my $key = $attribute->{init_arg};
31 my $default = $attribute->{default};
32 my $trigger = $attribute->{trigger};
37 if ($attribute->{is} eq 'rw') {
38 $accessor .= 'if (@_) {
39 $self->{$key} = $_[0];';
41 if ($attribute->{weak_ref}) {
42 $accessor .= 'Scalar::Util::weaken($self->{$key});';
46 $accessor .= '$trigger->($self, $_[0], $attribute);';
54 if ($attribute->{lazy}) {
55 $accessor .= '$self->{$key} = ';
56 $accessor .= ref($attribute->{default}) eq 'CODE'
59 $accessor .= ' if !exists($self->{$key});';
62 $accessor .= 'return $self->{$key}
65 return eval $accessor;
68 sub generate_predicate {
69 my $attribute = shift;
70 my $key = $attribute->{init_arg};
72 my $predicate = 'sub { exists($_[0]->{$key}) }';
74 return eval $predicate;
77 sub generate_clearer {
78 my $attribute = shift;
79 my $key = $attribute->{init_arg};
81 my $predicate = 'sub { delete($_[0]->{$key}) }';
83 return eval $predicate;
86 sub generate_handles {
87 my $attribute = shift;
88 my $reader = $attribute->{name};
92 for my $local_method (keys %{ $attribute->{handles} }) {
93 my $remote_method = $attribute->{handles}{$local_method};
97 $self->$reader->$remote_method(@_)
100 $method_map{$local_method} = eval $method;
107 my ($self, $class, $name, %args) = @_;
109 confess "You must specify a default for lazy attribute '$name'"
110 if $args{lazy} && !exists($args{default});
112 confess "Trigger is not allowed on read-only attribute '$name'"
113 if $args{trigger} && $args{is} ne 'rw';
115 confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
116 if ref($args{default})
117 && ref($args{default}) ne 'CODE';
119 $args{handles} = { map { $_ => $_ } @{ $args{handles} } }
121 && ref($args{handles}) eq 'ARRAY';
123 confess "You must pass a HASH or ARRAY to handles"
124 if exists($args{handles})
125 && ref($args{handles}) ne 'HASH';
127 my $attribute = $self->new(%args, name => $name, class => $class);
128 my $meta = $class->meta;
130 # install an accessor
131 if ($attribute->{is} eq 'rw' || $attribute->{is} eq 'ro') {
132 my $accessor = $attribute->generate_accessor;
134 *{ $class . '::' . $name } = $accessor;
137 $meta->add_attribute($attribute);
139 for my $method (qw/predicate clearer/) {
140 if (exists $attribute->{$method}) {
141 my $generator = "generate_$method";
142 my $coderef = $attribute->$generator;
144 *{ $class . '::' . $attribute->{$method} } = $coderef;
148 if ($attribute->{handles}) {
149 my $method_map = $attribute->generate_handles;
150 for my $method_name (keys %$method_map) {
152 *{ $class . '::' . $method_name } = $method_map->{$method_name};
165 Mouse::Attribute - attribute metaclass
169 =head2 new %args -> Mouse::Attribute
171 Instantiates a new Mouse::Attribute. Does nothing else.
173 =head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute
175 Creates a new attribute in OwnerClass. Accessors and helper methods are
176 installed. Some error checking is done.
178 =head2 name -> AttributeName
180 =head2 class -> OwnerClass
182 =head2 default -> Value
184 =head2 predicate -> MethodName
186 =head2 clearer -> MethodName
188 =head2 handles -> { LocalName => RemoteName }
190 =head2 weak_ref -> Bool
192 =head2 init_arg -> Str
194 Informational methods.
196 =head2 generate_accessor -> CODE
198 Creates a new code reference for the attribute's accessor.
200 =head2 generate_predicate -> CODE
202 Creates a new code reference for the attribute's predicate.
204 =head2 generate_clearer -> CODE
206 Creates a new code reference for the attribute's clearer.
208 =head2 generate_handles -> { MethodName => CODE }
210 Creates a new code reference for each of the attribute's handles methods.