whoops
[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
8339fae2 10our $VERSION = '0.03';
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;
21 $val = $params{$init_arg} if exists $params{$init_arg};
22 # if nothing was in the %params, we can use the
23 # attribute's default value (if it has one)
8339fae2 24 if (!defined $val && $attr->has_default) {
25 $val = $attr->default($instance);
26 }
00867c44 27 if (defined $val) {
00867c44 28 if ($attr->has_type_constraint) {
34a66aa3 29 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
a27aa600 30 $val = $attr->type_constraint->coercion->coerce($val);
7415b2cb 31 }
a27aa600 32 (defined($attr->type_constraint->check($val)))
66811d63 33 || confess "Attribute (" . $attr->name . ") does not pass the type contraint with '$val'";
00867c44 34 }
a15dff8d 35 }
36 $instance->{$attr->name} = $val;
6ba6d68c 37 if (defined $val && $attr->is_weak_ref) {
38 weaken($instance->{$attr->name});
39 }
a15dff8d 40 }
41 return $instance;
42}
43
c0e30cf5 441;
45
46__END__
47
48=pod
49
50=head1 NAME
51
e522431d 52Moose::Meta::Class - The Moose metaclass
c0e30cf5 53
c0e30cf5 54=head1 DESCRIPTION
55
e522431d 56This is a subclass of L<Class::MOP::Class> with Moose specific
57extensions.
58
6ba6d68c 59For the most part, the only time you will ever encounter an
60instance of this class is if you are doing some serious deep
61introspection. To really understand this class, you need to refer
62to the L<Class::MOP::Class> documentation.
63
c0e30cf5 64=head1 METHODS
65
66=over 4
67
a15dff8d 68=item B<construct_instance>
69
6ba6d68c 70This provides some Moose specific extensions to this method, you
71almost never call this method directly unless you really know what
72you are doing.
73
74This method makes sure to handle the moose weak-ref, type-constraint
75and type coercion features.
ef1d5f4b 76
c0e30cf5 77=back
78
79=head1 BUGS
80
81All complex software has bugs lurking in it, and this module is no
82exception. If you find a bug please either email me, or add the bug
83to cpan-RT.
84
c0e30cf5 85=head1 AUTHOR
86
87Stevan Little E<lt>stevan@iinteractive.comE<gt>
88
89=head1 COPYRIGHT AND LICENSE
90
91Copyright 2006 by Infinity Interactive, Inc.
92
93L<http://www.iinteractive.com>
94
95This library is free software; you can redistribute it and/or modify
96it under the same terms as Perl itself.
97
98=cut