2 package Class::MOP::Method::Constructor;
8 use Scalar::Util 'blessed', 'weaken';
11 use base 'Class::MOP::Method::Inlined';
17 (blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class'))
18 || confess "You must pass a metaclass instance if you want to inline"
19 if $options{is_inline};
21 ($options{package_name} && $options{name})
22 || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
24 my $self = $class->_new(\%options);
26 # we don't want this creating
27 # a cycle in the code, if not
29 weaken($self->{'associated_metaclass'});
31 $self->_initialize_body;
39 return Class::MOP::Class->initialize($class)->new_object(@_)
40 if $class ne __PACKAGE__;
42 my $params = @_ == 1 ? $_[0] : {@_};
45 # inherited from Class::MOP::Method
46 body => $params->{body},
47 # associated_metaclass => $params->{associated_metaclass}, # overriden
48 package_name => $params->{package_name},
49 name => $params->{name},
50 original_method => $params->{original_method},
52 # inherited from Class::MOP::Generated
53 is_inline => $params->{is_inline} || 0,
54 definition_context => $params->{definition_context},
56 # inherited from Class::MOP::Inlined
57 _expected_method_class => $params->{_expected_method_class},
59 # defined in this subclass
60 options => $params->{options} || {},
61 associated_metaclass => $params->{metaclass},
67 sub options { (shift)->{'options'} }
68 sub associated_metaclass { (shift)->{'associated_metaclass'} }
74 $self->{'attributes'} ||= [
75 sort { $a->name cmp $b->name }
76 $self->associated_metaclass->get_all_attributes
82 sub _initialize_body {
84 my $method_name = '_generate_constructor_method';
86 $method_name .= '_inline' if $self->is_inline;
88 $self->{'body'} = $self->$method_name;
91 sub _eval_environment {
93 my $defaults = [map { $_->default } @{ $self->_attributes }];
95 '$defaults' => \$defaults,
99 sub _generate_constructor_method {
100 return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
103 sub _generate_constructor_method_inline {
106 my $meta = $self->associated_metaclass;
110 $meta->_inline_new_object,
114 warn join("\n", @source) if $self->options->{debug};
117 $self->_compile_code(\@source);
120 my $source = join("\n", @source);
121 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$_";
129 # ABSTRACT: Method Meta Object for constructors
137 use Class::MOP::Method::Constructor;
139 my $constructor = Class::MOP::Method::Constructor->new(
140 metaclass => $metaclass,
142 debug => 1, # this is all for now
146 # calling the constructor ...
147 $constructor->body->execute($metaclass->name, %params);
151 This is a subclass of C<Class::MOP::Method> which generates
158 =item B<< Class::MOP::Method::Constructor->new(%options) >>
160 This creates a new constructor object. It accepts a hash reference of
167 This should be a L<Class::MOP::Class> object. It is required.
171 The method name (without a package name). This is required.
175 The package name for the method. This is required.
179 This indicates whether or not the constructor should be inlined. This
184 =item B<< $metamethod->is_inline >>
186 Returns a boolean indicating whether or not the constructor is
189 =item B<< $metamethod->associated_metaclass >>
191 This returns the L<Class::MOP::Class> object for the method.