Use compute_all_applicable_attributes instead of get_attribute_map in the constructor...
[gitmo/Mouse.git] / lib / Mouse / Object.pm
1 #!/usr/bin/env perl
2 package Mouse::Object;
3 use strict;
4 use warnings;
5 use MRO::Compat;
6
7 use Scalar::Util qw/blessed weaken/;
8 use Carp 'confess';
9
10 sub new {
11     my $class = shift;
12     my %args;
13     if (scalar @_ == 1) {
14         if (defined $_[0]) {
15             (ref($_[0]) eq 'HASH')
16                 || confess "Single parameters to new() must be a HASH ref";
17             %args = %{$_[0]};
18         }
19     }
20     else {
21         %args = @_;
22     }
23
24     my $instance = bless {}, $class;
25
26     for my $attribute ($class->meta->compute_all_applicable_attributes) {
27         my $from = $attribute->init_arg;
28         my $key  = $attribute->name;
29         my $default;
30
31         if (!exists($args{$from})) {
32             if ($attribute->has_default || $attribute->has_builder) {
33                 unless ($attribute->is_lazy) {
34                     my $default = $attribute->default;
35                     my $builder = $attribute->builder;
36                     my $value = $attribute->has_builder
37                               ? $instance->$builder
38                               : ref($default) eq 'CODE'
39                                   ? $default->()
40                                   : $default;
41
42                     $attribute->verify_type_constraint($value)
43                         if $attribute->has_type_constraint;
44
45                     $instance->{$key} = $value;
46
47                     weaken($instance->{$key})
48                         if ref($instance->{$key}) && $attribute->is_weak_ref;
49                 }
50             }
51             else {
52                 if ($attribute->is_required) {
53                     confess "Attribute (".$attribute->name.") is required";
54                 }
55             }
56         }
57
58         if (exists($args{$from})) {
59             $attribute->verify_type_constraint($args{$from})
60                 if $attribute->has_type_constraint;
61
62             $instance->{$key} = $args{$from};
63
64             weaken($instance->{$key})
65                 if ref($instance->{$key}) && $attribute->is_weak_ref;
66
67             if ($attribute->has_trigger) {
68                 $attribute->trigger->($instance, $args{$from}, $attribute);
69             }
70         }
71     }
72
73     $instance->BUILDALL(\%args);
74
75     return $instance;
76 }
77
78 sub DESTROY { shift->DEMOLISHALL }
79
80 sub BUILDALL {
81     my $self = shift;
82
83     # short circuit
84     return unless $self->can('BUILD');
85
86     no strict 'refs';
87
88     for my $class (reverse $self->meta->linearized_isa) {
89         my $code = *{ $class . '::BUILD' }{CODE}
90             or next;
91         $code->($self, @_);
92     }
93 }
94
95 sub DEMOLISHALL {
96     my $self = shift;
97
98     # short circuit
99     return unless $self->can('DEMOLISH');
100
101     no strict 'refs';
102
103     for my $class ($self->meta->linearized_isa) {
104         my $code = *{ $class . '::DEMOLISH' }{CODE}
105             or next;
106         $code->($self, @_);
107     }
108 }
109
110 1;
111
112 __END__
113
114 =head1 NAME
115
116 Mouse::Object - we don't need to steenkin' constructor
117
118 =head1 METHODS
119
120 =head2 new arguments -> object
121
122 Instantiates a new Mouse::Object. This is obviously intended for subclasses.
123
124 =head2 BUILDALL \%args
125
126 Calls L</BUILD> on each class in the class hierarchy. This is called at the
127 end of L</new>.
128
129 =head2 BUILD \%args
130
131 You may put any business logic initialization in BUILD methods. You don't
132 need to redispatch or return any specific value.
133
134 =head2 DEMOLISHALL
135
136 Calls L</DEMOLISH> on each class in the class hierarchy. This is called at
137 L</DESTROY> time.
138
139 =head2 DEMOLISH
140
141 You may put any business logic deinitialization in DEMOLISH methods. You don't
142 need to redispatch or return any specific value.
143
144 =cut
145