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