copyright date changes on Class::MOP
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Constructor.pm
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.02';
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 use base 'Class::MOP::Method::Generated';
14
15 sub new {
16     my $class   = shift;
17     my %options = @_;
18
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};
22
23     my $self = bless {
24         # from our superclass
25         '&!body'          => undef,
26         # specific to this subclass
27         '%!options'              => $options{options} || {},
28         '$!associated_metaclass' => $options{metaclass},
29         '$!is_inline'            => ($options{is_inline} || 0),
30     } => $class;
31
32     # we don't want this creating
33     # a cycle in the code, if not
34     # needed
35     weaken($self->{'$!associated_metaclass'});
36
37     $self->initialize_body;
38
39     return $self;
40 }
41
42 ## accessors
43
44 sub options              { (shift)->{'%!options'}              }
45 sub associated_metaclass { (shift)->{'$!associated_metaclass'} }
46
47 ## cached values ...
48
49 sub meta_instance {
50     my $self = shift;
51     $self->{'$!meta_instance'} ||= $self->associated_metaclass->get_meta_instance;
52 }
53
54 sub attributes {
55     my $self = shift;
56     $self->{'@!attributes'} ||= [ $self->associated_metaclass->compute_all_applicable_attributes ]
57 }
58
59 ## method
60
61 sub 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
70 sub generate_constructor_method {
71     return sub { (shift)->meta->new_object(@_) }
72 }
73
74 sub generate_constructor_method_inline {
75     my $self = shift;
76
77     my $source = 'sub {';
78     $source .= "\n" . 'my ($class, %params) = @_;';
79
80     $source .= "\n" . 'return $class->meta->new_object(%params)';
81     $source .= "\n" . '    if $class ne \'' . $self->associated_metaclass->name . '\';';
82
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     }
101     return $code;
102 }
103
104 sub _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     } elsif( $attr->has_builder ) {
129         $default = '$instance->'.$attr->builder;
130     }
131
132   'if(exists $params{\'' . $attr->init_arg . '\'}){' . "\n" .
133         $self->meta_instance->inline_set_slot_value(
134             '$instance',
135             ("'" . $attr->name . "'"),
136             '$params{\'' . $attr->init_arg . '\'}' ) . "\n" .
137    '} ' . (!defined $default ? '' : 'else {' . "\n" .
138         $self->meta_instance->inline_set_slot_value(
139             '$instance',
140             ("'" . $attr->name . "'"),
141              $default ) . "\n" .
142    '}');
143 }
144
145 1;
146
147 1;
148
149 __END__
150
151 =pod
152
153 =head1 NAME
154
155 Class::MOP::Method::Constructor - Method Meta Object for constructors
156
157 =head1 SYNOPSIS
158
159   use Class::MOP::Method::Constructor;
160
161   my $constructor = Class::MOP::Method::Constructor->new(
162       metaclass => $metaclass,
163       options   => {
164           debug => 1, # this is all for now
165       },
166   );
167
168   # calling the constructor ...
169   $constructor->body->($metaclass->name, %params);
170
171 =head1 DESCRIPTION
172
173 This is a subclass of C<Class::MOP::Method> which deals with
174 class constructors.
175
176 =head1 METHODS
177
178 =over 4
179
180 =item B<new (metaclass => $meta, options => \%options)>
181
182 =item B<options>
183
184 This returns the options HASH which is passed into C<new>.
185
186 =item B<associated_metaclass>
187
188 This returns the metaclass which is passed into C<new>.
189
190 =item B<attributes>
191
192 This returns the list of attributes which are associated with the
193 metaclass which is passed into C<new>.
194
195 =item B<meta_instance>
196
197 This returns the meta instance which is associated with the
198 metaclass which is passed into C<new>.
199
200 =item B<is_inline>
201
202 This returns a boolean, but since constructors are very rarely
203 not inlined, this always returns true for now.
204
205 =item B<initialize_body>
206
207 This creates the code reference for the constructor itself.
208
209 =back
210
211 =head2 Method Generators
212
213 =over 4
214
215 =item B<generate_constructor_method>
216
217 =item B<generate_constructor_method_inline>
218
219 =back
220
221 =head1 AUTHORS
222
223 Stevan Little E<lt>stevan@iinteractive.comE<gt>
224
225 =head1 COPYRIGHT AND LICENSE
226
227 Copyright 2006-2008 by Infinity Interactive, Inc.
228
229 L<http://www.iinteractive.com>
230
231 This library is free software; you can redistribute it and/or modify
232 it under the same terms as Perl itself.
233
234 =cut
235