2 package Moose::Meta::Class;
8 use Scalar::Util 'weaken', 'blessed';
10 our $VERSION = '0.04';
12 use base 'Class::MOP::Class';
14 sub construct_instance {
15 my ($class, %params) = @_;
16 my $instance = $params{'__INSTANCE__'} || {};
17 foreach my $attr ($class->compute_all_applicable_attributes()) {
18 my $init_arg = $attr->init_arg();
19 # try to fetch the init arg from the %params ...
21 if (exists $params{$init_arg}) {
22 $val = $params{$init_arg};
25 # skip it if it's lazy
26 next if $attr->is_lazy;
27 # and die if it is required
28 confess "Attribute (" . $attr->name . ") is required"
31 # if nothing was in the %params, we can use the
32 # attribute's default value (if it has one)
33 if (!defined $val && $attr->has_default) {
34 $val = $attr->default($instance);
37 if ($attr->has_type_constraint) {
38 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
39 $val = $attr->type_constraint->coercion->coerce($val);
41 (defined($attr->type_constraint->check($val)))
42 || confess "Attribute (" . $attr->name . ") does not pass the type contraint with '$val'";
45 $instance->{$attr->name} = $val;
46 if (defined $val && $attr->is_weak_ref) {
47 weaken($instance->{$attr->name});
54 my ($self, $method_name) = @_;
55 (defined $method_name && $method_name)
56 || confess "You must define a method name";
58 my $sub_name = ($self->name . '::' . $method_name);
61 return 0 if !defined(&{$sub_name});
62 my $method = \&{$sub_name};
64 return 1 if blessed($method) && $method->isa('Moose::Meta::Role::Method');
65 return $self->SUPER::has_method($method_name);
69 sub add_override_method_modifier {
70 my ($self, $name, $method, $_super_package) = @_;
71 # need this for roles ...
72 $_super_package ||= $self->name;
73 my $super = $self->find_next_method_by_name($name);
75 || confess "You cannot override '$name' because it has no super method";
76 $self->add_method($name => bless sub {
79 no warnings 'redefine';
80 local *{$_super_package . '::super'} = sub { $super->(@args) };
81 return $method->(@args);
82 } => 'Moose::Meta::Method::Overriden');
85 sub add_augment_method_modifier {
86 my ($self, $name, $method) = @_;
87 my $super = $self->find_next_method_by_name($name);
89 || confess "You cannot augment '$name' because it has no super method";
90 my $_super_package = $super->package_name;
91 # BUT!,... if this is an overriden method ....
92 if ($super->isa('Moose::Meta::Method::Overriden')) {
93 # we need to be sure that we actually
94 # find the next method, which is not
95 # an 'override' method, the reason is
96 # that an 'override' method will not
97 # be the one calling inner()
98 my $real_super = $self->_find_next_method_by_name_which_is_not_overridden($name);
99 $_super_package = $real_super->package_name;
101 $self->add_method($name => sub {
104 no warnings 'redefine';
105 local *{$_super_package . '::inner'} = sub { $method->(@args) };
106 return $super->(@args);
110 sub _find_next_method_by_name_which_is_not_overridden {
111 my ($self, $name) = @_;
112 my @methods = $self->find_all_methods_by_name($name);
113 foreach my $method (@methods) {
114 return $method->{code}
115 if blessed($method->{code}) && !$method->{code}->isa('Moose::Meta::Method::Overriden');
120 package Moose::Meta::Method::Overriden;
125 our $VERSION = '0.01';
127 use base 'Class::MOP::Method';
137 Moose::Meta::Class - The Moose metaclass
141 This is a subclass of L<Class::MOP::Class> with Moose specific
144 For the most part, the only time you will ever encounter an
145 instance of this class is if you are doing some serious deep
146 introspection. To really understand this class, you need to refer
147 to the L<Class::MOP::Class> documentation.
153 =item B<construct_instance>
155 This provides some Moose specific extensions to this method, you
156 almost never call this method directly unless you really know what
159 This method makes sure to handle the moose weak-ref, type-constraint
160 and type coercion features.
162 =item B<has_method ($name)>
164 This accomidates Moose::Meta::Role::Method instances, which are
165 aliased, instead of added, but still need to be counted as valid
168 =item B<add_override_method_modifier ($name, $method)>
170 =item B<add_augment_method_modifier ($name, $method)>
176 All complex software has bugs lurking in it, and this module is no
177 exception. If you find a bug please either email me, or add the bug
182 Stevan Little E<lt>stevan@iinteractive.comE<gt>
184 =head1 COPYRIGHT AND LICENSE
186 Copyright 2006 by Infinity Interactive, Inc.
188 L<http://www.iinteractive.com>
190 This library is free software; you can redistribute it and/or modify
191 it under the same terms as Perl itself.