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