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