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