0_03_02
[gitmo/Moose.git] / lib / Moose / Object.pm
1
2 package Moose::Object;
3
4 use strict;
5 use warnings;
6 use metaclass 'Moose::Meta::Class' => (
7         ':attribute_metaclass' => 'Moose::Meta::Attribute'
8 );
9
10 use Carp 'confess';
11
12 our $VERSION = '0.04';
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 (undef, $role_name) = @_;
42     (defined $role_name)
43         || confess "You much supply a role name to does()";
44     0;    
45 }
46
47 1;
48
49 __END__
50
51 =pod
52
53 =head1 NAME
54
55 Moose::Object - The base object for Moose
56
57 =head1 DESCRIPTION
58
59 This serves as the base object for all Moose classes. Every 
60 effort will be made to ensure that all classes which C<use Moose> 
61 will inherit from this class. It provides a default constructor 
62 and destructor, which run all the BUILD and DEMOLISH methods in 
63 the class tree.
64
65 You don't actually I<need> to inherit from this in order to 
66 use Moose though. It is just here to make life easier.
67
68 =head1 METHODS
69
70 =over 4
71
72 =item B<meta>
73
74 This will return the metaclass associated with the given class.
75
76 =item B<new>
77
78 This will create a new instance and call C<BUILDALL>.
79
80 =item B<BUILDALL>
81
82 This will call every C<BUILD> method in the inheritance hierarchy, 
83 and pass it a hash-ref of the the C<%params> passed to C<new>.
84
85 =item B<DEMOLISHALL>
86
87 This will call every C<DEMOLISH> method in the inheritance hierarchy.
88
89 =item B<does ($role_name)>
90
91 =back
92
93 =head1 BUGS
94
95 All complex software has bugs lurking in it, and this module is no 
96 exception. If you find a bug please either email me, or add the bug
97 to cpan-RT.
98
99 =head1 AUTHOR
100
101 Stevan Little E<lt>stevan@iinteractive.comE<gt>
102
103 =head1 COPYRIGHT AND LICENSE
104
105 Copyright 2006 by Infinity Interactive, Inc.
106
107 L<http://www.iinteractive.com>
108
109 This library is free software; you can redistribute it and/or modify
110 it under the same terms as Perl itself. 
111
112 =cut