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