2 package Moose::Meta::Class;
10 use Scalar::Util 'weaken', 'blessed', 'reftype';
12 our $VERSION = '0.06';
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;
38 my ($self, $role_name) = @_;
40 || confess "You must supply a role name to look for";
41 foreach my $role (@{$self->roles}) {
42 return 1 if $role->does_role($role_name);
48 my ($class, %params) = @_;
49 my $self = $class->SUPER::new_object(%params);
50 foreach my $attr ($class->compute_all_applicable_attributes()) {
51 next unless $params{$attr->init_arg} && $attr->can('has_trigger') && $attr->has_trigger;
52 $attr->trigger->($self, $params{$attr->init_arg}, $attr);
57 sub construct_instance {
58 my ($class, %params) = @_;
59 my $meta_instance = $class->get_meta_instance;
61 # the code below is almost certainly incorrect
62 # but this is foreign inheritence, so we might
63 # have to kludge it in the end.
64 my $instance = $params{'__INSTANCE__'} || $meta_instance->create_instance();
65 foreach my $attr ($class->compute_all_applicable_attributes()) {
66 $attr->initialize_instance_slot($meta_instance, $instance, \%params)
72 my ($self, $method_name) = @_;
73 (defined $method_name && $method_name)
74 || confess "You must define a method name";
76 my $sub_name = ($self->name . '::' . $method_name);
79 return 0 if !defined(&{$sub_name});
80 my $method = \&{$sub_name};
82 return 1 if blessed($method) && $method->isa('Moose::Meta::Role::Method');
83 return $self->SUPER::has_method($method_name);
86 sub add_override_method_modifier {
87 my ($self, $name, $method, $_super_package) = @_;
88 # need this for roles ...
89 $_super_package ||= $self->name;
90 my $super = $self->find_next_method_by_name($name);
92 || confess "You cannot override '$name' because it has no super method";
93 $self->add_method($name => bless sub {
96 no warnings 'redefine';
97 local *{$_super_package . '::super'} = sub { $super->(@args) };
98 return $method->(@args);
99 } => 'Moose::Meta::Method::Overriden');
102 sub add_augment_method_modifier {
103 my ($self, $name, $method) = @_;
104 my $super = $self->find_next_method_by_name($name);
106 || confess "You cannot augment '$name' because it has no super method";
107 my $_super_package = $super->package_name;
108 # BUT!,... if this is an overriden method ....
109 if ($super->isa('Moose::Meta::Method::Overriden')) {
110 # we need to be sure that we actually
111 # find the next method, which is not
112 # an 'override' method, the reason is
113 # that an 'override' method will not
114 # be the one calling inner()
115 my $real_super = $self->_find_next_method_by_name_which_is_not_overridden($name);
116 $_super_package = $real_super->package_name;
118 $self->add_method($name => sub {
121 no warnings 'redefine';
122 local *{$_super_package . '::inner'} = sub { $method->(@args) };
123 return $super->(@args);
127 sub _find_next_method_by_name_which_is_not_overridden {
128 my ($self, $name) = @_;
129 my @methods = $self->find_all_methods_by_name($name);
130 foreach my $method (@methods) {
131 return $method->{code}
132 if blessed($method->{code}) && !$method->{code}->isa('Moose::Meta::Method::Overriden');
137 package Moose::Meta::Method::Overriden;
142 our $VERSION = '0.01';
144 use base 'Class::MOP::Method';
154 Moose::Meta::Class - The Moose metaclass
158 This is a subclass of L<Class::MOP::Class> with Moose specific
161 For the most part, the only time you will ever encounter an
162 instance of this class is if you are doing some serious deep
163 introspection. To really understand this class, you need to refer
164 to the L<Class::MOP::Class> documentation.
174 We override this method to support the C<trigger> attribute option.
176 =item B<construct_instance>
178 This provides some Moose specific extensions to this method, you
179 almost never call this method directly unless you really know what
182 This method makes sure to handle the moose weak-ref, type-constraint
183 and type coercion features.
185 =item B<has_method ($name)>
187 This accomidates Moose::Meta::Role::Method instances, which are
188 aliased, instead of added, but still need to be counted as valid
191 =item B<add_override_method_modifier ($name, $method)>
193 This will create an C<override> method modifier for you, and install
196 =item B<add_augment_method_modifier ($name, $method)>
198 This will create an C<augment> method modifier for you, and install
203 This will return an array of C<Moose::Meta::Role> instances which are
204 attached to this class.
206 =item B<add_role ($role)>
208 This takes an instance of C<Moose::Meta::Role> in C<$role>, and adds it
209 to the list of associated roles.
211 =item B<does_role ($role_name)>
213 This will test if this class C<does> a given C<$role_name>. It will
214 not only check it's local roles, but ask them as well in order to
215 cascade down the role hierarchy.
217 =item B<add_attribute $attr_name, %params>
219 This method does the same thing as L<Class::MOP::Class/add_attribute>, but adds
220 suport for delegation.
224 =head1 INTERNAL METHODS
228 =item compute_delegation
230 =item generate_delegation_list
232 =item generate_delgate_method
234 =item get_delegatable_methods
236 =item filter_delegations
242 All complex software has bugs lurking in it, and this module is no
243 exception. If you find a bug please either email me, or add the bug
248 Stevan Little E<lt>stevan@iinteractive.comE<gt>
250 =head1 COPYRIGHT AND LICENSE
252 Copyright 2006 by Infinity Interactive, Inc.
254 L<http://www.iinteractive.com>
256 This library is free software; you can redistribute it and/or modify
257 it under the same terms as Perl itself.