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