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