2 package Moose::Meta::Class;
10 use Scalar::Util 'weaken', 'blessed', 'reftype';
12 our $VERSION = '0.07';
14 use base 'Class::MOP::Class';
16 __PACKAGE__->meta->add_attribute('roles' => (
24 $class->SUPER::initialize($pkg,
25 ':attribute_metaclass' => 'Moose::Meta::Attribute',
26 ':instance_metaclass' => 'Moose::Meta::Instance',
31 my ($self, $role) = @_;
32 (blessed($role) && $role->isa('Moose::Meta::Role'))
33 || confess "Roles must be instances of Moose::Meta::Role";
34 push @{$self->roles} => $role;
37 sub calculate_all_roles {
40 grep { !$seen{$_->name}++ } map { $_->calculate_all_roles } @{ $self->roles };
44 my ($self, $role_name) = @_;
46 || confess "You must supply a role name to look for";
47 foreach my $class ($self->class_precedence_list) {
48 next unless $class->can('meta');
49 foreach my $role (@{$class->meta->roles}) {
50 return 1 if $role->does_role($role_name);
57 my ($self, $role_name) = @_;
59 || confess "You must supply a role name to look for";
60 foreach my $class ($self->class_precedence_list) {
61 next unless $class->can('meta');
62 foreach my $role (@{$class->meta->roles}) {
63 return 1 if $role->excludes_role($role_name);
70 my ($class, %params) = @_;
71 my $self = $class->SUPER::new_object(%params);
72 foreach my $attr ($class->compute_all_applicable_attributes()) {
74 # this does not accept undefined
75 # values, nor does it accept false
76 # values to be passed into the init-arg
77 next unless $params{$attr->init_arg} && $attr->can('has_trigger') && $attr->has_trigger;
78 $attr->trigger->($self, $params{$attr->init_arg}, $attr);
83 sub construct_instance {
84 my ($class, %params) = @_;
85 my $meta_instance = $class->get_meta_instance;
87 # the code below is almost certainly incorrect
88 # but this is foreign inheritence, so we might
89 # have to kludge it in the end.
90 my $instance = $params{'__INSTANCE__'} || $meta_instance->create_instance();
91 foreach my $attr ($class->compute_all_applicable_attributes()) {
92 $attr->initialize_instance_slot($meta_instance, $instance, \%params)
98 my ($self, $method_name) = @_;
99 (defined $method_name && $method_name)
100 || confess "You must define a method name";
102 my $sub_name = ($self->name . '::' . $method_name);
105 # this should use the ::Package code
106 # and not turn off strict refs
108 return 0 if !defined(&{$sub_name});
109 my $method = \&{$sub_name};
111 return 1 if blessed($method) && $method->isa('Moose::Meta::Role::Method');
112 return $self->SUPER::has_method($method_name);
118 if (scalar @_ == 1 && ref($_[0]) eq 'HASH') {
120 # if it is a HASH ref, we de-ref it.
121 # this will usually mean that it is
123 $self->SUPER::add_attribute($name => %{$_[0]});
126 # otherwise we just pass the args
127 $self->SUPER::add_attribute($name => @_);
131 sub add_override_method_modifier {
132 my ($self, $name, $method, $_super_package) = @_;
133 (!$self->has_method($name))
134 || confess "Cannot add an override method if a local method is already present";
135 # need this for roles ...
136 $_super_package ||= $self->name;
137 my $super = $self->find_next_method_by_name($name);
139 || confess "You cannot override '$name' because it has no super method";
140 $self->add_method($name => bless sub {
143 no warnings 'redefine';
144 local *{$_super_package . '::super'} = sub { $super->(@args) };
145 return $method->(@args);
146 } => 'Moose::Meta::Method::Overriden');
149 sub add_augment_method_modifier {
150 my ($self, $name, $method) = @_;
151 (!$self->has_method($name))
152 || confess "Cannot add an augment method if a local method is already present";
153 my $super = $self->find_next_method_by_name($name);
155 || confess "You cannot augment '$name' because it has no super method";
156 my $_super_package = $super->package_name;
157 # BUT!,... if this is an overriden method ....
158 if ($super->isa('Moose::Meta::Method::Overriden')) {
159 # we need to be sure that we actually
160 # find the next method, which is not
161 # an 'override' method, the reason is
162 # that an 'override' method will not
163 # be the one calling inner()
164 my $real_super = $self->_find_next_method_by_name_which_is_not_overridden($name);
165 $_super_package = $real_super->package_name;
167 $self->add_method($name => sub {
170 no warnings 'redefine';
171 local *{$_super_package . '::inner'} = sub { $method->(@args) };
172 return $super->(@args);
176 ## Private Utility methods ...
178 sub _find_next_method_by_name_which_is_not_overridden {
179 my ($self, $name) = @_;
180 foreach my $method ($self->find_all_methods_by_name($name)) {
181 return $method->{code}
182 if blessed($method->{code}) && !$method->{code}->isa('Moose::Meta::Method::Overriden');
187 sub _fix_metaclass_incompatability {
188 my ($self, @superclasses) = @_;
189 foreach my $super (@superclasses) {
190 # don't bother if it does not have a meta.
191 next unless $super->can('meta');
192 # if it's meta is a vanilla Moose,
193 # then we can safely ignore it.
194 next if blessed($super->meta) eq 'Moose::Meta::Class';
195 # but if we have anything else,
196 # we need to check it out ...
197 unless (# see if of our metaclass is incompatible
198 ($self->isa(blessed($super->meta)) &&
199 # and see if our instance metaclass is incompatible
200 $self->instance_metaclass->isa($super->meta->instance_metaclass)) &&
201 # ... and if we are just a vanilla Moose
202 $self->isa('Moose::Meta::Class')) {
203 # re-initialize the meta ...
204 my $super_meta = $super->meta;
206 # We might want to consider actually
207 # transfering any attributes from the
208 # original meta into this one, but in
209 # general you should not have any there
210 # at this point anyway, so it's very
211 # much an obscure edge case anyway
212 $self = $super_meta->reinitialize($self->name => (
213 ':attribute_metaclass' => $super_meta->attribute_metaclass,
214 ':method_metaclass' => $super_meta->method_metaclass,
215 ':instance_metaclass' => $super_meta->instance_metaclass,
222 sub _apply_all_roles {
223 my ($self, @roles) = @_;
224 ($_->can('meta') && $_->meta->isa('Moose::Meta::Role'))
225 || confess "You can only consume roles, $_ is not a Moose role"
227 if (scalar @roles == 1) {
228 $roles[0]->meta->apply($self);
232 # we should make a Moose::Meta::Role::Composite
233 # which is a smaller version of Moose::Meta::Role
234 # which does not use any package stuff
235 Moose::Meta::Role->combine(
236 map { $_->meta } @roles
241 sub _process_attribute {
242 my ($self, $name, %options) = @_;
243 if ($name =~ /^\+(.*)/) {
244 my $new_attr = $self->_process_inherited_attribute($1, %options);
245 $self->add_attribute($new_attr);
248 if ($options{metaclass}) {
249 Moose::_load_all_classes($options{metaclass});
250 $self->add_attribute($options{metaclass}->new($name, %options));
253 $self->add_attribute($name, %options);
258 sub _process_inherited_attribute {
259 my ($self, $attr_name, %options) = @_;
260 my $inherited_attr = $self->find_attribute_by_name($attr_name);
261 (defined $inherited_attr)
262 || confess "Could not find an attribute by the name of '$attr_name' to inherit from";
264 if ($inherited_attr->isa('Moose::Meta::Attribute')) {
265 $new_attr = $inherited_attr->clone_and_inherit_options(%options);
269 # kind of a kludge to handle Class::MOP::Attributes
270 $new_attr = Moose::Meta::Attribute::clone_and_inherit_options(
271 $inherited_attr, %options
277 package Moose::Meta::Method::Overriden;
282 our $VERSION = '0.01';
284 use base 'Class::MOP::Method';
294 Moose::Meta::Class - The Moose metaclass
298 This is a subclass of L<Class::MOP::Class> with Moose specific
301 For the most part, the only time you will ever encounter an
302 instance of this class is if you are doing some serious deep
303 introspection. To really understand this class, you need to refer
304 to the L<Class::MOP::Class> documentation.
314 We override this method to support the C<trigger> attribute option.
316 =item B<construct_instance>
318 This provides some Moose specific extensions to this method, you
319 almost never call this method directly unless you really know what
322 This method makes sure to handle the moose weak-ref, type-constraint
323 and type coercion features.
325 =item B<has_method ($name)>
327 This accommodates Moose::Meta::Role::Method instances, which are
328 aliased, instead of added, but still need to be counted as valid
331 =item B<add_override_method_modifier ($name, $method)>
333 This will create an C<override> method modifier for you, and install
336 =item B<add_augment_method_modifier ($name, $method)>
338 This will create an C<augment> method modifier for you, and install
341 =item B<calculate_all_roles>
345 This will return an array of C<Moose::Meta::Role> instances which are
346 attached to this class.
348 =item B<add_role ($role)>
350 This takes an instance of C<Moose::Meta::Role> in C<$role>, and adds it
351 to the list of associated roles.
353 =item B<does_role ($role_name)>
355 This will test if this class C<does> a given C<$role_name>. It will
356 not only check it's local roles, but ask them as well in order to
357 cascade down the role hierarchy.
359 =item B<excludes_role ($role_name)>
361 This will test if this class C<excludes> a given C<$role_name>. It will
362 not only check it's local roles, but ask them as well in order to
363 cascade down the role hierarchy.
365 =item B<add_attribute ($attr_name, %params|$params)>
367 This method does the same thing as L<Class::MOP::Class::add_attribute>, but adds
368 support for taking the C<$params> as a HASH ref.
374 All complex software has bugs lurking in it, and this module is no
375 exception. If you find a bug please either email me, or add the bug
380 Stevan Little E<lt>stevan@iinteractive.comE<gt>
382 =head1 COPYRIGHT AND LICENSE
384 Copyright 2006 by Infinity Interactive, Inc.
386 L<http://www.iinteractive.com>
388 This library is free software; you can redistribute it and/or modify
389 it under the same terms as Perl itself.