Switch to Mouse::Exporter
[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
fce211ae 7 $class->throw_error('Cannot call new() on an instance') if ref $class;
da4cb913 8
8536d351 9 my $args = $class->BUILDARGS(@_);
c3398f5b 10
fce211ae 11 my $instance = Mouse::Meta::Class->initialize($class)->new_object($args);
d574882a 12 $instance->BUILDALL($args);
c3398f5b 13 return $instance;
14}
15
d574882a 16sub BUILDARGS {
17 my $class = shift;
18
19 if (scalar @_ == 1) {
c9aefe26 20 (ref($_[0]) eq 'HASH')
fce211ae 21 || $class->meta->throw_error("Single parameters to new() must be a HASH ref");
7ca5c5fb 22
c9aefe26 23 return {%{$_[0]}};
d574882a 24 }
25 else {
26 return {@_};
27 }
28}
29
7ca5c5fb 30sub DESTROY {
31 my $self = shift;
32
33 $self->DEMOLISHALL();
34}
c3398f5b 35
36sub BUILDALL {
37 my $self = shift;
38
39 # short circuit
40 return unless $self->can('BUILD');
41
2230a6a3 42 for my $class (reverse $self->meta->linearized_isa) {
7ca5c5fb 43 my $build = do{ no strict 'refs'; *{ $class . '::BUILD' }{CODE} }
c3398f5b 44 or next;
7ca5c5fb 45
46 $self->$build(@_);
c3398f5b 47 }
3a63a2e7 48 return;
c3398f5b 49}
50
51sub DEMOLISHALL {
52 my $self = shift;
53
54 # short circuit
55 return unless $self->can('DEMOLISH');
56
8536d351 57 # We cannot count on being able to retrieve a previously made
58 # metaclass, _or_ being able to make a new one during global
59 # destruction. However, we should still be able to use mro at
60 # that time (at least tests suggest so ;)
c26e296a 61
8536d351 62 foreach my $class (@{ Mouse::Util::get_linear_isa(ref $self) }) {
7ca5c5fb 63 my $demolish = do{ no strict 'refs'; *{ $class . '::DEMOLISH'}{CODE} }
64 or next;
65
66 $self->$demolish();
c3398f5b 67 }
3a63a2e7 68 return;
c3398f5b 69}
70
711;
72
73__END__
74
75=head1 NAME
76
bedd575c 77Mouse::Object - The base object for Mouse classes
c3398f5b 78
79=head1 METHODS
80
31c5194b 81=head2 C<< new (Arguments) -> Object >>
c3398f5b 82
1820fffe 83Instantiates a new C<Mouse::Object>. This is obviously intended for subclasses.
c3398f5b 84
31c5194b 85=head2 C<< BUILDARGS (Arguments) -> HashRef >>
c3398f5b 86
1820fffe 87Lets you override the arguments that C<new> takes. Return a hashref of
88parameters.
c3398f5b 89
31c5194b 90=head2 C<< BUILDALL (\%args) >>
c3398f5b 91
1820fffe 92Calls C<BUILD> on each class in the class hierarchy. This is called at the
93end of C<new>.
c3398f5b 94
31c5194b 95=head2 C<< BUILD (\%args) >>
442125dc 96
1820fffe 97You may put any business logic initialization in BUILD methods. You don't
98need to redispatch or return any specific value.
442125dc 99
1820fffe 100=head2 C<< DEMOLISHALL >>
c3398f5b 101
1820fffe 102Calls C<DEMOLISH> on each class in the class hierarchy. This is called at
103C<DESTROY> time.
c3398f5b 104
1820fffe 105=head2 C<< DEMOLISH >>
c3398f5b 106
107You may put any business logic deinitialization in DEMOLISH methods. You don't
108need to redispatch or return any specific value.
109
df963a63 110
1820fffe 111=head2 C<< does ($role_name) -> Bool >>
56a558f9 112
1820fffe 113This will check if the invocant's class B<does> a given C<$role_name>.
56a558f9 114This is similar to "isa" for object, but it checks the roles instead.
115
1820fffe 116=head2 C<<dump ($maxdepth) -> Str >>
df963a63 117
118From the Moose POD:
119
120 C'mon, how many times have you written the following code while debugging:
121
122 use Data::Dumper;
123 warn Dumper $obj;
124
125 It can get seriously annoying, so why not just use this.
126
127The implementation was lifted directly from Moose::Object.
128
1820fffe 129=head1 SEE ALSO
130
131L<Moose::Object>
c3398f5b 132
1820fffe 133=cut
df963a63 134