Commit | Line | Data |
d90b42a6 |
1 | |
2 | package Class::MOP::Method::Constructor; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
7 | use Carp 'confess'; |
8 | use Scalar::Util 'blessed', 'weaken', 'looks_like_number'; |
9 | |
10 | our $VERSION = '0.01'; |
11 | our $AUTHORITY = 'cpan:STEVAN'; |
12 | |
13 | use base 'Class::MOP::Method'; |
14 | |
15 | sub new { |
16 | my $class = shift; |
17 | my %options = @_; |
18 | |
19 | (exists $options{options} && ref $options{options} eq 'HASH') |
20 | || confess "You must pass a hash of options"; |
d90b42a6 |
21 | |
22 | my $self = bless { |
23 | # from our superclass |
c23184fc |
24 | '&!body' => undef, |
d90b42a6 |
25 | # specific to this subclass |
c23184fc |
26 | '%!options' => $options{options}, |
27 | '$!meta_instance' => $options{metaclass}->get_meta_instance, |
28 | '@!attributes' => [ $options{metaclass}->compute_all_applicable_attributes ], |
29 | # ... |
30 | '$!associated_metaclass' => $options{metaclass}, |
d90b42a6 |
31 | } => $class; |
32 | |
33 | # we don't want this creating |
34 | # a cycle in the code, if not |
35 | # needed |
c23184fc |
36 | weaken($self->{'$!associated_metaclass'}); |
d90b42a6 |
37 | |
38 | $self->intialize_body; |
39 | |
40 | return $self; |
41 | } |
42 | |
c23184fc |
43 | ## predicates |
44 | |
45 | # NOTE: |
46 | # if it is blessed into this class, |
47 | # then it is always inlined, that is |
48 | # pretty much what this class is for. |
49 | sub is_inline { 1 } |
50 | |
d90b42a6 |
51 | ## accessors |
52 | |
c23184fc |
53 | sub options { (shift)->{'%!options'} } |
54 | sub meta_instance { (shift)->{'$!meta_instance'} } |
55 | sub attributes { (shift)->{'@!attributes'} } |
56 | |
57 | sub associated_metaclass { (shift)->{'$!associated_metaclass'} } |
d90b42a6 |
58 | |
59 | ## method |
60 | |
61 | sub intialize_body { |
62 | my $self = shift; |
63 | # TODO: |
64 | # the %options should also include a both |
65 | # a call 'initializer' and call 'SUPER::' |
66 | # options, which should cover approx 90% |
67 | # of the possible use cases (even if it |
68 | # requires some adaption on the part of |
69 | # the author, after all, nothing is free) |
70 | my $source = 'sub {'; |
71 | $source .= "\n" . 'my ($class, %params) = @_;'; |
72 | $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class'); |
73 | $source .= ";\n" . (join ";\n" => map { |
74 | $self->_generate_slot_initializer($_) |
75 | } 0 .. (@{$self->attributes} - 1)); |
76 | $source .= ";\n" . 'return $instance'; |
77 | $source .= ";\n" . '}'; |
78 | warn $source if $self->options->{debug}; |
79 | |
80 | my $code; |
81 | { |
82 | # NOTE: |
83 | # create the nessecary lexicals |
84 | # to be picked up in the eval |
85 | my $attrs = $self->attributes; |
86 | |
87 | $code = eval $source; |
88 | confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@; |
89 | } |
c23184fc |
90 | $self->{'&!body'} = $code; |
d90b42a6 |
91 | } |
92 | |
93 | sub _generate_slot_initializer { |
94 | my $self = shift; |
95 | my $index = shift; |
96 | |
97 | my $attr = $self->attributes->[$index]; |
98 | |
99 | my $default; |
100 | if ($attr->has_default) { |
101 | # NOTE: |
102 | # default values can either be CODE refs |
103 | # in which case we need to call them. Or |
104 | # they can be scalars (strings/numbers) |
105 | # in which case we can just deal with them |
106 | # in the code we eval. |
107 | if ($attr->is_default_a_coderef) { |
108 | $default = '$attrs->[' . $index . ']->default($instance)'; |
109 | } |
110 | else { |
111 | $default = $attr->default; |
112 | # make sure to quote strings ... |
113 | unless (looks_like_number($default)) { |
114 | $default = "'$default'"; |
115 | } |
116 | } |
117 | } |
118 | $self->meta_instance->inline_set_slot_value( |
119 | '$instance', |
120 | ("'" . $attr->name . "'"), |
121 | ('$params{\'' . $attr->init_arg . '\'}' . (defined $default ? (' || ' . $default) : '')) |
122 | ); |
123 | } |
124 | |
125 | 1; |
126 | |
127 | 1; |
128 | |
129 | __END__ |
130 | |
131 | =pod |
132 | |
133 | =head1 NAME |
134 | |
135 | Class::MOP::Method::Constructor - Method Meta Object for constructors |
136 | |
137 | =head1 SYNOPSIS |
138 | |
96e38ba6 |
139 | use Class::MOP::Method::Constructor; |
140 | |
141 | my $constructor = Class::MOP::Method::Constructor->new( |
142 | metaclass => $metaclass, |
143 | options => { |
144 | debug => 1, # this is all for now |
145 | }, |
146 | ); |
147 | |
148 | # calling the constructor ... |
149 | $constructor->body->($metaclass->name, %params); |
150 | |
d90b42a6 |
151 | =head1 DESCRIPTION |
152 | |
96e38ba6 |
153 | This is a subclass of C<Class::MOP::Method> which deals with |
154 | class constructors. |
155 | |
d90b42a6 |
156 | =head1 METHODS |
157 | |
158 | =over 4 |
159 | |
96e38ba6 |
160 | =item B<new (metaclass => $meta, options => \%options)> |
d90b42a6 |
161 | |
96e38ba6 |
162 | =item B<options> |
163 | |
164 | This returns the options HASH which is passed into C<new>. |
165 | |
166 | =item B<associated_metaclass> |
167 | |
168 | This returns the metaclass which is passed into C<new>. |
c23184fc |
169 | |
d90b42a6 |
170 | =item B<attributes> |
171 | |
96e38ba6 |
172 | This returns the list of attributes which are associated with the |
173 | metaclass which is passed into C<new>. |
174 | |
d90b42a6 |
175 | =item B<meta_instance> |
176 | |
96e38ba6 |
177 | This returns the meta instance which is associated with the |
178 | metaclass which is passed into C<new>. |
c23184fc |
179 | |
96e38ba6 |
180 | =item B<is_inline> |
181 | |
182 | This returns a boolean, but since constructors are very rarely |
183 | not inlined, this always returns true for now. |
d90b42a6 |
184 | |
185 | =item B<intialize_body> |
186 | |
96e38ba6 |
187 | This creates the code reference for the constructor itself. |
188 | |
d90b42a6 |
189 | =back |
190 | |
191 | =head1 AUTHORS |
192 | |
193 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
194 | |
195 | =head1 COPYRIGHT AND LICENSE |
196 | |
2367814a |
197 | Copyright 2006, 2007 by Infinity Interactive, Inc. |
d90b42a6 |
198 | |
199 | L<http://www.iinteractive.com> |
200 | |
201 | This library is free software; you can redistribute it and/or modify |
202 | it under the same terms as Perl itself. |
203 | |
204 | =cut |
205 | |