more-tweaks
[gitmo/Moose.git] / lib / Moose / Meta / Class.pm
CommitLineData
c0e30cf5 1
2package Moose::Meta::Class;
3
4use strict;
5use warnings;
6
a15dff8d 7use Carp 'confess';
8
0e3def6b 9our $VERSION = '0.02';
bc1e29b5 10
c0e30cf5 11use base 'Class::MOP::Class';
12
a15dff8d 13sub construct_instance {
14 my ($class, %params) = @_;
e522431d 15 my $instance = $params{'__INSTANCE__'} || {};
a15dff8d 16 foreach my $attr ($class->compute_all_applicable_attributes()) {
17 my $init_arg = $attr->init_arg();
18 # try to fetch the init arg from the %params ...
19 my $val;
20 $val = $params{$init_arg} if exists $params{$init_arg};
21 # if nothing was in the %params, we can use the
22 # attribute's default value (if it has one)
23 $val ||= $attr->default($instance) if $attr->has_default;
00867c44 24 if (defined $val) {
00867c44 25 if ($attr->has_type_constraint) {
34a66aa3 26 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
a27aa600 27 $val = $attr->type_constraint->coercion->coerce($val);
7415b2cb 28 }
a27aa600 29 (defined($attr->type_constraint->check($val)))
66811d63 30 || confess "Attribute (" . $attr->name . ") does not pass the type contraint with '$val'";
00867c44 31 }
a15dff8d 32 }
33 $instance->{$attr->name} = $val;
34 }
35 return $instance;
36}
37
c0e30cf5 381;
39
40__END__
41
42=pod
43
44=head1 NAME
45
e522431d 46Moose::Meta::Class - The Moose metaclass
c0e30cf5 47
48=head1 SYNOPSIS
49
50=head1 DESCRIPTION
51
e522431d 52This is a subclass of L<Class::MOP::Class> with Moose specific
53extensions.
54
c0e30cf5 55=head1 METHODS
56
57=over 4
58
a15dff8d 59=item B<construct_instance>
60
ef1d5f4b 61=item B<mixed_in>
62
c0e30cf5 63=back
64
65=head1 BUGS
66
67All complex software has bugs lurking in it, and this module is no
68exception. If you find a bug please either email me, or add the bug
69to cpan-RT.
70
c0e30cf5 71=head1 AUTHOR
72
73Stevan Little E<lt>stevan@iinteractive.comE<gt>
74
75=head1 COPYRIGHT AND LICENSE
76
77Copyright 2006 by Infinity Interactive, Inc.
78
79L<http://www.iinteractive.com>
80
81This library is free software; you can redistribute it and/or modify
82it under the same terms as Perl itself.
83
84=cut