Commit | Line | Data |
---|---|---|
c3398f5b | 1 | package Mouse::Object; |
bc69ee88 | 2 | use Mouse::Util qw(does dump); # enables strict and warnings |
6d28c5cf | 3 | |
c3398f5b | 4 | sub 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 | 16 | sub 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 | 30 | sub DESTROY { |
31 | my $self = shift; | |
32 | ||
33 | $self->DEMOLISHALL(); | |
34 | } | |
c3398f5b | 35 | |
36 | sub 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 | ||
51 | sub 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 | ||
71 | 1; | |
72 | ||
73 | __END__ | |
74 | ||
75 | =head1 NAME | |
76 | ||
bedd575c | 77 | Mouse::Object - The base object for Mouse classes |
c3398f5b | 78 | |
79 | =head1 METHODS | |
80 | ||
31c5194b | 81 | =head2 C<< new (Arguments) -> Object >> |
c3398f5b | 82 | |
1820fffe | 83 | Instantiates a new C<Mouse::Object>. This is obviously intended for subclasses. |
c3398f5b | 84 | |
31c5194b | 85 | =head2 C<< BUILDARGS (Arguments) -> HashRef >> |
c3398f5b | 86 | |
1820fffe | 87 | Lets you override the arguments that C<new> takes. Return a hashref of |
88 | parameters. | |
c3398f5b | 89 | |
31c5194b | 90 | =head2 C<< BUILDALL (\%args) >> |
c3398f5b | 91 | |
1820fffe | 92 | Calls C<BUILD> on each class in the class hierarchy. This is called at the |
93 | end of C<new>. | |
c3398f5b | 94 | |
31c5194b | 95 | =head2 C<< BUILD (\%args) >> |
442125dc | 96 | |
1820fffe | 97 | You may put any business logic initialization in BUILD methods. You don't |
98 | need to redispatch or return any specific value. | |
442125dc | 99 | |
1820fffe | 100 | =head2 C<< DEMOLISHALL >> |
c3398f5b | 101 | |
1820fffe | 102 | Calls C<DEMOLISH> on each class in the class hierarchy. This is called at |
103 | C<DESTROY> time. | |
c3398f5b | 104 | |
1820fffe | 105 | =head2 C<< DEMOLISH >> |
c3398f5b | 106 | |
107 | You may put any business logic deinitialization in DEMOLISH methods. You don't | |
108 | need to redispatch or return any specific value. | |
109 | ||
df963a63 | 110 | |
1820fffe | 111 | =head2 C<< does ($role_name) -> Bool >> |
56a558f9 | 112 | |
1820fffe | 113 | This will check if the invocant's class B<does> a given C<$role_name>. |
56a558f9 | 114 | This is similar to "isa" for object, but it checks the roles instead. |
115 | ||
1820fffe | 116 | =head2 C<<dump ($maxdepth) -> Str >> |
df963a63 | 117 | |
118 | From 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 | ||
127 | The implementation was lifted directly from Moose::Object. | |
128 | ||
1820fffe | 129 | =head1 SEE ALSO |
130 | ||
131 | L<Moose::Object> | |
c3398f5b | 132 | |
1820fffe | 133 | =cut |
df963a63 | 134 |