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