Commit | Line | Data |
fcd84ca9 |
1 | |
2 | package Moose::Object; |
3 | |
4 | use strict; |
5 | use warnings; |
648e79ae |
6 | |
9922fa6f |
7 | use if ( not our $__mx_is_compiled ), 'Moose::Meta::Class'; |
8 | use if ( not our $__mx_is_compiled ), metaclass => 'Moose::Meta::Class'; |
bc1e29b5 |
9 | |
0677220d |
10 | use Carp 'confess'; |
11 | |
3a0c064a |
12 | our $VERSION = '0.13'; |
d44714be |
13 | our $AUTHORITY = 'cpan:STEVAN'; |
fcd84ca9 |
14 | |
15 | sub new { |
2c0cbef7 |
16 | my $class = shift; |
8a157bab |
17 | my %params; |
18 | if (scalar @_ == 1) { |
86629f93 |
19 | if (defined $_[0]) { |
20 | (ref($_[0]) eq 'HASH') |
21 | || confess "Single parameters to new() must be a HASH ref"; |
22 | %params = %{$_[0]}; |
23 | } |
8a157bab |
24 | } |
25 | else { |
26 | %params = @_; |
27 | } |
fb1e11d5 |
28 | my $self = $class->meta->new_object(%params); |
29 | $self->BUILDALL(\%params); |
30 | return $self; |
fcd84ca9 |
31 | } |
32 | |
c0e30cf5 |
33 | sub BUILDALL { |
d44714be |
34 | # NOTE: we ask Perl if we even |
35 | # need to do this first, to avoid |
36 | # extra meta level calls |
fb1e11d5 |
37 | return unless $_[0]->can('BUILD'); |
38 | my ($self, $params) = @_; |
39 | foreach my $method (reverse $self->meta->find_all_methods_by_name('BUILD')) { |
ac0824ee |
40 | $method->{code}->body->($self, $params); |
fb1e11d5 |
41 | } |
c0e30cf5 |
42 | } |
43 | |
44 | sub DEMOLISHALL { |
3a0c064a |
45 | my $self = shift; |
46 | foreach my $method ($self->meta->find_all_methods_by_name('DEMOLISH')) { |
47 | $method->{code}->body->($self); |
48 | } |
49 | } |
50 | |
51 | sub DESTROY { |
d44714be |
52 | # NOTE: we ask Perl if we even |
53 | # need to do this first, to avoid |
54 | # extra meta level calls |
3a0c064a |
55 | return unless $_[0]->can('DEMOLISH'); |
56 | # if we have an exception here ... |
57 | if (my $e = $@) { |
58 | # run DEMOLISHALL ourselves, ... |
59 | (shift)->DEMOLISHALL; |
60 | # then restore the exception ... |
61 | $@ = $e; |
62 | # and return ... |
63 | return; |
64 | } |
65 | goto &DEMOLISHALL |
c0e30cf5 |
66 | } |
67 | |
ef333f17 |
68 | # new does() methods will be created |
69 | # as approiate see Moose::Meta::Role |
0677220d |
70 | sub does { |
bdabd620 |
71 | my ($self, $role_name) = @_; |
0677220d |
72 | (defined $role_name) |
73 | || confess "You much supply a role name to does()"; |
bdabd620 |
74 | my $meta = $self->meta; |
75 | foreach my $class ($meta->class_precedence_list) { |
587e457d |
76 | my $m = $meta->initialize($class); |
bdabd620 |
77 | return 1 |
587e457d |
78 | if $m->can('does_role') && $m->does_role($role_name); |
bdabd620 |
79 | } |
80 | return 0; |
0677220d |
81 | } |
ef333f17 |
82 | |
f742dfef |
83 | # RANT: |
84 | # Cmon, how many times have you written |
85 | # the following code while debugging: |
86 | # |
87 | # use Data::Dumper; |
88 | # warn Dumper \%thing; |
89 | # |
90 | # It can get seriously annoying, so why |
91 | # not just do this ... |
92 | sub dump { |
93 | my $self = shift; |
94 | require Data::Dumper; |
1a386a6c |
95 | local $Data::Dumper::Maxdepth = shift if @_; |
f742dfef |
96 | Data::Dumper::Dumper $self; |
97 | } |
98 | |
fcd84ca9 |
99 | 1; |
100 | |
101 | __END__ |
102 | |
103 | =pod |
104 | |
105 | =head1 NAME |
106 | |
e522431d |
107 | Moose::Object - The base object for Moose |
fcd84ca9 |
108 | |
fcd84ca9 |
109 | =head1 DESCRIPTION |
110 | |
6ba6d68c |
111 | This serves as the base object for all Moose classes. Every |
112 | effort will be made to ensure that all classes which C<use Moose> |
113 | will inherit from this class. It provides a default constructor |
114 | and destructor, which run all the BUILD and DEMOLISH methods in |
115 | the class tree. |
116 | |
117 | You don't actually I<need> to inherit from this in order to |
118 | use Moose though. It is just here to make life easier. |
119 | |
fcd84ca9 |
120 | =head1 METHODS |
121 | |
122 | =over 4 |
123 | |
124 | =item B<meta> |
125 | |
6ba6d68c |
126 | This will return the metaclass associated with the given class. |
127 | |
fcd84ca9 |
128 | =item B<new> |
129 | |
e522431d |
130 | This will create a new instance and call C<BUILDALL>. |
131 | |
c0e30cf5 |
132 | =item B<BUILDALL> |
133 | |
d7f17ebb |
134 | This will call every C<BUILD> method in the inheritance hierarchy, |
135 | and pass it a hash-ref of the the C<%params> passed to C<new>. |
e522431d |
136 | |
c0e30cf5 |
137 | =item B<DEMOLISHALL> |
138 | |
e522431d |
139 | This will call every C<DEMOLISH> method in the inheritance hierarchy. |
140 | |
ef333f17 |
141 | =item B<does ($role_name)> |
142 | |
02a0fb52 |
143 | This will check if the invocant's class C<does> a given C<$role_name>. |
144 | This is similar to C<isa> for object, but it checks the roles instead. |
145 | |
f742dfef |
146 | =item B<dump ($maxdepth)> |
147 | |
148 | Cmon, how many times have you written the following code while debugging: |
149 | |
150 | use Data::Dumper; |
151 | warn Dumper $obj; |
152 | |
153 | It can get seriously annoying, so why not just use this. |
154 | |
fcd84ca9 |
155 | =back |
156 | |
157 | =head1 BUGS |
158 | |
159 | All complex software has bugs lurking in it, and this module is no |
160 | exception. If you find a bug please either email me, or add the bug |
161 | to cpan-RT. |
162 | |
fcd84ca9 |
163 | =head1 AUTHOR |
164 | |
165 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
166 | |
167 | =head1 COPYRIGHT AND LICENSE |
168 | |
778db3ac |
169 | Copyright 2006-2008 by Infinity Interactive, Inc. |
fcd84ca9 |
170 | |
171 | L<http://www.iinteractive.com> |
172 | |
173 | This library is free software; you can redistribute it and/or modify |
174 | it under the same terms as Perl itself. |
175 | |
9922fa6f |
176 | =cut |