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