Commit | Line | Data |
c3398f5b |
1 | package Mouse::Object; |
2 | use strict; |
3 | use warnings; |
c3398f5b |
4 | |
6d28c5cf |
5 | use Mouse::Util; |
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 | |
df963a63 |
69 | sub dump { |
8536d351 |
70 | my($self, $maxdepth) = @_; |
71 | |
72 | require 'Data/Dumper.pm'; # we don't want to create its namespace |
73 | my $dd = Data::Dumper->new([$self]); |
74 | $dd->Maxdepth($maxdepth || 1); |
75 | return $dd->Dump(); |
df963a63 |
76 | } |
77 | |
56a558f9 |
78 | |
79 | sub does { |
80 | my ($self, $role_name) = @_; |
81 | (defined $role_name) |
fce211ae |
82 | || $self->meta->throw_error("You must supply a role name to does()"); |
3a63a2e7 |
83 | |
3370794f |
84 | return $self->meta->does_role($role_name); |
56a558f9 |
85 | }; |
86 | |
c3398f5b |
87 | 1; |
88 | |
89 | __END__ |
90 | |
91 | =head1 NAME |
92 | |
93 | Mouse::Object - we don't need to steenkin' constructor |
94 | |
95 | =head1 METHODS |
96 | |
97 | =head2 new arguments -> object |
98 | |
99 | Instantiates a new Mouse::Object. This is obviously intended for subclasses. |
100 | |
101 | =head2 BUILDALL \%args |
102 | |
103 | Calls L</BUILD> on each class in the class hierarchy. This is called at the |
104 | end of L</new>. |
105 | |
106 | =head2 BUILD \%args |
107 | |
108 | You may put any business logic initialization in BUILD methods. You don't |
109 | need to redispatch or return any specific value. |
110 | |
442125dc |
111 | =head2 BUILDARGS |
112 | |
113 | Lets you override the arguments that C<new> takes. Return a hashref of |
114 | parameters. |
115 | |
c3398f5b |
116 | =head2 DEMOLISHALL |
117 | |
118 | Calls L</DEMOLISH> on each class in the class hierarchy. This is called at |
119 | L</DESTROY> time. |
120 | |
121 | =head2 DEMOLISH |
122 | |
123 | You may put any business logic deinitialization in DEMOLISH methods. You don't |
124 | need to redispatch or return any specific value. |
125 | |
df963a63 |
126 | |
56a558f9 |
127 | =head2 does $role_name |
128 | |
129 | This will check if the invocant's class "does" a given C<$role_name>. |
130 | This is similar to "isa" for object, but it checks the roles instead. |
131 | |
132 | |
df963a63 |
133 | =head2 B<dump ($maxdepth)> |
134 | |
135 | From the Moose POD: |
136 | |
137 | C'mon, how many times have you written the following code while debugging: |
138 | |
139 | use Data::Dumper; |
140 | warn Dumper $obj; |
141 | |
142 | It can get seriously annoying, so why not just use this. |
143 | |
144 | The implementation was lifted directly from Moose::Object. |
145 | |
c3398f5b |
146 | =cut |
147 | |
df963a63 |
148 | |