tweaking a little here and there
[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.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"; 
21         
22     (blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class'))
23         || confess "You must pass a metaclass instance";
24     
25     my $self = bless {
26         # from our superclass
27         '&!body'          => undef,
28         # specific to this subclass
29         '$!metaclass'     => $options{metaclass},
30         '%!options'       => $options{options},        
31     } => $class;
32
33     # we don't want this creating 
34     # a cycle in the code, if not 
35     # needed
36     weaken($self->{'$!metaclass'});
37
38     $self->intialize_body;
39
40     return $self;    
41 }
42
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
51 ## accessors 
52
53 sub options       {   (shift)->{'%!options'}                      }
54 sub meta_instance {   (shift)->{'$!metaclass'}->get_meta_instance }
55 sub attributes    { [ (shift)->{'$!metaclass'}->compute_all_applicable_attributes ] }
56
57 ## method
58
59 sub intialize_body {
60     my $self = shift;
61     # TODO:
62     # the %options should also include a both 
63     # a call 'initializer' and call 'SUPER::' 
64     # options, which should cover approx 90% 
65     # of the possible use cases (even if it 
66     # requires some adaption on the part of 
67     # the author, after all, nothing is free)
68     my $source = 'sub {';
69     $source .= "\n" . 'my ($class, %params) = @_;';
70     $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
71     $source .= ";\n" . (join ";\n" => map { 
72         $self->_generate_slot_initializer($_) 
73     } 0 .. (@{$self->attributes} - 1));
74     $source .= ";\n" . 'return $instance';
75     $source .= ";\n" . '}'; 
76     warn $source if $self->options->{debug};   
77     
78     my $code;
79     {
80         # NOTE:
81         # create the nessecary lexicals
82         # to be picked up in the eval 
83         my $attrs = $self->attributes;
84         
85         $code = eval $source;
86         confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
87     }
88     $self->{'&!body'} = $code;
89 }
90
91 sub _generate_slot_initializer {
92     my $self  = shift;
93     my $index = shift;
94     
95     my $attr = $self->attributes->[$index];
96     
97     my $default;
98     if ($attr->has_default) {
99         # NOTE:
100         # default values can either be CODE refs
101         # in which case we need to call them. Or 
102         # they can be scalars (strings/numbers)
103         # in which case we can just deal with them
104         # in the code we eval.
105         if ($attr->is_default_a_coderef) {
106             $default = '$attrs->[' . $index . ']->default($instance)';
107         }
108         else {
109             $default = $attr->default;
110             # make sure to quote strings ...
111             unless (looks_like_number($default)) {
112                 $default = "'$default'";
113             }
114         }
115     }
116     $self->meta_instance->inline_set_slot_value(
117         '$instance', 
118         ("'" . $attr->name . "'"), 
119         ('$params{\'' . $attr->init_arg . '\'}' . (defined $default ? (' || ' . $default) : ''))
120     );   
121 }
122
123 1;
124
125 1;
126
127 __END__
128
129 =pod
130
131 =head1 NAME 
132
133 Class::MOP::Method::Constructor - Method Meta Object for constructors
134
135 =head1 SYNOPSIS
136
137 =head1 DESCRIPTION
138
139 =head1 METHODS
140
141 =over 4
142
143 =item B<new>
144
145 =item B<is_inline>
146
147 =item B<attributes>
148
149 =item B<meta_instance>
150
151 =item B<options>
152
153 =item B<intialize_body>
154
155 =back
156
157 =head1 AUTHORS
158
159 Stevan Little E<lt>stevan@iinteractive.comE<gt>
160
161 =head1 COPYRIGHT AND LICENSE
162
163 Copyright 2006 by Infinity Interactive, Inc.
164
165 L<http://www.iinteractive.com>
166
167 This library is free software; you can redistribute it and/or modify
168 it under the same terms as Perl itself. 
169
170 =cut
171