Refactor attribute constructor
[gitmo/Mouse.git] / lib / Mouse / Meta / Class.pm
CommitLineData
306290e8 1package Mouse::Meta::Class;
c3398f5b 2use strict;
3use warnings;
4
cecfb973 5use Scalar::Util qw/blessed weaken/;
6d28c5cf 6
fce211ae 7use Mouse::Util qw/get_linear_isa not_supported/;
72b88a88 8
6d28c5cf 9use Mouse::Meta::Method::Constructor;
10use Mouse::Meta::Method::Destructor;
11use Mouse::Meta::Module;
12
3a63a2e7 13use base qw(Mouse::Meta::Module);
14
6cfa1e5e 15sub method_metaclass(){ 'Mouse::Meta::Method' } # required for get_method()
3a63a2e7 16
8e64d0fa 17sub _construct_meta {
88ed7189 18 my($class, %args) = @_;
c3398f5b 19
8536d351 20 $args{attributes} ||= {};
21 $args{methods} ||= {};
22 $args{roles} ||= [];
23
c3398f5b 24 $args{superclasses} = do {
25 no strict 'refs';
88ed7189 26 \@{ $args{package} . '::ISA' };
c3398f5b 27 };
28
7a50b450 29 #return Mouse::Meta::Class->initialize($class)->new_object(%args)
30 # if $class ne __PACKAGE__;
31
8e64d0fa 32 return bless \%args, ref($class) || $class;
7a50b450 33}
34
35sub create_anon_class{
36 my $self = shift;
37 return $self->create(undef, @_);
38}
39
40sub is_anon_class{
41 return exists $_[0]->{anon_serial_id};
c3398f5b 42}
43
afc73948 44sub roles { $_[0]->{roles} }
c3398f5b 45
46sub superclasses {
47 my $self = shift;
48
49 if (@_) {
50 Mouse::load_class($_) for @_;
51 @{ $self->{superclasses} } = @_;
52 }
53
8e64d0fa 54 return @{ $self->{superclasses} };
55}
56
57sub find_method_by_name{
58 my($self, $method_name) = @_;
59 defined($method_name)
60 or $self->throw_error('You must define a method name to find');
61 foreach my $class( $self->linearized_isa ){
62 my $method = $self->initialize($class)->get_method($method_name);
63 return $method if defined $method;
64 }
65 return undef;
66}
67
68sub get_all_methods {
69 my($self) = @_;
70 return map{ $self->find_method_by_name($self) } $self->get_all_method_names;
c3398f5b 71}
72
60cfc6ad 73sub get_all_method_names {
74 my $self = shift;
75 my %uniq;
76 return grep { $uniq{$_}++ == 0 }
3a63a2e7 77 map { Mouse::Meta::Class->initialize($_)->get_method_list() }
60cfc6ad 78 $self->linearized_isa;
79}
80
87ca293b 81sub add_attribute {
c3398f5b 82 my $self = shift;
60f6eba9 83
87ca293b 84 my($attr, $name);
1b9e472d 85
87ca293b 86 if(blessed $_[0]){
87 $attr = $_[0];
1b9e472d 88
87ca293b 89 $attr->isa('Mouse::Meta::Attribute')
90 || $self->throw_error("Your attribute must be an instance of Mouse::Meta::Attribute (or a subclass)");
1b9e472d 91
87ca293b 92 $name = $attr->name;
1b9e472d 93 }
94 else{
87ca293b 95 # _process_attribute
96 $name = shift;
1b9e472d 97
87ca293b 98 my %args = (@_ == 1) ? %{$_[0]} : @_;
1b9e472d 99
87ca293b 100 defined($name)
101 or $self->throw_error('You must provide a name for the attribute');
1b9e472d 102
87ca293b 103 if ($name =~ s/^\+//) { # inherited attributes
104 my $inherited_attr;
105
106 foreach my $class($self->linearized_isa){
107 my $meta = Mouse::Meta::Module::get_metaclass_by_name($class) or next;
108 $inherited_attr = $meta->get_attribute($name) and last;
109 }
110
111 defined($inherited_attr)
112 or $self->throw_error("Could not find an attribute by the name of '$name' to inherit from in ".$self->name);
113
114 $attr = $inherited_attr->clone_and_inherit_options($name, \%args);
115 }
116 else{
117 my($attribute_class, @traits) = Mouse::Meta::Attribute->interpolate_class($name, \%args);
118 $args{traits} = \@traits if @traits;
119
120 $attr = $attribute_class->new($name, \%args);
121 }
122 }
1b9e472d 123
124 weaken( $attr->{associated_class} = $self );
125
126 $self->{attributes}{$attr->name} = $attr;
127 $attr->install_accessors();
128
129 if(!$attr->{associated_methods} && ($attr->{is} || '') ne 'bare'){
130 Carp::cluck(qq{Attribute (}.$attr->name.qq{) of class }.$self->name.qq{ has no associated methods (did you mean to provide an "is" argument?)});
60f6eba9 131 }
1b9e472d 132 return $attr;
c3398f5b 133}
134
d60824af 135sub compute_all_applicable_attributes { shift->get_all_attributes(@_) }
136sub get_all_attributes {
72b88a88 137 my $self = shift;
138 my (@attr, %seen);
139
140 for my $class ($self->linearized_isa) {
141 my $meta = $self->_metaclass_cache($class)
142 or next;
143
144 for my $name (keys %{ $meta->get_attribute_map }) {
145 next if $seen{$name}++;
146 push @attr, $meta->get_attribute($name);
147 }
148 }
149
150 return @attr;
151}
152
8536d351 153sub linearized_isa { @{ get_linear_isa($_[0]->name) } }
154
155sub new_object {
c68b4110 156 my $self = shift;
7a50b450 157 my %args = (@_ == 1 ? %{$_[0]} : @_);
c3398f5b 158
fce211ae 159 my $instance = bless {}, $self->name;
160
1b9e472d 161 $self->_initialize_instance($instance, \%args);
162 return $instance;
163}
164
165sub _initialize_instance{
166 my($self, $instance, $args) = @_;
167
53c495ce 168 my @triggers_queue;
169
fce211ae 170 foreach my $attribute ($self->get_all_attributes) {
8536d351 171 my $from = $attribute->init_arg;
172 my $key = $attribute->name;
173
1b9e472d 174 if (defined($from) && exists($args->{$from})) {
175 $args->{$from} = $attribute->coerce_constraint($args->{$from})
8536d351 176 if $attribute->should_coerce;
8536d351 177
1b9e472d 178 $attribute->verify_against_type_constraint($args->{$from});
179
180 $instance->{$key} = $args->{$from};
8536d351 181
182 weaken($instance->{$key})
fce211ae 183 if ref($instance->{$key}) && $attribute->is_weak_ref;
8536d351 184
185 if ($attribute->has_trigger) {
1b9e472d 186 push @triggers_queue, [ $attribute->trigger, $args->{$from} ];
8536d351 187 }
188 }
189 else {
190 if ($attribute->has_default || $attribute->has_builder) {
191 unless ($attribute->is_lazy) {
192 my $default = $attribute->default;
193 my $builder = $attribute->builder;
194 my $value = $attribute->has_builder
195 ? $instance->$builder
196 : ref($default) eq 'CODE'
197 ? $default->($instance)
198 : $default;
199
200 $value = $attribute->coerce_constraint($value)
201 if $attribute->should_coerce;
202 $attribute->verify_against_type_constraint($value);
203
204 $instance->{$key} = $value;
205
206 weaken($instance->{$key})
fce211ae 207 if ref($instance->{$key}) && $attribute->is_weak_ref;
8536d351 208 }
209 }
210 else {
211 if ($attribute->is_required) {
fce211ae 212 $self->throw_error("Attribute (".$attribute->name.") is required");
8536d351 213 }
214 }
215 }
216 }
53c495ce 217
218 foreach my $trigger_and_value(@triggers_queue){
219 my($trigger, $value) = @{$trigger_and_value};
220 $trigger->($instance, $value);
221 }
222
2efc0af1 223 if($self->is_anon_class){
224 $instance->{__METACLASS__} = $self;
225 }
226
8536d351 227 return $instance;
228}
c3398f5b 229
7a59f4e8 230sub clone_object {
231 my $class = shift;
232 my $instance = shift;
233
234 (blessed($instance) && $instance->isa($class->name))
fce211ae 235 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($instance)");
7a59f4e8 236
237 $class->clone_instance($instance, @_);
238}
239
240sub clone_instance {
241 my ($class, $instance, %params) = @_;
242
243 (blessed($instance))
fce211ae 244 || $class->throw_error("You can only clone instances, ($instance) is not a blessed instance");
7a59f4e8 245
246 my $clone = bless { %$instance }, ref $instance;
247
d60824af 248 foreach my $attr ($class->get_all_attributes()) {
7a59f4e8 249 if ( defined( my $init_arg = $attr->init_arg ) ) {
250 if (exists $params{$init_arg}) {
251 $clone->{ $attr->name } = $params{$init_arg};
252 }
253 }
254 }
255
256 return $clone;
257
258}
259
fc1d8369 260sub make_immutable {
261 my $self = shift;
6a1d1835 262 my %args = (
263 inline_constructor => 1,
e578d610 264 inline_destructor => 1,
6a1d1835 265 @_,
266 );
267
fc1d8369 268 $self->{is_immutable}++;
c7a6403f 269
6a1d1835 270 if ($args{inline_constructor}) {
c7a6403f 271 $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self ));
272 }
273
8632b6fe 274 if ($args{inline_destructor}) {
275 $self->add_method('DESTROY' => Mouse::Meta::Method::Destructor->generate_destructor_method_inline( $self ));
276 }
2276cb14 277
278 # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
279 # at the end of a source file.
280 return 1;
fc1d8369 281}
ad958001 282
fce211ae 283sub make_mutable { not_supported }
ad958001 284
6cfa1e5e 285sub is_immutable { $_[0]->{is_immutable} }
286sub is_mutable { !$_[0]->{is_immutable} }
84ef660f 287
4859d490 288sub _install_modifier {
289 my ( $self, $into, $type, $name, $code ) = @_;
4f5b44a0 290
291 # which is modifer class available?
292 my $modifier_class = do {
293 if (eval "require Class::Method::Modifiers::Fast; 1") {
294 'Class::Method::Modifiers::Fast';
295 } elsif (eval "require Class::Method::Modifiers; 1") {
296 'Class::Method::Modifiers';
297 } else {
298 Carp::croak("Method modifiers require the use of Class::Method::Modifiers or Class::Method::Modifiers::Fast. Please install it from CPAN and file a bug report with this application.");
299 }
300 };
301 my $modifier = $modifier_class->can('_install_modifier');
302
303 # replace this method itself :)
304 {
4f5b44a0 305 no warnings 'redefine';
4f9945f5 306 *_install_modifier = sub {
4f5b44a0 307 my ( $self, $into, $type, $name, $code ) = @_;
308 $modifier->(
309 $into,
310 $type,
311 $name,
312 $code
313 );
6cfa1e5e 314 $self->{methods}{$name}++; # register it to the method map
315 return;
4f5b44a0 316 };
1b79a118 317 }
4f5b44a0 318
319 # call me. for first time.
320 $self->_install_modifier( $into, $type, $name, $code );
4859d490 321}
322
50dc6ee5 323sub add_before_method_modifier {
4859d490 324 my ( $self, $name, $code ) = @_;
325 $self->_install_modifier( $self->name, 'before', $name, $code );
50dc6ee5 326}
327
328sub add_around_method_modifier {
4859d490 329 my ( $self, $name, $code ) = @_;
330 $self->_install_modifier( $self->name, 'around', $name, $code );
50dc6ee5 331}
332
333sub add_after_method_modifier {
4859d490 334 my ( $self, $name, $code ) = @_;
335 $self->_install_modifier( $self->name, 'after', $name, $code );
50dc6ee5 336}
337
67199842 338sub add_override_method_modifier {
339 my ($self, $name, $code) = @_;
340
6cfa1e5e 341 my $package = $self->name;
67199842 342
6cfa1e5e 343 my $body = $package->can($name)
344 or $self->throw_error("You cannot override '$name' because it has no super method");
67199842 345
6cfa1e5e 346 $self->add_method($name => sub { $code->($package, $body, @_) });
67199842 347}
348
47f36c05 349sub does_role {
350 my ($self, $role_name) = @_;
ad958001 351
47f36c05 352 (defined $role_name)
fce211ae 353 || $self->throw_error("You must supply a role name to look for");
ad958001 354
f7fec86c 355 for my $class ($self->linearized_isa) {
08f7a8db 356 my $meta = Mouse::Meta::Module::class_of($class);
3a63a2e7 357 next unless $meta && $meta->can('roles');
358
359 for my $role (@{ $meta->roles }) {
ff687069 360
3a63a2e7 361 return 1 if $role->does_role($role_name);
f7fec86c 362 }
47f36c05 363 }
ad958001 364
47f36c05 365 return 0;
366}
367
c3398f5b 3681;
369
370__END__
371
372=head1 NAME
373
306290e8 374Mouse::Meta::Class - hook into the Mouse MOP
c3398f5b 375
376=head1 METHODS
377
306290e8 378=head2 initialize ClassName -> Mouse::Meta::Class
c3398f5b 379
306290e8 380Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only
381one instance should exist for a given class.
c3398f5b 382
306290e8 383=head2 new %args -> Mouse::Meta::Class
c3398f5b 384
306290e8 385Creates a new Mouse::Meta::Class. Don't call this directly.
c3398f5b 386
387=head2 name -> ClassName
388
389Returns the name of the owner class.
390
391=head2 superclasses -> [ClassName]
392
393Gets (or sets) the list of superclasses of the owner class.
394
60f6eba9 395=head2 add_attribute (Mouse::Meta::Attribute| name => spec)
c3398f5b 396
306290e8 397Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
398class.
c3398f5b 399
d60824af 400=head2 get_all_attributes -> (Mouse::Meta::Attribute)
72b88a88 401
402Returns the list of all L<Mouse::Meta::Attribute> instances associated with
403this class and its superclasses.
404
306290e8 405=head2 get_attribute_map -> { name => Mouse::Meta::Attribute }
c3398f5b 406
407Returns a mapping of attribute names to their corresponding
306290e8 408L<Mouse::Meta::Attribute> objects.
c3398f5b 409
c68b4110 410=head2 get_attribute_list -> { name => Mouse::Meta::Attribute }
411
412This returns a list of attribute names which are defined in the local
413class. If you want a list of all applicable attributes for a class,
d60824af 414use the C<get_all_attributes> method.
c68b4110 415
cbc437f2 416=head2 has_attribute Name -> Bool
66eea168 417
418Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
419
306290e8 420=head2 get_attribute Name -> Mouse::Meta::Attribute | undef
c3398f5b 421
306290e8 422Returns the L<Mouse::Meta::Attribute> with the given name.
c3398f5b 423
424=head2 linearized_isa -> [ClassNames]
425
426Returns the list of classes in method dispatch order, with duplicates removed.
427
f7b11a21 428=head2 clone_object Instance -> Instance
429
430Clones the given C<Instance> which must be an instance governed by this
431metaclass.
432
433=head2 clone_instance Instance, Parameters -> Instance
434
518e303a 435The clone_instance method has been made private.
436The public version is deprecated.
f7b11a21 437
c3398f5b 438=cut
439