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