2 package Moose::Meta::Class;
10 use Scalar::Util 'weaken', 'blessed';
12 our $VERSION = '0.05';
14 use base 'Class::MOP::Class';
16 __PACKAGE__->meta->add_attribute('roles' => (
24 $class->SUPER::initialize($pkg,
25 ':attribute_metaclass' => 'Moose::Meta::Attribute',
30 my ($self, $role) = @_;
31 (blessed($role) && $role->isa('Moose::Meta::Role'))
32 || confess "Roles must be instances of Moose::Meta::Role";
33 push @{$self->roles} => $role;
37 my ($self, $role_name) = @_;
39 || confess "You must supply a role name to look for";
40 foreach my $role (@{$self->roles}) {
41 return 1 if $role->does_role($role_name);
47 my ($class, %params) = @_;
48 my $self = $class->SUPER::new_object(%params);
49 foreach my $attr ($class->compute_all_applicable_attributes()) {
50 next unless $params{$attr->init_arg} && $attr->can('has_trigger') && $attr->has_trigger;
51 $attr->trigger->($self, $params{$attr->init_arg}, $attr);
56 sub construct_instance {
57 my ($class, %params) = @_;
58 my $instance = $params{'__INSTANCE__'} || {};
59 foreach my $attr ($class->compute_all_applicable_attributes()) {
60 $attr->initialize_instance_slot($class, $instance, \%params)
66 my ($self, $method_name) = @_;
67 (defined $method_name && $method_name)
68 || confess "You must define a method name";
70 my $sub_name = ($self->name . '::' . $method_name);
73 return 0 if !defined(&{$sub_name});
74 my $method = \&{$sub_name};
76 return 1 if blessed($method) && $method->isa('Moose::Meta::Role::Method');
77 return $self->SUPER::has_method($method_name);
81 sub add_override_method_modifier {
82 my ($self, $name, $method, $_super_package) = @_;
83 # need this for roles ...
84 $_super_package ||= $self->name;
85 my $super = $self->find_next_method_by_name($name);
87 || confess "You cannot override '$name' because it has no super method";
88 $self->add_method($name => bless sub {
91 no warnings 'redefine';
92 local *{$_super_package . '::super'} = sub { $super->(@args) };
93 return $method->(@args);
94 } => 'Moose::Meta::Method::Overriden');
97 sub add_augment_method_modifier {
98 my ($self, $name, $method) = @_;
99 my $super = $self->find_next_method_by_name($name);
101 || confess "You cannot augment '$name' because it has no super method";
102 my $_super_package = $super->package_name;
103 # BUT!,... if this is an overriden method ....
104 if ($super->isa('Moose::Meta::Method::Overriden')) {
105 # we need to be sure that we actually
106 # find the next method, which is not
107 # an 'override' method, the reason is
108 # that an 'override' method will not
109 # be the one calling inner()
110 my $real_super = $self->_find_next_method_by_name_which_is_not_overridden($name);
111 $_super_package = $real_super->package_name;
113 $self->add_method($name => sub {
116 no warnings 'redefine';
117 local *{$_super_package . '::inner'} = sub { $method->(@args) };
118 return $super->(@args);
122 sub _find_next_method_by_name_which_is_not_overridden {
123 my ($self, $name) = @_;
124 my @methods = $self->find_all_methods_by_name($name);
125 foreach my $method (@methods) {
126 return $method->{code}
127 if blessed($method->{code}) && !$method->{code}->isa('Moose::Meta::Method::Overriden');
132 package Moose::Meta::Method::Overriden;
137 our $VERSION = '0.01';
139 use base 'Class::MOP::Method';
149 Moose::Meta::Class - The Moose metaclass
153 This is a subclass of L<Class::MOP::Class> with Moose specific
156 For the most part, the only time you will ever encounter an
157 instance of this class is if you are doing some serious deep
158 introspection. To really understand this class, you need to refer
159 to the L<Class::MOP::Class> documentation.
169 We override this method to support the C<trigger> attribute option.
171 =item B<construct_instance>
173 This provides some Moose specific extensions to this method, you
174 almost never call this method directly unless you really know what
177 This method makes sure to handle the moose weak-ref, type-constraint
178 and type coercion features.
180 =item B<has_method ($name)>
182 This accomidates Moose::Meta::Role::Method instances, which are
183 aliased, instead of added, but still need to be counted as valid
186 =item B<add_override_method_modifier ($name, $method)>
188 This will create an C<override> method modifier for you, and install
191 =item B<add_augment_method_modifier ($name, $method)>
193 This will create an C<augment> method modifier for you, and install
198 This will return an array of C<Moose::Meta::Role> instances which are
199 attached to this class.
201 =item B<add_role ($role)>
203 This takes an instance of C<Moose::Meta::Role> in C<$role>, and adds it
204 to the list of associated roles.
206 =item B<does_role ($role_name)>
208 This will test if this class C<does> a given C<$role_name>. It will
209 not only check it's local roles, but ask them as well in order to
210 cascade down the role hierarchy.
216 All complex software has bugs lurking in it, and this module is no
217 exception. If you find a bug please either email me, or add the bug
222 Stevan Little E<lt>stevan@iinteractive.comE<gt>
224 =head1 COPYRIGHT AND LICENSE
226 Copyright 2006 by Infinity Interactive, Inc.
228 L<http://www.iinteractive.com>
230 This library is free software; you can redistribute it and/or modify
231 it under the same terms as Perl itself.