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