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