okay, this is not meant to be used, but since i am not using svk or anything, I have...
[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.04';
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         '$!package_name'         => $options{package_name},
27         '$!name'                 => $options{name},        
28         # specific to this subclass
29         '%!options'              => $options{options} || {},
30         '$!associated_metaclass' => $options{metaclass},
31         '$!is_inline'            => ($options{is_inline} || 0),
32     } => $class;
33
34     # we don't want this creating
35     # a cycle in the code, if not
36     # needed
37     weaken($self->{'$!associated_metaclass'});
38
39     $self->initialize_body;
40
41     return $self;
42 }
43
44 ## accessors
45
46 sub options              { (shift)->{'%!options'}              }
47 sub associated_metaclass { (shift)->{'$!associated_metaclass'} }
48
49 ## cached values ...
50
51 sub meta_instance {
52     my $self = shift;
53     $self->{'$!meta_instance'} ||= $self->associated_metaclass->get_meta_instance;
54 }
55
56 sub attributes {
57     my $self = shift;
58     $self->{'@!attributes'} ||= [ $self->associated_metaclass->compute_all_applicable_attributes ]
59 }
60
61 ## method
62
63 sub initialize_body {
64     my $self        = shift;
65     my $method_name = 'generate_constructor_method';
66
67     $method_name .= '_inline' if $self->is_inline;
68
69     $self->{'&!body'} = $self->$method_name;
70 }
71
72 sub generate_constructor_method {
73     return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
74 }
75
76 sub generate_constructor_method_inline {
77     my $self = shift;
78
79     my $source = 'sub {';
80     $source .= "\n" . 'my ($class, %params) = @_;';
81
82     $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(%params)';
83     $source .= "\n" . '    if $class ne \'' . $self->associated_metaclass->name . '\';';
84
85     $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
86     $source .= ";\n" . (join ";\n" => map {
87         $self->_generate_slot_initializer($_)
88     } 0 .. (@{$self->attributes} - 1));
89     $source .= ";\n" . 'return $instance';
90     $source .= ";\n" . '}';
91     warn $source if $self->options->{debug};
92
93     my $code;
94     {
95         # NOTE:
96         # create the nessecary lexicals
97         # to be picked up in the eval
98         my $attrs = $self->attributes;
99
100         $code = eval $source;
101         confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
102     }
103     return $code;
104 }
105
106 sub _generate_slot_initializer {
107     my $self  = shift;
108     my $index = shift;
109
110     my $attr = $self->attributes->[$index];
111
112     my $default;
113     if ($attr->has_default) {
114         # NOTE:
115         # default values can either be CODE refs
116         # in which case we need to call them. Or
117         # they can be scalars (strings/numbers)
118         # in which case we can just deal with them
119         # in the code we eval.
120         if ($attr->is_default_a_coderef) {
121             $default = '$attrs->[' . $index . ']->default($instance)';
122         }
123         else {
124             $default = $attr->default;
125             # make sure to quote strings ...
126             unless (looks_like_number($default)) {
127                 $default = "'$default'";
128             }
129         }
130     } elsif( $attr->has_builder ) {
131         $default = '$instance->'.$attr->builder;
132     }
133
134     if ( defined $attr->init_arg ) {
135       return (
136           'if(exists $params{\'' . $attr->init_arg . '\'}){' . "\n" .
137                 $self->meta_instance->inline_set_slot_value(
138                     '$instance',
139                     ("'" . $attr->name . "'"),
140                     '$params{\'' . $attr->init_arg . '\'}' ) . "\n" .
141            '} ' . (!defined $default ? '' : 'else {' . "\n" .
142                 $self->meta_instance->inline_set_slot_value(
143                     '$instance',
144                     ("'" . $attr->name . "'"),
145                      $default ) . "\n" .
146            '}')
147         );
148     } elsif ( defined $default ) {
149         return (
150             $self->meta_instance->inline_set_slot_value(
151                 '$instance',
152                 ("'" . $attr->name . "'"),
153                  $default ) . "\n"
154         );
155     } else { return '' }
156 }
157
158 1;
159
160 1;
161
162 __END__
163
164 =pod
165
166 =head1 NAME
167
168 Class::MOP::Method::Constructor - Method Meta Object for constructors
169
170 =head1 SYNOPSIS
171
172   use Class::MOP::Method::Constructor;
173
174   my $constructor = Class::MOP::Method::Constructor->new(
175       metaclass => $metaclass,
176       options   => {
177           debug => 1, # this is all for now
178       },
179   );
180
181   # calling the constructor ...
182   $constructor->body->($metaclass->name, %params);
183
184 =head1 DESCRIPTION
185
186 This is a subclass of C<Class::MOP::Method> which deals with
187 class constructors. This is used when making a class immutable
188 to generate an optimized constructor.
189
190 =head1 METHODS
191
192 =over 4
193
194 =item B<new (metaclass => $meta, options => \%options)>
195
196 =item B<options>
197
198 This returns the options HASH which is passed into C<new>.
199
200 =item B<associated_metaclass>
201
202 This returns the metaclass which is passed into C<new>.
203
204 =item B<attributes>
205
206 This returns the list of attributes which are associated with the
207 metaclass which is passed into C<new>.
208
209 =item B<meta_instance>
210
211 This returns the meta instance which is associated with the
212 metaclass which is passed into C<new>.
213
214 =item B<is_inline>
215
216 This returns a boolean, but since constructors are very rarely
217 not inlined, this always returns true for now.
218
219 =item B<initialize_body>
220
221 This creates the code reference for the constructor itself.
222
223 =back
224
225 =head2 Method Generators 
226
227 =over 4
228
229 =item B<generate_constructor_method>
230
231 =item B<generate_constructor_method_inline>
232
233 =back
234
235 =head1 AUTHORS
236
237 Stevan Little E<lt>stevan@iinteractive.comE<gt>
238
239 =head1 COPYRIGHT AND LICENSE
240
241 Copyright 2006-2008 by Infinity Interactive, Inc.
242
243 L<http://www.iinteractive.com>
244
245 This library is free software; you can redistribute it and/or modify
246 it under the same terms as Perl itself.
247
248 =cut
249