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