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