Use Test::Exception in this test
[gitmo/Mouse.git] / lib / Mouse / Object.pm
CommitLineData
c3398f5b 1package Mouse::Object;
2use strict;
3use warnings;
c3398f5b 4
6c169c50 5use Scalar::Util qw/weaken/;
c3398f5b 6use Carp 'confess';
7
8sub new {
9 my $class = shift;
d574882a 10
11 my $args = $class->BUILDARGS(@_);
da4cb913 12
c3398f5b 13 my $instance = bless {}, $class;
14
28d26949 15 for my $attribute ($class->meta->compute_all_applicable_attributes) {
384072a3 16 my $from = $attribute->init_arg;
17 my $key = $attribute->name;
c3398f5b 18
d574882a 19 if (defined($from) && exists($args->{$from})) {
4188b837 20 $args->{$from} = $attribute->coerce_constraint($args->{$from})
32af3489 21 if $attribute->should_coerce;
a08e715f 22 $attribute->verify_type_constraint($args->{$from})
23 if $attribute->has_type_constraint;
491e5923 24
a08e715f 25 $instance->{$key} = $args->{$from};
fe5fe061 26
a08e715f 27 weaken($instance->{$key})
28 if ref($instance->{$key}) && $attribute->is_weak_ref;
491e5923 29
a08e715f 30 if ($attribute->has_trigger) {
31 $attribute->trigger->($instance, $args->{$from}, $attribute);
491e5923 32 }
33 }
34 else {
de9a434a 35 if ($attribute->has_default || $attribute->has_builder) {
2434d21b 36 unless ($attribute->is_lazy) {
fb706f5c 37 my $default = $attribute->default;
de9a434a 38 my $builder = $attribute->builder;
39 my $value = $attribute->has_builder
40 ? $instance->$builder
41 : ref($default) eq 'CODE'
0e503bd9 42 ? $default->($instance)
de9a434a 43 : $default;
44
4188b837 45 $value = $attribute->coerce_constraint($value)
32af3489 46 if $attribute->should_coerce;
de9a434a 47 $attribute->verify_type_constraint($value)
5aa30ced 48 if $attribute->has_type_constraint;
49
de9a434a 50 $instance->{$key} = $value;
5aa30ced 51
b17094ce 52 weaken($instance->{$key})
3645b316 53 if ref($instance->{$key}) && $attribute->is_weak_ref;
c3398f5b 54 }
55 }
56 else {
2434d21b 57 if ($attribute->is_required) {
398327c3 58 confess "Attribute (".$attribute->name.") is required";
c3398f5b 59 }
60 }
61 }
c3398f5b 62 }
63
d574882a 64 $instance->BUILDALL($args);
c3398f5b 65
66 return $instance;
67}
68
d574882a 69sub BUILDARGS {
70 my $class = shift;
71
72 if (scalar @_ == 1) {
73 if (defined $_[0]) {
74 (ref($_[0]) eq 'HASH')
75 || confess "Single parameters to new() must be a HASH ref";
76 return {%{$_[0]}};
77 } else {
78 return {};
79 }
80 }
81 else {
82 return {@_};
83 }
84}
85
c3398f5b 86sub DESTROY { shift->DEMOLISHALL }
87
88sub BUILDALL {
89 my $self = shift;
90
91 # short circuit
92 return unless $self->can('BUILD');
93
2230a6a3 94 for my $class (reverse $self->meta->linearized_isa) {
cbe29bd9 95 no strict 'refs';
96 no warnings 'once';
c3398f5b 97 my $code = *{ $class . '::BUILD' }{CODE}
98 or next;
99 $code->($self, @_);
100 }
101}
102
103sub DEMOLISHALL {
104 my $self = shift;
105
106 # short circuit
107 return unless $self->can('DEMOLISH');
108
109 no strict 'refs';
110
111 for my $class ($self->meta->linearized_isa) {
112 my $code = *{ $class . '::DEMOLISH' }{CODE}
113 or next;
114 $code->($self, @_);
115 }
116}
117
1181;
119
120__END__
121
122=head1 NAME
123
124Mouse::Object - we don't need to steenkin' constructor
125
126=head1 METHODS
127
128=head2 new arguments -> object
129
130Instantiates a new Mouse::Object. This is obviously intended for subclasses.
131
132=head2 BUILDALL \%args
133
134Calls L</BUILD> on each class in the class hierarchy. This is called at the
135end of L</new>.
136
137=head2 BUILD \%args
138
139You may put any business logic initialization in BUILD methods. You don't
140need to redispatch or return any specific value.
141
442125dc 142=head2 BUILDARGS
143
144Lets you override the arguments that C<new> takes. Return a hashref of
145parameters.
146
c3398f5b 147=head2 DEMOLISHALL
148
149Calls L</DEMOLISH> on each class in the class hierarchy. This is called at
150L</DESTROY> time.
151
152=head2 DEMOLISH
153
154You may put any business logic deinitialization in DEMOLISH methods. You don't
155need to redispatch or return any specific value.
156
157=cut
158