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