Add BUILDARGS tests
[gitmo/Mouse.git] / lib / Mouse / Object.pm
CommitLineData
c3398f5b 1package Mouse::Object;
bc69ee88 2use Mouse::Util qw(does dump); # enables strict and warnings
6d28c5cf 3
c3398f5b 4sub new {
5 my $class = shift;
d574882a 6
c73a0a96 7 $class->meta->throw_error('Cannot call new() on an instance') if ref $class;
da4cb913 8
8536d351 9 my $args = $class->BUILDARGS(@_);
c3398f5b 10
6ff81d81 11 my $meta = Mouse::Meta::Class->initialize($class);
12 my $self = $meta->new_object($args);
13
14 # BUILDALL
15 if( $self->can('BUILD') ) {
16 for my $class (reverse $meta->linearized_isa) {
17 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
18 || next;
19
20 $self->$build($args);
21 }
22 }
23
24 return $self;
c3398f5b 25}
26
d574882a 27sub BUILDARGS {
28 my $class = shift;
29
30 if (scalar @_ == 1) {
c9aefe26 31 (ref($_[0]) eq 'HASH')
fce211ae 32 || $class->meta->throw_error("Single parameters to new() must be a HASH ref");
7ca5c5fb 33
c9aefe26 34 return {%{$_[0]}};
d574882a 35 }
36 else {
37 return {@_};
38 }
39}
40
7ca5c5fb 41sub DESTROY {
42 my $self = shift;
43
6ff81d81 44 return unless $self->can('DEMOLISH'); # short circuit
45
7746ba36 46 local $?;
47
48 my $e = do{
49 local $@;
50 eval{
6ff81d81 51
52 # DEMOLISHALL
53
54 # We cannot count on being able to retrieve a previously made
55 # metaclass, _or_ being able to make a new one during global
56 # destruction. However, we should still be able to use mro at
57 # that time (at least tests suggest so ;)
58
59 foreach my $class (@{ Mouse::Util::get_linear_isa(ref $self) }) {
60 my $demolish = Mouse::Util::get_code_ref($class, 'DEMOLISH')
61 || next;
62
63 $self->$demolish();
64 }
7746ba36 65 };
66 $@;
67 };
68
69 no warnings 'misc';
70 die $e if $e; # rethrow
7ca5c5fb 71}
c3398f5b 72
73sub BUILDALL {
74 my $self = shift;
75
76 # short circuit
77 return unless $self->can('BUILD');
78
2230a6a3 79 for my $class (reverse $self->meta->linearized_isa) {
5124b717 80 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
81 || next;
7ca5c5fb 82
83 $self->$build(@_);
c3398f5b 84 }
3a63a2e7 85 return;
c3398f5b 86}
87
88sub DEMOLISHALL {
89 my $self = shift;
90
91 # short circuit
92 return unless $self->can('DEMOLISH');
93
8536d351 94 # We cannot count on being able to retrieve a previously made
95 # metaclass, _or_ being able to make a new one during global
96 # destruction. However, we should still be able to use mro at
97 # that time (at least tests suggest so ;)
c26e296a 98
8536d351 99 foreach my $class (@{ Mouse::Util::get_linear_isa(ref $self) }) {
5124b717 100 my $demolish = Mouse::Util::get_code_ref($class, 'DEMOLISH')
101 || next;
7ca5c5fb 102
103 $self->$demolish();
c3398f5b 104 }
3a63a2e7 105 return;
c3398f5b 106}
107
1081;
109
110__END__
111
112=head1 NAME
113
bedd575c 114Mouse::Object - The base object for Mouse classes
c3398f5b 115
a25ca8d6 116=head1 VERSION
117
6e168432 118This document describes Mouse version 0.40_05
a25ca8d6 119
c3398f5b 120=head1 METHODS
121
31c5194b 122=head2 C<< new (Arguments) -> Object >>
c3398f5b 123
1820fffe 124Instantiates a new C<Mouse::Object>. This is obviously intended for subclasses.
c3398f5b 125
31c5194b 126=head2 C<< BUILDARGS (Arguments) -> HashRef >>
c3398f5b 127
1820fffe 128Lets you override the arguments that C<new> takes. Return a hashref of
129parameters.
c3398f5b 130
31c5194b 131=head2 C<< BUILDALL (\%args) >>
c3398f5b 132
1820fffe 133Calls C<BUILD> on each class in the class hierarchy. This is called at the
134end of C<new>.
c3398f5b 135
31c5194b 136=head2 C<< BUILD (\%args) >>
442125dc 137
1820fffe 138You may put any business logic initialization in BUILD methods. You don't
139need to redispatch or return any specific value.
442125dc 140
1820fffe 141=head2 C<< DEMOLISHALL >>
c3398f5b 142
1820fffe 143Calls C<DEMOLISH> on each class in the class hierarchy. This is called at
144C<DESTROY> time.
c3398f5b 145
1820fffe 146=head2 C<< DEMOLISH >>
c3398f5b 147
148You may put any business logic deinitialization in DEMOLISH methods. You don't
149need to redispatch or return any specific value.
150
df963a63 151
1820fffe 152=head2 C<< does ($role_name) -> Bool >>
56a558f9 153
1820fffe 154This will check if the invocant's class B<does> a given C<$role_name>.
56a558f9 155This is similar to "isa" for object, but it checks the roles instead.
156
1820fffe 157=head2 C<<dump ($maxdepth) -> Str >>
df963a63 158
159From the Moose POD:
160
161 C'mon, how many times have you written the following code while debugging:
162
163 use Data::Dumper;
164 warn Dumper $obj;
165
166 It can get seriously annoying, so why not just use this.
167
168The implementation was lifted directly from Moose::Object.
169
1820fffe 170=head1 SEE ALSO
171
172L<Moose::Object>
c3398f5b 173
1820fffe 174=cut
df963a63 175