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