ROLES
[gitmo/Moose.git] / lib / Moose / Meta / Class.pm
CommitLineData
c0e30cf5 1
2package Moose::Meta::Class;
3
4use strict;
5use warnings;
6
6ba6d68c 7use Carp 'confess';
8use Scalar::Util 'weaken';
a15dff8d 9
78cd1d3b 10our $VERSION = '0.04';
bc1e29b5 11
c0e30cf5 12use base 'Class::MOP::Class';
13
a15dff8d 14sub construct_instance {
15 my ($class, %params) = @_;
e522431d 16 my $instance = $params{'__INSTANCE__'} || {};
a15dff8d 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 ...
20 my $val;
ca01a97b 21 if (exists $params{$init_arg}) {
22 $val = $params{$init_arg};
23 }
24 else {
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"
29 if $attr->is_required
30 }
a15dff8d 31 # if nothing was in the %params, we can use the
32 # attribute's default value (if it has one)
8339fae2 33 if (!defined $val && $attr->has_default) {
34 $val = $attr->default($instance);
35 }
00867c44 36 if (defined $val) {
00867c44 37 if ($attr->has_type_constraint) {
34a66aa3 38 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
a27aa600 39 $val = $attr->type_constraint->coercion->coerce($val);
7415b2cb 40 }
a27aa600 41 (defined($attr->type_constraint->check($val)))
66811d63 42 || confess "Attribute (" . $attr->name . ") does not pass the type contraint with '$val'";
00867c44 43 }
a15dff8d 44 }
45 $instance->{$attr->name} = $val;
6ba6d68c 46 if (defined $val && $attr->is_weak_ref) {
47 weaken($instance->{$attr->name});
48 }
a15dff8d 49 }
50 return $instance;
51}
52
78cd1d3b 53sub add_override_method_modifier {
54 my ($self, $name, $method, $_super_package) = @_;
55 # need this for roles ...
56 $_super_package ||= $self->name;
57 my $super = $self->find_next_method_by_name($name);
58 (defined $super)
59 || confess "You cannot override '$name' because it has no super method";
60 $self->add_method($name => sub {
61 my @args = @_;
62 no strict 'refs';
63 no warnings 'redefine';
64 local *{$_super_package . '::super'} = sub { $super->(@args) };
65 return $method->(@args);
66 });
67}
68
69sub add_augment_method_modifier {
70 my ($self, $name, $method) = @_;
71 my $super = $self->find_next_method_by_name($name);
72 (defined $super)
73 || confess "You cannot augment '$name' because it has no super method";
74 $self->add_method($name => sub {
75 my @args = @_;
76 no strict 'refs';
77 no warnings 'redefine';
78 local *{$super->package_name . '::inner'} = sub { $method->(@args) };
79 return $super->(@args);
80 });
81}
82
c0e30cf5 831;
84
85__END__
86
87=pod
88
89=head1 NAME
90
e522431d 91Moose::Meta::Class - The Moose metaclass
c0e30cf5 92
c0e30cf5 93=head1 DESCRIPTION
94
e522431d 95This is a subclass of L<Class::MOP::Class> with Moose specific
96extensions.
97
6ba6d68c 98For the most part, the only time you will ever encounter an
99instance of this class is if you are doing some serious deep
100introspection. To really understand this class, you need to refer
101to the L<Class::MOP::Class> documentation.
102
c0e30cf5 103=head1 METHODS
104
105=over 4
106
a15dff8d 107=item B<construct_instance>
108
6ba6d68c 109This provides some Moose specific extensions to this method, you
110almost never call this method directly unless you really know what
111you are doing.
112
113This method makes sure to handle the moose weak-ref, type-constraint
114and type coercion features.
ef1d5f4b 115
78cd1d3b 116=item B<add_override_method_modifier ($name, $method)>
117
118=item B<add_augment_method_modifier ($name, $method)>
119
c0e30cf5 120=back
121
122=head1 BUGS
123
124All complex software has bugs lurking in it, and this module is no
125exception. If you find a bug please either email me, or add the bug
126to cpan-RT.
127
c0e30cf5 128=head1 AUTHOR
129
130Stevan Little E<lt>stevan@iinteractive.comE<gt>
131
132=head1 COPYRIGHT AND LICENSE
133
134Copyright 2006 by Infinity Interactive, Inc.
135
136L<http://www.iinteractive.com>
137
138This library is free software; you can redistribute it and/or modify
139it under the same terms as Perl itself.
140
141=cut