Commit | Line | Data |
d90b42a6 |
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{meta_instance} && $options{meta_instance}->isa('Class::MOP::Instance')) |
23 | || confess "You must supply a meta-instance"; |
24 | |
25 | (exists $options{attributes} && ref $options{attributes} eq 'ARRAY') |
26 | || confess "You must pass an array of options"; |
27 | |
28 | (blessed($_) && $_->isa('Class::MOP::Attribute')) |
29 | || confess "You must supply a list of attributes which is a 'Class::MOP::Attribute' instance" |
30 | for @{$options{attributes}}; |
31 | |
32 | my $self = bless { |
33 | # from our superclass |
34 | body => undef, |
35 | # specific to this subclass |
36 | options => $options{options}, |
37 | meta_instance => $options{meta_instance}, |
38 | attributes => $options{attributes}, |
39 | } => $class; |
40 | |
41 | # we don't want this creating |
42 | # a cycle in the code, if not |
43 | # needed |
44 | weaken($self->{meta_instance}); |
45 | |
46 | $self->intialize_body; |
47 | |
48 | return $self; |
49 | } |
50 | |
51 | ## accessors |
52 | |
53 | sub options { (shift)->{options} } |
54 | sub meta_instance { (shift)->{meta_instance} } |
55 | sub attributes { (shift)->{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<attributes> |
146 | |
147 | =item B<meta_instance> |
148 | |
149 | =item B<options> |
150 | |
151 | =item B<intialize_body> |
152 | |
153 | =back |
154 | |
155 | =head1 AUTHORS |
156 | |
157 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
158 | |
159 | =head1 COPYRIGHT AND LICENSE |
160 | |
161 | Copyright 2006 by Infinity Interactive, Inc. |
162 | |
163 | L<http://www.iinteractive.com> |
164 | |
165 | This library is free software; you can redistribute it and/or modify |
166 | it under the same terms as Perl itself. |
167 | |
168 | =cut |
169 | |