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