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