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