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