56eaff4d34d568ab835a78e7bc4fdadce44fee4b
[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.56';
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     if (scalar @_ == 1) {
26         if (defined $_[0]) {
27             (ref($_[0]) eq 'HASH')
28                 || confess "Single parameters to new() must be a HASH ref";
29             return {%{$_[0]}};
30         } 
31         else {
32             return {}; # FIXME this is compat behavior, but is it correct?
33         }
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 # support for UNIVERSAL::DOES ...
77 BEGIN {
78     my $does = UNIVERSAL->can("DOES") ? "SUPER::DOES" : "isa";
79     eval 'sub DOES {
80         my ( $self, $class_or_role_name ) = @_;
81         return $self->'.$does.'($class_or_role_name)
82             || $self->does($class_or_role_name);
83     }';
84 }
85
86 # new does() methods will be created 
87 # as approiate see Moose::Meta::Role
88 sub does {
89     my ($self, $role_name) = @_;
90     (defined $role_name)
91         || confess "You must supply a role name to does()";
92     my $meta = $self->meta;
93     foreach my $class ($meta->class_precedence_list) {
94         my $m = $meta->initialize($class);
95         return 1 
96             if $m->can('does_role') && $m->does_role($role_name);            
97     }
98     return 0;   
99 }
100
101 # RANT:
102 # Cmon, how many times have you written 
103 # the following code while debugging:
104
105 #  use Data::Dumper; 
106 #  warn Dumper \%thing;
107 #
108 # It can get seriously annoying, so why 
109 # not just do this ...
110 sub dump { 
111     my $self = shift;
112     require Data::Dumper;
113     local $Data::Dumper::Maxdepth = shift if @_;
114     Data::Dumper::Dumper $self;
115 }
116
117 1;
118
119 __END__
120
121 =pod
122
123 =head1 NAME
124
125 Moose::Object - The base object for Moose
126
127 =head1 DESCRIPTION
128
129 This serves as the base object for all Moose classes. Every 
130 effort will be made to ensure that all classes which C<use Moose> 
131 will inherit from this class. It provides a default constructor 
132 and destructor, which run all the BUILD and DEMOLISH methods in 
133 the class tree.
134
135 You don't actually I<need> to inherit from this in order to 
136 use Moose though. It is just here to make life easier.
137
138 =head1 METHODS
139
140 =over 4
141
142 =item B<meta>
143
144 This will return the metaclass associated with the given class.
145
146 =item B<new>
147
148 This will call C<BUILDARGS>, create a new instance and call C<BUILDALL>.
149
150 =item B<BUILDARGS>
151
152 This method processes an argument list into a hash reference. It is used by
153 C<new>.
154
155 =item B<BUILDALL>
156
157 This will call every C<BUILD> method in the inheritance hierarchy, 
158 and pass it a hash-ref of the the C<%params> passed to C<new>.
159
160 =item B<DEMOLISHALL>
161
162 This will call every C<DEMOLISH> method in the inheritance hierarchy.
163
164 =item B<does ($role_name)>
165
166 This will check if the invocant's class C<does> a given C<$role_name>. 
167 This is similar to C<isa> for object, but it checks the roles instead.
168
169 =item B<DOES ($class_or_role_name)>
170
171 A Moose Role aware implementation of L<UNIVERSAL/DOES>.
172
173 C<DOES> is equivalent to C<isa> or C<does>.
174
175 =item B<dump ($maxdepth)>
176
177 Cmon, how many times have you written the following code while debugging:
178
179  use Data::Dumper; 
180  warn Dumper $obj;
181
182 It can get seriously annoying, so why not just use this.
183
184 =back
185
186 =head1 BUGS
187
188 All complex software has bugs lurking in it, and this module is no 
189 exception. If you find a bug please either email me, or add the bug
190 to cpan-RT.
191
192 =head1 AUTHOR
193
194 Stevan Little E<lt>stevan@iinteractive.comE<gt>
195
196 =head1 COPYRIGHT AND LICENSE
197
198 Copyright 2006-2008 by Infinity Interactive, Inc.
199
200 L<http://www.iinteractive.com>
201
202 This library is free software; you can redistribute it and/or modify
203 it under the same terms as Perl itself. 
204
205 =cut