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