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