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