If we re-import Mouse, don't thrash the class's superclasses.
[gitmo/Mouse.git] / lib / Mouse / Object.pm
CommitLineData
c3398f5b 1#!/usr/bin/env perl
2package Mouse::Object;
3use strict;
4use warnings;
5use MRO::Compat;
6
7use Scalar::Util 'blessed';
8use Carp 'confess';
9
10sub new {
11 my $class = shift;
12 my %args = @_;
13 my $instance = bless {}, $class;
14
15 for my $attribute ($class->meta->attributes) {
16 my $key = $attribute->init_arg;
17 my $default;
18
19 if (!exists($args{$key})) {
1be61030 20 if (exists($attribute->{default}) || exists($attribute->{builder})) {
c3398f5b 21 unless ($attribute->{lazy}) {
1be61030 22 my $builder = $attribute->{builder};
23 my $default = exists($attribute->{builder})
24 ? $instance->$builder
25 : ref($attribute->{default}) eq 'CODE'
26 ? $attribute->{default}->()
27 : $attribute->{default};
5aa30ced 28
29 $attribute->verify_type_constraint($default)
30 if $attribute->has_type_constraint;
31
32 $instance->{$key} = $default;
33
34 Scalar::Util::weaken($instance->{$key})
35 if $attribute->{weak_ref};
c3398f5b 36 }
37 }
38 else {
39 if ($attribute->{required}) {
40 confess "Attribute '$attribute->{name}' is required";
41 }
42 }
43 }
44
45 if (exists($args{$key})) {
5aa30ced 46 $attribute->verify_type_constraint($args{$key})
47 if $attribute->has_type_constraint;
48
c3398f5b 49 $instance->{$key} = $args{$key};
5aa30ced 50
c3398f5b 51 Scalar::Util::weaken($instance->{$key})
52 if $attribute->{weak_ref};
53
54 if ($attribute->{trigger}) {
55 $attribute->{trigger}->($instance, $args{$key}, $attribute);
56 }
57 }
58 }
59
60 $instance->BUILDALL(\%args);
61
62 return $instance;
63}
64
65sub DESTROY { shift->DEMOLISHALL }
66
67sub BUILDALL {
68 my $self = shift;
69
70 # short circuit
71 return unless $self->can('BUILD');
72
73 no strict 'refs';
74
75 for my $class ($self->meta->linearized_isa) {
76 my $code = *{ $class . '::BUILD' }{CODE}
77 or next;
78 $code->($self, @_);
79 }
80}
81
82sub DEMOLISHALL {
83 my $self = shift;
84
85 # short circuit
86 return unless $self->can('DEMOLISH');
87
88 no strict 'refs';
89
90 for my $class ($self->meta->linearized_isa) {
91 my $code = *{ $class . '::DEMOLISH' }{CODE}
92 or next;
93 $code->($self, @_);
94 }
95}
96
971;
98
99__END__
100
101=head1 NAME
102
103Mouse::Object - we don't need to steenkin' constructor
104
105=head1 METHODS
106
107=head2 new arguments -> object
108
109Instantiates a new Mouse::Object. This is obviously intended for subclasses.
110
111=head2 BUILDALL \%args
112
113Calls L</BUILD> on each class in the class hierarchy. This is called at the
114end of L</new>.
115
116=head2 BUILD \%args
117
118You may put any business logic initialization in BUILD methods. You don't
119need to redispatch or return any specific value.
120
121=head2 DEMOLISHALL
122
123Calls L</DEMOLISH> on each class in the class hierarchy. This is called at
124L</DESTROY> time.
125
126=head2 DEMOLISH
127
128You may put any business logic deinitialization in DEMOLISH methods. You don't
129need to redispatch or return any specific value.
130
131=cut
132