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