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