5 use Scalar::Util 'weaken';
11 my $args = $class->BUILDARGS(@_);
13 my $instance = bless {}, $class;
15 for my $attribute ($class->meta->compute_all_applicable_attributes) {
16 my $from = $attribute->init_arg;
17 my $key = $attribute->name;
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});
24 $instance->{$key} = $args->{$from};
26 weaken($instance->{$key})
27 if ref($instance->{$key}) && $attribute->is_weak_ref;
29 if ($attribute->has_trigger) {
30 $attribute->trigger->($instance, $args->{$from});
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
40 : ref($default) eq 'CODE'
41 ? $default->($instance)
44 $value = $attribute->coerce_constraint($value)
45 if $attribute->should_coerce;
46 $attribute->verify_against_type_constraint($value);
48 $instance->{$key} = $value;
50 weaken($instance->{$key})
51 if ref($instance->{$key}) && $attribute->is_weak_ref;
55 if ($attribute->is_required) {
56 confess "Attribute (".$attribute->name.") is required";
62 $instance->BUILDALL($args);
72 (ref($_[0]) eq 'HASH')
73 || confess "Single parameters to new() must be a HASH ref";
84 sub DESTROY { shift->DEMOLISHALL }
90 return unless $self->can('BUILD');
92 for my $class (reverse $self->meta->linearized_isa) {
95 my $code = *{ $class . '::BUILD' }{CODE}
105 return unless $self->can('DEMOLISH');
109 for my $class ($self->meta->linearized_isa) {
110 my $code = *{ $class . '::DEMOLISH' }{CODE}
118 require Data::Dumper;
119 local $Data::Dumper::Maxdepth = shift if @_;
120 Data::Dumper::Dumper $self;
125 my ($self, $role_name) = @_;
127 || confess "You must supply a role name to does()";
128 my $meta = $self->meta;
129 foreach my $class ($meta->linearized_isa) {
130 my $m = $meta->initialize($class);
132 if $m->can('does_role') && $m->does_role($role_name);
143 Mouse::Object - we don't need to steenkin' constructor
147 =head2 new arguments -> object
149 Instantiates a new Mouse::Object. This is obviously intended for subclasses.
151 =head2 BUILDALL \%args
153 Calls L</BUILD> on each class in the class hierarchy. This is called at the
158 You may put any business logic initialization in BUILD methods. You don't
159 need to redispatch or return any specific value.
163 Lets you override the arguments that C<new> takes. Return a hashref of
168 Calls L</DEMOLISH> on each class in the class hierarchy. This is called at
173 You may put any business logic deinitialization in DEMOLISH methods. You don't
174 need to redispatch or return any specific value.
177 =head2 does $role_name
179 This will check if the invocant's class "does" a given C<$role_name>.
180 This is similar to "isa" for object, but it checks the roles instead.
183 =head2 B<dump ($maxdepth)>
187 C'mon, how many times have you written the following code while debugging:
192 It can get seriously annoying, so why not just use this.
194 The implementation was lifted directly from Moose::Object.