need to use a consistent sort order, now that this isn't cached
[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';
9 use Try::Tiny;
10
11 our $VERSION   = '1.11';
12 $VERSION = eval $VERSION;
13 our $AUTHORITY = 'cpan:STEVAN';
14
15 use base 'Class::MOP::Method::Inlined';
16
17 sub new {
18     my $class   = shift;
19     my %options = @_;
20
21     (blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class'))
22         || confess "You must pass a metaclass instance if you want to inline"
23             if $options{is_inline};
24
25     ($options{package_name} && $options{name})
26         || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
27
28     my $self = $class->_new(\%options);
29
30     # we don't want this creating
31     # a cycle in the code, if not
32     # needed
33     weaken($self->{'associated_metaclass'});
34
35     $self->_initialize_body;
36
37     return $self;
38 }
39
40 sub _new {
41     my $class = shift;
42
43     return Class::MOP::Class->initialize($class)->new_object(@_)
44         if $class ne __PACKAGE__;
45
46     my $params = @_ == 1 ? $_[0] : {@_};
47
48     return bless {
49         # inherited from Class::MOP::Method
50         body                 => $params->{body},
51         # associated_metaclass => $params->{associated_metaclass}, # overriden
52         package_name         => $params->{package_name},
53         name                 => $params->{name},
54         original_method      => $params->{original_method},
55
56         # inherited from Class::MOP::Generated
57         is_inline            => $params->{is_inline} || 0,
58         definition_context   => $params->{definition_context},
59
60         # inherited from Class::MOP::Inlined
61         _expected_method_class => $params->{_expected_method_class},
62
63         # defined in this subclass
64         options              => $params->{options} || {},
65         associated_metaclass => $params->{metaclass},
66     }, $class;
67 }
68
69 ## accessors
70
71 sub options              { (shift)->{'options'}              }
72 sub associated_metaclass { (shift)->{'associated_metaclass'} }
73
74 ## cached values ...
75
76 sub _attributes {
77     my $self = shift;
78     $self->{'attributes'} ||= [
79         sort { $a->name cmp $b->name }
80              $self->associated_metaclass->get_all_attributes
81     ]
82 }
83
84 ## method
85
86 sub _initialize_body {
87     my $self        = shift;
88     my $method_name = '_generate_constructor_method';
89
90     $method_name .= '_inline' if $self->is_inline;
91
92     $self->{'body'} = $self->$method_name;
93 }
94
95 sub _eval_environment {
96     my $self = shift;
97     my $defaults = [map { $_->default } @{ $self->_attributes }];
98     return {
99         '$defaults' => \$defaults,
100     };
101 }
102
103 sub _generate_constructor_method {
104     return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
105 }
106
107 sub _generate_constructor_method_inline {
108     my $self = shift;
109
110     my $meta = $self->associated_metaclass;
111
112     my @source = (
113         'sub {',
114             $meta->_inline_new_object,
115         '}',
116     );
117
118     warn join("\n", @source) if $self->options->{debug};
119
120     my $code = try {
121         $self->_compile_code(\@source);
122     }
123     catch {
124         my $source = join("\n", @source);
125         confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$_";
126     };
127
128     return $code;
129 }
130
131 1;
132
133 __END__
134
135 =pod
136
137 =head1 NAME
138
139 Class::MOP::Method::Constructor - Method Meta Object for constructors
140
141 =head1 SYNOPSIS
142
143   use Class::MOP::Method::Constructor;
144
145   my $constructor = Class::MOP::Method::Constructor->new(
146       metaclass => $metaclass,
147       options   => {
148           debug => 1, # this is all for now
149       },
150   );
151
152   # calling the constructor ...
153   $constructor->body->execute($metaclass->name, %params);
154
155 =head1 DESCRIPTION
156
157 This is a subclass of C<Class::MOP::Method> which generates
158 constructor methods.
159
160 =head1 METHODS
161
162 =over 4
163
164 =item B<< Class::MOP::Method::Constructor->new(%options) >>
165
166 This creates a new constructor object. It accepts a hash reference of
167 options.
168
169 =over 8
170
171 =item * metaclass
172
173 This should be a L<Class::MOP::Class> object. It is required.
174
175 =item * name
176
177 The method name (without a package name). This is required.
178
179 =item * package_name
180
181 The package name for the method. This is required.
182
183 =item * is_inline
184
185 This indicates whether or not the constructor should be inlined. This
186 defaults to false.
187
188 =back
189
190 =item B<< $metamethod->is_inline >>
191
192 Returns a boolean indicating whether or not the constructor is
193 inlined.
194
195 =item B<< $metamethod->associated_metaclass >>
196
197 This returns the L<Class::MOP::Class> object for the method.
198
199 =back
200
201 =head1 AUTHORS
202
203 Stevan Little E<lt>stevan@iinteractive.comE<gt>
204
205 =head1 COPYRIGHT AND LICENSE
206
207 Copyright 2006-2010 by Infinity Interactive, Inc.
208
209 L<http://www.iinteractive.com>
210
211 This library is free software; you can redistribute it and/or modify
212 it under the same terms as Perl itself.
213
214 =cut
215