0.18 final
[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.08';
13 our $AUTHORITY = 'cpan:STEVAN';
14
15 sub new {
16     my $class = shift;
17     my %params;
18     if (scalar @_ == 1) {
19         (ref($_[0]) eq 'HASH')
20             || confess "Single parameters to new() must be a HASH ref";
21         %params = %{$_[0]};
22     }
23     else {
24         %params = @_;
25     }
26         my $self = $class->meta->new_object(%params);
27         $self->BUILDALL(\%params);
28         return $self;
29 }
30
31 sub BUILDALL {
32     # NOTE: we ask Perl if we even 
33     # need to do this first, to avoid
34     # extra meta level calls
35         return unless $_[0]->can('BUILD');    
36         my ($self, $params) = @_;
37         foreach my $method (reverse $self->meta->find_all_methods_by_name('BUILD')) {
38                 $method->{code}->($self, $params);
39         }
40 }
41
42 sub DEMOLISHALL {
43     # NOTE: we ask Perl if we even 
44     # need to do this first, to avoid
45     # extra meta level calls    
46         return unless $_[0]->can('DEMOLISH');    
47         my $self = shift;       
48         foreach my $method ($self->meta->find_all_methods_by_name('DEMOLISH')) {
49                 $method->{code}->($self);
50         }       
51 }
52
53 sub DESTROY { goto &DEMOLISHALL }
54
55 # new does() methods will be created 
56 # as approiate see Moose::Meta::Role
57 sub does {
58     my ($self, $role_name) = @_;
59     (defined $role_name)
60         || confess "You much supply a role name to does()";
61     my $meta = $self->meta;
62     foreach my $class ($meta->class_precedence_list) {
63         return 1 
64             if $meta->initialize($class)->does_role($role_name);            
65     }
66     return 0;   
67 }
68
69 # RANT:
70 # Cmon, how many times have you written 
71 # the following code while debugging:
72
73 #  use Data::Dumper; 
74 #  warn Dumper \%thing;
75 #
76 # It can get seriously annoying, so why 
77 # not just do this ...
78 sub dump { 
79     my $self = shift;
80     require Data::Dumper;
81     $Data::Dumper::Maxdepth = shift if @_;
82     Data::Dumper::Dumper $self;
83 }
84
85 1;
86
87 __END__
88
89 =pod
90
91 =head1 NAME
92
93 Moose::Object - The base object for Moose
94
95 =head1 DESCRIPTION
96
97 This serves as the base object for all Moose classes. Every 
98 effort will be made to ensure that all classes which C<use Moose> 
99 will inherit from this class. It provides a default constructor 
100 and destructor, which run all the BUILD and DEMOLISH methods in 
101 the class tree.
102
103 You don't actually I<need> to inherit from this in order to 
104 use Moose though. It is just here to make life easier.
105
106 =head1 METHODS
107
108 =over 4
109
110 =item B<meta>
111
112 This will return the metaclass associated with the given class.
113
114 =item B<new>
115
116 This will create a new instance and call C<BUILDALL>.
117
118 =item B<BUILDALL>
119
120 This will call every C<BUILD> method in the inheritance hierarchy, 
121 and pass it a hash-ref of the the C<%params> passed to C<new>.
122
123 =item B<DEMOLISHALL>
124
125 This will call every C<DEMOLISH> method in the inheritance hierarchy.
126
127 =item B<does ($role_name)>
128
129 This will check if the invocant's class C<does> a given C<$role_name>. 
130 This is similar to C<isa> for object, but it checks the roles instead.
131
132 =item B<dump ($maxdepth)>
133
134 Cmon, how many times have you written the following code while debugging:
135
136  use Data::Dumper; 
137  warn Dumper $obj;
138
139 It can get seriously annoying, so why not just use this.
140
141 =back
142
143 =head1 BUGS
144
145 All complex software has bugs lurking in it, and this module is no 
146 exception. If you find a bug please either email me, or add the bug
147 to cpan-RT.
148
149 =head1 AUTHOR
150
151 Stevan Little E<lt>stevan@iinteractive.comE<gt>
152
153 =head1 COPYRIGHT AND LICENSE
154
155 Copyright 2006, 2007 by Infinity Interactive, Inc.
156
157 L<http://www.iinteractive.com>
158
159 This library is free software; you can redistribute it and/or modify
160 it under the same terms as Perl itself. 
161
162 =cut