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