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