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