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