Save global variables (Mouse/Util.pm)
[gitmo/Mouse.git] / lib / Mouse / Object.pm
CommitLineData
c3398f5b 1package Mouse::Object;
2use strict;
3use warnings;
c3398f5b 4
f6715552 5use Scalar::Util 'weaken';
c3398f5b 6use Carp 'confess';
7
8sub new {
9 my $class = shift;
d574882a 10
11 my $args = $class->BUILDARGS(@_);
da4cb913 12
c3398f5b 13 my $instance = bless {}, $class;
14
0eaf53c2 15 for my $attribute ($class->meta->get_all_attributes) {
384072a3 16 my $from = $attribute->init_arg;
17 my $key = $attribute->name;
c3398f5b 18
d574882a 19 if (defined($from) && exists($args->{$from})) {
4188b837 20 $args->{$from} = $attribute->coerce_constraint($args->{$from})
32af3489 21 if $attribute->should_coerce;
20e25eb9 22 $attribute->verify_against_type_constraint($args->{$from});
491e5923 23
a08e715f 24 $instance->{$key} = $args->{$from};
fe5fe061 25
a08e715f 26 weaken($instance->{$key})
27 if ref($instance->{$key}) && $attribute->is_weak_ref;
491e5923 28
a08e715f 29 if ($attribute->has_trigger) {
88b6c018 30 $attribute->trigger->($instance, $args->{$from});
491e5923 31 }
32 }
33 else {
de9a434a 34 if ($attribute->has_default || $attribute->has_builder) {
2434d21b 35 unless ($attribute->is_lazy) {
fb706f5c 36 my $default = $attribute->default;
de9a434a 37 my $builder = $attribute->builder;
38 my $value = $attribute->has_builder
39 ? $instance->$builder
40 : ref($default) eq 'CODE'
0e503bd9 41 ? $default->($instance)
de9a434a 42 : $default;
43
4188b837 44 $value = $attribute->coerce_constraint($value)
32af3489 45 if $attribute->should_coerce;
20e25eb9 46 $attribute->verify_against_type_constraint($value);
5aa30ced 47
de9a434a 48 $instance->{$key} = $value;
5aa30ced 49
b17094ce 50 weaken($instance->{$key})
3645b316 51 if ref($instance->{$key}) && $attribute->is_weak_ref;
c3398f5b 52 }
53 }
54 else {
2434d21b 55 if ($attribute->is_required) {
398327c3 56 confess "Attribute (".$attribute->name.") is required";
c3398f5b 57 }
58 }
59 }
c3398f5b 60 }
61
d574882a 62 $instance->BUILDALL($args);
c3398f5b 63
64 return $instance;
65}
66
d574882a 67sub BUILDARGS {
68 my $class = shift;
69
70 if (scalar @_ == 1) {
c9aefe26 71 (ref($_[0]) eq 'HASH')
72 || confess "Single parameters to new() must be a HASH ref";
73 return {%{$_[0]}};
d574882a 74 }
75 else {
76 return {@_};
77 }
78}
79
c3398f5b 80sub DESTROY { shift->DEMOLISHALL }
81
82sub BUILDALL {
83 my $self = shift;
84
85 # short circuit
86 return unless $self->can('BUILD');
87
2230a6a3 88 for my $class (reverse $self->meta->linearized_isa) {
cbe29bd9 89 no strict 'refs';
90 no warnings 'once';
c3398f5b 91 my $code = *{ $class . '::BUILD' }{CODE}
92 or next;
93 $code->($self, @_);
94 }
95}
96
97sub DEMOLISHALL {
98 my $self = shift;
99
100 # short circuit
101 return unless $self->can('DEMOLISH');
102
103 no strict 'refs';
104
c26e296a 105 my @isa;
106 if ( my $meta = Mouse::class_of($self) ) {
107 @isa = $meta->linearized_isa;
108 } else {
109 # We cannot count on being able to retrieve a previously made
110 # metaclass, _or_ being able to make a new one during global
111 # destruction. However, we should still be able to use mro at
112 # that time (at least tests suggest so ;)
113 my $class_name = ref $self;
6ebf64d5 114 @isa = @{ Mouse::Util::get_linear_isa($class_name) }
c26e296a 115 }
116
117 foreach my $class (@isa) {
118 no strict 'refs';
119 my $demolish = *{"${class}::DEMOLISH"}{CODE};
120 $self->$demolish
121 if defined $demolish;
c3398f5b 122 }
123}
124
df963a63 125sub dump {
126 my $self = shift;
127 require Data::Dumper;
128 local $Data::Dumper::Maxdepth = shift if @_;
129 Data::Dumper::Dumper $self;
130}
131
56a558f9 132
133sub does {
134 my ($self, $role_name) = @_;
135 (defined $role_name)
136 || confess "You must supply a role name to does()";
137 my $meta = $self->meta;
138 foreach my $class ($meta->linearized_isa) {
88ed7189 139 my $m = ref($meta)->initialize($class);
56a558f9 140 return 1
141 if $m->can('does_role') && $m->does_role($role_name);
142 }
143 return 0;
144};
145
c3398f5b 1461;
147
148__END__
149
150=head1 NAME
151
152Mouse::Object - we don't need to steenkin' constructor
153
154=head1 METHODS
155
156=head2 new arguments -> object
157
158Instantiates a new Mouse::Object. This is obviously intended for subclasses.
159
160=head2 BUILDALL \%args
161
162Calls L</BUILD> on each class in the class hierarchy. This is called at the
163end of L</new>.
164
165=head2 BUILD \%args
166
167You may put any business logic initialization in BUILD methods. You don't
168need to redispatch or return any specific value.
169
442125dc 170=head2 BUILDARGS
171
172Lets you override the arguments that C<new> takes. Return a hashref of
173parameters.
174
c3398f5b 175=head2 DEMOLISHALL
176
177Calls L</DEMOLISH> on each class in the class hierarchy. This is called at
178L</DESTROY> time.
179
180=head2 DEMOLISH
181
182You may put any business logic deinitialization in DEMOLISH methods. You don't
183need to redispatch or return any specific value.
184
df963a63 185
56a558f9 186=head2 does $role_name
187
188This will check if the invocant's class "does" a given C<$role_name>.
189This is similar to "isa" for object, but it checks the roles instead.
190
191
df963a63 192=head2 B<dump ($maxdepth)>
193
194From the Moose POD:
195
196 C'mon, how many times have you written the following code while debugging:
197
198 use Data::Dumper;
199 warn Dumper $obj;
200
201 It can get seriously annoying, so why not just use this.
202
203The implementation was lifted directly from Moose::Object.
204
c3398f5b 205=cut
206
df963a63 207