Use Scalar::Util/Carp imports for brevity
[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
b17094ce 7use Scalar::Util qw/blessed weaken/;
c3398f5b 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})) {
de9a434a 20 if ($attribute->has_default || $attribute->has_builder) {
21 my $default = $attribute->default;
22
2434d21b 23 unless ($attribute->is_lazy) {
de9a434a 24 my $builder = $attribute->builder;
25 my $value = $attribute->has_builder
26 ? $instance->$builder
27 : ref($default) eq 'CODE'
28 ? $default->()
29 : $default;
30
31 $attribute->verify_type_constraint($value)
5aa30ced 32 if $attribute->has_type_constraint;
33
de9a434a 34 $instance->{$key} = $value;
5aa30ced 35
b17094ce 36 weaken($instance->{$key})
de9a434a 37 if $attribute->weak_ref;
c3398f5b 38 }
39 }
40 else {
2434d21b 41 if ($attribute->is_required) {
de9a434a 42 confess "Attribute '".$attribute->name."' is required";
c3398f5b 43 }
44 }
45 }
46
47 if (exists($args{$key})) {
5aa30ced 48 $attribute->verify_type_constraint($args{$key})
49 if $attribute->has_type_constraint;
50
c3398f5b 51 $instance->{$key} = $args{$key};
5aa30ced 52
b17094ce 53 weaken($instance->{$key})
de9a434a 54 if $attribute->weak_ref;
c3398f5b 55
de9a434a 56 if ($attribute->has_trigger) {
57 $attribute->trigger->($instance, $args{$key}, $attribute);
c3398f5b 58 }
59 }
60 }
61
62 $instance->BUILDALL(\%args);
63
64 return $instance;
65}
66
67sub DESTROY { shift->DEMOLISHALL }
68
69sub BUILDALL {
70 my $self = shift;
71
72 # short circuit
73 return unless $self->can('BUILD');
74
75 no strict 'refs';
76
77 for my $class ($self->meta->linearized_isa) {
78 my $code = *{ $class . '::BUILD' }{CODE}
79 or next;
80 $code->($self, @_);
81 }
82}
83
84sub DEMOLISHALL {
85 my $self = shift;
86
87 # short circuit
88 return unless $self->can('DEMOLISH');
89
90 no strict 'refs';
91
92 for my $class ($self->meta->linearized_isa) {
93 my $code = *{ $class . '::DEMOLISH' }{CODE}
94 or next;
95 $code->($self, @_);
96 }
97}
98
991;
100
101__END__
102
103=head1 NAME
104
105Mouse::Object - we don't need to steenkin' constructor
106
107=head1 METHODS
108
109=head2 new arguments -> object
110
111Instantiates a new Mouse::Object. This is obviously intended for subclasses.
112
113=head2 BUILDALL \%args
114
115Calls L</BUILD> on each class in the class hierarchy. This is called at the
116end of L</new>.
117
118=head2 BUILD \%args
119
120You may put any business logic initialization in BUILD methods. You don't
121need to redispatch or return any specific value.
122
123=head2 DEMOLISHALL
124
125Calls L</DEMOLISH> on each class in the class hierarchy. This is called at
126L</DESTROY> time.
127
128=head2 DEMOLISH
129
130You may put any business logic deinitialization in DEMOLISH methods. You don't
131need to redispatch or return any specific value.
132
133=cut
134