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