Resolve a todo (method confliction)
[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
8536d351 17sub _new {
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
32 return bless \%args, $class;
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
54 @{ $self->{superclasses} };
55}
56
60cfc6ad 57sub get_all_method_names {
58 my $self = shift;
59 my %uniq;
60 return grep { $uniq{$_}++ == 0 }
3a63a2e7 61 map { Mouse::Meta::Class->initialize($_)->get_method_list() }
60cfc6ad 62 $self->linearized_isa;
63}
64
c3398f5b 65sub add_attribute {
66 my $self = shift;
c3398f5b 67
60f6eba9 68 if (@_ == 1 && blessed($_[0])) {
69 my $attr = shift @_;
70 $self->{'attributes'}{$attr->name} = $attr;
7a50b450 71 }
72 else {
60f6eba9 73 my $names = shift @_;
74 $names = [$names] if !ref($names);
75 my $metaclass = 'Mouse::Meta::Attribute';
7a50b450 76 my %options = (@_ == 1 ? %{$_[0]} : @_);
60f6eba9 77
78 if ( my $metaclass_name = delete $options{metaclass} ) {
79 my $new_class = Mouse::Util::resolve_metaclass_alias(
80 'Attribute',
81 $metaclass_name
82 );
83 if ( $metaclass ne $new_class ) {
84 $metaclass = $new_class;
85 }
86 }
87
88 for my $name (@$names) {
89 if ($name =~ s/^\+//) {
7a50b450 90 $metaclass->clone_parent($self, $name, %options);
60f6eba9 91 }
92 else {
7a50b450 93 $metaclass->create($self, $name, %options);
60f6eba9 94 }
95 }
96 }
c3398f5b 97}
98
d60824af 99sub compute_all_applicable_attributes { shift->get_all_attributes(@_) }
100sub get_all_attributes {
72b88a88 101 my $self = shift;
102 my (@attr, %seen);
103
104 for my $class ($self->linearized_isa) {
105 my $meta = $self->_metaclass_cache($class)
106 or next;
107
108 for my $name (keys %{ $meta->get_attribute_map }) {
109 next if $seen{$name}++;
110 push @attr, $meta->get_attribute($name);
111 }
112 }
113
114 return @attr;
115}
116
8536d351 117sub linearized_isa { @{ get_linear_isa($_[0]->name) } }
118
119sub new_object {
c68b4110 120 my $self = shift;
7a50b450 121 my %args = (@_ == 1 ? %{$_[0]} : @_);
c3398f5b 122
fce211ae 123 my $instance = bless {}, $self->name;
124
53c495ce 125 my @triggers_queue;
126
fce211ae 127 foreach my $attribute ($self->get_all_attributes) {
8536d351 128 my $from = $attribute->init_arg;
129 my $key = $attribute->name;
130
7a50b450 131 if (defined($from) && exists($args{$from})) {
132 $args{$from} = $attribute->coerce_constraint($args{$from})
8536d351 133 if $attribute->should_coerce;
7a50b450 134 $attribute->verify_against_type_constraint($args{$from});
8536d351 135
7a50b450 136 $instance->{$key} = $args{$from};
8536d351 137
138 weaken($instance->{$key})
fce211ae 139 if ref($instance->{$key}) && $attribute->is_weak_ref;
8536d351 140
141 if ($attribute->has_trigger) {
53c495ce 142 push @triggers_queue, [ $attribute->trigger, $args{$from} ];
8536d351 143 }
144 }
145 else {
146 if ($attribute->has_default || $attribute->has_builder) {
147 unless ($attribute->is_lazy) {
148 my $default = $attribute->default;
149 my $builder = $attribute->builder;
150 my $value = $attribute->has_builder
151 ? $instance->$builder
152 : ref($default) eq 'CODE'
153 ? $default->($instance)
154 : $default;
155
156 $value = $attribute->coerce_constraint($value)
157 if $attribute->should_coerce;
158 $attribute->verify_against_type_constraint($value);
159
160 $instance->{$key} = $value;
161
162 weaken($instance->{$key})
fce211ae 163 if ref($instance->{$key}) && $attribute->is_weak_ref;
8536d351 164 }
165 }
166 else {
167 if ($attribute->is_required) {
fce211ae 168 $self->throw_error("Attribute (".$attribute->name.") is required");
8536d351 169 }
170 }
171 }
172 }
53c495ce 173
174 foreach my $trigger_and_value(@triggers_queue){
175 my($trigger, $value) = @{$trigger_and_value};
176 $trigger->($instance, $value);
177 }
178
8536d351 179 return $instance;
180}
c3398f5b 181
7a59f4e8 182sub clone_object {
183 my $class = shift;
184 my $instance = shift;
185
186 (blessed($instance) && $instance->isa($class->name))
fce211ae 187 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($instance)");
7a59f4e8 188
189 $class->clone_instance($instance, @_);
190}
191
192sub clone_instance {
193 my ($class, $instance, %params) = @_;
194
195 (blessed($instance))
fce211ae 196 || $class->throw_error("You can only clone instances, ($instance) is not a blessed instance");
7a59f4e8 197
198 my $clone = bless { %$instance }, ref $instance;
199
d60824af 200 foreach my $attr ($class->get_all_attributes()) {
7a59f4e8 201 if ( defined( my $init_arg = $attr->init_arg ) ) {
202 if (exists $params{$init_arg}) {
203 $clone->{ $attr->name } = $params{$init_arg};
204 }
205 }
206 }
207
208 return $clone;
209
210}
211
fc1d8369 212sub make_immutable {
213 my $self = shift;
6a1d1835 214 my %args = (
215 inline_constructor => 1,
e578d610 216 inline_destructor => 1,
6a1d1835 217 @_,
218 );
219
fc1d8369 220 $self->{is_immutable}++;
c7a6403f 221
6a1d1835 222 if ($args{inline_constructor}) {
c7a6403f 223 $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self ));
224 }
225
8632b6fe 226 if ($args{inline_destructor}) {
227 $self->add_method('DESTROY' => Mouse::Meta::Method::Destructor->generate_destructor_method_inline( $self ));
228 }
2276cb14 229
230 # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
231 # at the end of a source file.
232 return 1;
fc1d8369 233}
ad958001 234
fce211ae 235sub make_mutable { not_supported }
ad958001 236
6cfa1e5e 237sub is_immutable { $_[0]->{is_immutable} }
238sub is_mutable { !$_[0]->{is_immutable} }
84ef660f 239
4859d490 240sub _install_modifier {
241 my ( $self, $into, $type, $name, $code ) = @_;
4f5b44a0 242
243 # which is modifer class available?
244 my $modifier_class = do {
245 if (eval "require Class::Method::Modifiers::Fast; 1") {
246 'Class::Method::Modifiers::Fast';
247 } elsif (eval "require Class::Method::Modifiers; 1") {
248 'Class::Method::Modifiers';
249 } else {
250 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.");
251 }
252 };
253 my $modifier = $modifier_class->can('_install_modifier');
254
255 # replace this method itself :)
256 {
4f5b44a0 257 no warnings 'redefine';
4f9945f5 258 *_install_modifier = sub {
4f5b44a0 259 my ( $self, $into, $type, $name, $code ) = @_;
260 $modifier->(
261 $into,
262 $type,
263 $name,
264 $code
265 );
6cfa1e5e 266 $self->{methods}{$name}++; # register it to the method map
267 return;
4f5b44a0 268 };
1b79a118 269 }
4f5b44a0 270
271 # call me. for first time.
272 $self->_install_modifier( $into, $type, $name, $code );
4859d490 273}
274
50dc6ee5 275sub add_before_method_modifier {
4859d490 276 my ( $self, $name, $code ) = @_;
277 $self->_install_modifier( $self->name, 'before', $name, $code );
50dc6ee5 278}
279
280sub add_around_method_modifier {
4859d490 281 my ( $self, $name, $code ) = @_;
282 $self->_install_modifier( $self->name, 'around', $name, $code );
50dc6ee5 283}
284
285sub add_after_method_modifier {
4859d490 286 my ( $self, $name, $code ) = @_;
287 $self->_install_modifier( $self->name, 'after', $name, $code );
50dc6ee5 288}
289
67199842 290sub add_override_method_modifier {
291 my ($self, $name, $code) = @_;
292
6cfa1e5e 293 my $package = $self->name;
67199842 294
6cfa1e5e 295 my $body = $package->can($name)
296 or $self->throw_error("You cannot override '$name' because it has no super method");
67199842 297
6cfa1e5e 298 $self->add_method($name => sub { $code->($package, $body, @_) });
67199842 299}
300
47f36c05 301sub does_role {
302 my ($self, $role_name) = @_;
ad958001 303
47f36c05 304 (defined $role_name)
fce211ae 305 || $self->throw_error("You must supply a role name to look for");
ad958001 306
f7fec86c 307 for my $class ($self->linearized_isa) {
08f7a8db 308 my $meta = Mouse::Meta::Module::class_of($class);
3a63a2e7 309 next unless $meta && $meta->can('roles');
310
311 for my $role (@{ $meta->roles }) {
ff687069 312
3a63a2e7 313 return 1 if $role->does_role($role_name);
f7fec86c 314 }
47f36c05 315 }
ad958001 316
47f36c05 317 return 0;
318}
319
c3398f5b 3201;
321
322__END__
323
324=head1 NAME
325
306290e8 326Mouse::Meta::Class - hook into the Mouse MOP
c3398f5b 327
328=head1 METHODS
329
306290e8 330=head2 initialize ClassName -> Mouse::Meta::Class
c3398f5b 331
306290e8 332Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only
333one instance should exist for a given class.
c3398f5b 334
306290e8 335=head2 new %args -> Mouse::Meta::Class
c3398f5b 336
306290e8 337Creates a new Mouse::Meta::Class. Don't call this directly.
c3398f5b 338
339=head2 name -> ClassName
340
341Returns the name of the owner class.
342
343=head2 superclasses -> [ClassName]
344
345Gets (or sets) the list of superclasses of the owner class.
346
60f6eba9 347=head2 add_attribute (Mouse::Meta::Attribute| name => spec)
c3398f5b 348
306290e8 349Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
350class.
c3398f5b 351
d60824af 352=head2 get_all_attributes -> (Mouse::Meta::Attribute)
72b88a88 353
354Returns the list of all L<Mouse::Meta::Attribute> instances associated with
355this class and its superclasses.
356
306290e8 357=head2 get_attribute_map -> { name => Mouse::Meta::Attribute }
c3398f5b 358
359Returns a mapping of attribute names to their corresponding
306290e8 360L<Mouse::Meta::Attribute> objects.
c3398f5b 361
c68b4110 362=head2 get_attribute_list -> { name => Mouse::Meta::Attribute }
363
364This returns a list of attribute names which are defined in the local
365class. If you want a list of all applicable attributes for a class,
d60824af 366use the C<get_all_attributes> method.
c68b4110 367
cbc437f2 368=head2 has_attribute Name -> Bool
66eea168 369
370Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
371
306290e8 372=head2 get_attribute Name -> Mouse::Meta::Attribute | undef
c3398f5b 373
306290e8 374Returns the L<Mouse::Meta::Attribute> with the given name.
c3398f5b 375
376=head2 linearized_isa -> [ClassNames]
377
378Returns the list of classes in method dispatch order, with duplicates removed.
379
f7b11a21 380=head2 clone_object Instance -> Instance
381
382Clones the given C<Instance> which must be an instance governed by this
383metaclass.
384
385=head2 clone_instance Instance, Parameters -> Instance
386
518e303a 387The clone_instance method has been made private.
388The public version is deprecated.
f7b11a21 389
c3398f5b 390=cut
391