tweaking a little here and there
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Constructor.pm
CommitLineData
81c8a65b 1
2package Class::MOP::Method::Constructor;
3
4use strict;
5use warnings;
6
7use Carp 'confess';
8use Scalar::Util 'blessed', 'weaken', 'looks_like_number';
9
10our $VERSION = '0.01';
11our $AUTHORITY = 'cpan:STEVAN';
12
13use base 'Class::MOP::Method';
14
15sub 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
8ea9d560 22 (blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class'))
23 || confess "You must pass a metaclass instance";
81c8a65b 24
25 my $self = bless {
26 # from our superclass
27 '&!body' => undef,
28 # specific to this subclass
8ea9d560 29 '$!metaclass' => $options{metaclass},
30 '%!options' => $options{options},
81c8a65b 31 } => $class;
32
33 # we don't want this creating
34 # a cycle in the code, if not
35 # needed
8ea9d560 36 weaken($self->{'$!metaclass'});
81c8a65b 37
38 $self->intialize_body;
39
40 return $self;
41}
42
8ea9d560 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.
49sub is_inline { 1 }
50
81c8a65b 51## accessors
52
8ea9d560 53sub options { (shift)->{'%!options'} }
54sub meta_instance { (shift)->{'$!metaclass'}->get_meta_instance }
55sub attributes { [ (shift)->{'$!metaclass'}->compute_all_applicable_attributes ] }
81c8a65b 56
57## method
58
59sub 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 }
4d048908 88 $self->{'&!body'} = $code;
81c8a65b 89}
90
91sub _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
1231;
124
1251;
126
127__END__
128
129=pod
130
131=head1 NAME
132
133Class::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
8ea9d560 145=item B<is_inline>
146
81c8a65b 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
159Stevan Little E<lt>stevan@iinteractive.comE<gt>
160
161=head1 COPYRIGHT AND LICENSE
162
163Copyright 2006 by Infinity Interactive, Inc.
164
165L<http://www.iinteractive.com>
166
167This library is free software; you can redistribute it and/or modify
168it under the same terms as Perl itself.
169
170=cut
171