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})) { |
23 | $attribute->verify_type_constraint($args->{$from}) |
491e5923 |
24 | if $attribute->has_type_constraint; |
25 | |
d574882a |
26 | $instance->{$key} = $args->{$from}; |
491e5923 |
27 | |
28 | weaken($instance->{$key}) |
29 | if ref($instance->{$key}) && $attribute->is_weak_ref; |
30 | |
31 | if ($attribute->has_trigger) { |
d574882a |
32 | $attribute->trigger->($instance, $args->{$from}, $attribute); |
491e5923 |
33 | } |
34 | } |
35 | else { |
de9a434a |
36 | if ($attribute->has_default || $attribute->has_builder) { |
2434d21b |
37 | unless ($attribute->is_lazy) { |
fb706f5c |
38 | my $default = $attribute->default; |
de9a434a |
39 | my $builder = $attribute->builder; |
40 | my $value = $attribute->has_builder |
41 | ? $instance->$builder |
42 | : ref($default) eq 'CODE' |
43 | ? $default->() |
44 | : $default; |
45 | |
46 | $attribute->verify_type_constraint($value) |
5aa30ced |
47 | if $attribute->has_type_constraint; |
48 | |
de9a434a |
49 | $instance->{$key} = $value; |
5aa30ced |
50 | |
b17094ce |
51 | weaken($instance->{$key}) |
3645b316 |
52 | if ref($instance->{$key}) && $attribute->is_weak_ref; |
c3398f5b |
53 | } |
54 | } |
55 | else { |
2434d21b |
56 | if ($attribute->is_required) { |
398327c3 |
57 | confess "Attribute (".$attribute->name.") is required"; |
c3398f5b |
58 | } |
59 | } |
60 | } |
c3398f5b |
61 | } |
62 | |
d574882a |
63 | $instance->BUILDALL($args); |
c3398f5b |
64 | |
65 | return $instance; |
66 | } |
67 | |
d574882a |
68 | sub BUILDARGS { |
69 | my $class = shift; |
70 | |
71 | if (scalar @_ == 1) { |
72 | if (defined $_[0]) { |
73 | (ref($_[0]) eq 'HASH') |
74 | || confess "Single parameters to new() must be a HASH ref"; |
75 | return {%{$_[0]}}; |
76 | } else { |
77 | return {}; |
78 | } |
79 | } |
80 | else { |
81 | return {@_}; |
82 | } |
83 | } |
84 | |
c3398f5b |
85 | sub DESTROY { shift->DEMOLISHALL } |
86 | |
87 | sub BUILDALL { |
88 | my $self = shift; |
89 | |
90 | # short circuit |
91 | return unless $self->can('BUILD'); |
92 | |
93 | no strict 'refs'; |
94 | |
2230a6a3 |
95 | for my $class (reverse $self->meta->linearized_isa) { |
c3398f5b |
96 | my $code = *{ $class . '::BUILD' }{CODE} |
97 | or next; |
98 | $code->($self, @_); |
99 | } |
100 | } |
101 | |
102 | sub DEMOLISHALL { |
103 | my $self = shift; |
104 | |
105 | # short circuit |
106 | return unless $self->can('DEMOLISH'); |
107 | |
108 | no strict 'refs'; |
109 | |
110 | for my $class ($self->meta->linearized_isa) { |
111 | my $code = *{ $class . '::DEMOLISH' }{CODE} |
112 | or next; |
113 | $code->($self, @_); |
114 | } |
115 | } |
116 | |
117 | 1; |
118 | |
119 | __END__ |
120 | |
121 | =head1 NAME |
122 | |
123 | Mouse::Object - we don't need to steenkin' constructor |
124 | |
125 | =head1 METHODS |
126 | |
127 | =head2 new arguments -> object |
128 | |
129 | Instantiates a new Mouse::Object. This is obviously intended for subclasses. |
130 | |
131 | =head2 BUILDALL \%args |
132 | |
133 | Calls L</BUILD> on each class in the class hierarchy. This is called at the |
134 | end of L</new>. |
135 | |
136 | =head2 BUILD \%args |
137 | |
138 | You may put any business logic initialization in BUILD methods. You don't |
139 | need to redispatch or return any specific value. |
140 | |
141 | =head2 DEMOLISHALL |
142 | |
143 | Calls L</DEMOLISH> on each class in the class hierarchy. This is called at |
144 | L</DESTROY> time. |
145 | |
146 | =head2 DEMOLISH |
147 | |
148 | You may put any business logic deinitialization in DEMOLISH methods. You don't |
149 | need to redispatch or return any specific value. |
150 | |
151 | =cut |
152 | |