triggers
[gitmo/Moose.git] / lib / Moose / Object.pm
CommitLineData
fcd84ca9 1
2package Moose::Object;
3
4use strict;
5use warnings;
648e79ae 6
7use Moose::Meta::Class;
bc1e29b5 8use metaclass 'Moose::Meta::Class' => (
9 ':attribute_metaclass' => 'Moose::Meta::Attribute'
10);
11
0677220d 12use Carp 'confess';
13
ef333f17 14our $VERSION = '0.04';
fcd84ca9 15
16sub new {
05d9eaf6 17 my $class = shift;
18 my %params = (scalar @_ == 1) ? %{$_[0]} : @_;
c0e30cf5 19 my $self = $class->meta->new_object(%params);
d7f17ebb 20 $self->BUILDALL(\%params);
c0e30cf5 21 return $self;
fcd84ca9 22}
23
c0e30cf5 24sub BUILDALL {
d7f17ebb 25 my ($self, $params) = @_;
6ba6d68c 26 foreach my $method (reverse $self->meta->find_all_methods_by_name('BUILD')) {
d7f17ebb 27 $method->{code}->($self, $params);
c0e30cf5 28 }
29}
30
31sub DEMOLISHALL {
32 my $self = shift;
33 foreach my $method ($self->meta->find_all_methods_by_name('DEMOLISH')) {
5569c072 34 $method->{code}->($self);
c0e30cf5 35 }
36}
37
38sub DESTROY { goto &DEMOLISHALL }
39
ef333f17 40# new does() methods will be created
41# as approiate see Moose::Meta::Role
0677220d 42sub does {
bdabd620 43 my ($self, $role_name) = @_;
0677220d 44 (defined $role_name)
45 || confess "You much supply a role name to does()";
bdabd620 46 my $meta = $self->meta;
47 foreach my $class ($meta->class_precedence_list) {
48 return 1
49 if $meta->initialize($class)->does_role($role_name);
50 }
51 return 0;
0677220d 52}
ef333f17 53
fcd84ca9 541;
55
56__END__
57
58=pod
59
60=head1 NAME
61
e522431d 62Moose::Object - The base object for Moose
fcd84ca9 63
fcd84ca9 64=head1 DESCRIPTION
65
6ba6d68c 66This serves as the base object for all Moose classes. Every
67effort will be made to ensure that all classes which C<use Moose>
68will inherit from this class. It provides a default constructor
69and destructor, which run all the BUILD and DEMOLISH methods in
70the class tree.
71
72You don't actually I<need> to inherit from this in order to
73use Moose though. It is just here to make life easier.
74
fcd84ca9 75=head1 METHODS
76
77=over 4
78
79=item B<meta>
80
6ba6d68c 81This will return the metaclass associated with the given class.
82
fcd84ca9 83=item B<new>
84
e522431d 85This will create a new instance and call C<BUILDALL>.
86
c0e30cf5 87=item B<BUILDALL>
88
d7f17ebb 89This will call every C<BUILD> method in the inheritance hierarchy,
90and pass it a hash-ref of the the C<%params> passed to C<new>.
e522431d 91
c0e30cf5 92=item B<DEMOLISHALL>
93
e522431d 94This will call every C<DEMOLISH> method in the inheritance hierarchy.
95
ef333f17 96=item B<does ($role_name)>
97
fcd84ca9 98=back
99
100=head1 BUGS
101
102All complex software has bugs lurking in it, and this module is no
103exception. If you find a bug please either email me, or add the bug
104to cpan-RT.
105
fcd84ca9 106=head1 AUTHOR
107
108Stevan Little E<lt>stevan@iinteractive.comE<gt>
109
110=head1 COPYRIGHT AND LICENSE
111
112Copyright 2006 by Infinity Interactive, Inc.
113
114L<http://www.iinteractive.com>
115
116This library is free software; you can redistribute it and/or modify
117it under the same terms as Perl itself.
118
119=cut