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