2 package Class::MOP::Method::Constructor;
8 use Scalar::Util 'blessed', 'weaken';
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Class::MOP::Method::Inlined';
19 (blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class'))
20 || confess "You must pass a metaclass instance if you want to inline"
21 if $options{is_inline};
23 ($options{package_name} && $options{name})
24 || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
26 my $self = $class->_new(\%options);
28 # we don't want this creating
29 # a cycle in the code, if not
31 weaken($self->{'associated_metaclass'});
33 $self->_initialize_body;
41 return Class::MOP::Class->initialize($class)->new_object(@_)
42 if $class ne __PACKAGE__;
44 my $params = @_ == 1 ? $_[0] : {@_};
47 # inherited from Class::MOP::Method
48 body => $params->{body},
49 # associated_metaclass => $params->{associated_metaclass}, # overriden
50 package_name => $params->{package_name},
51 name => $params->{name},
52 original_method => $params->{original_method},
54 # inherited from Class::MOP::Generated
55 is_inline => $params->{is_inline} || 0,
56 definition_context => $params->{definition_context},
58 # inherited from Class::MOP::Inlined
59 _expected_method_class => $params->{_expected_method_class},
61 # defined in this subclass
62 options => $params->{options} || {},
63 associated_metaclass => $params->{metaclass},
69 sub options { (shift)->{'options'} }
70 sub associated_metaclass { (shift)->{'associated_metaclass'} }
76 $self->{'attributes'} ||= [
77 sort { $a->name cmp $b->name }
78 $self->associated_metaclass->get_all_attributes
84 sub _initialize_body {
86 my $method_name = '_generate_constructor_method';
88 $method_name .= '_inline' if $self->is_inline;
90 $self->{'body'} = $self->$method_name;
93 sub _eval_environment {
95 my $defaults = [map { $_->default } @{ $self->_attributes }];
97 '$defaults' => \$defaults,
101 sub _generate_constructor_method {
102 return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
105 sub _generate_constructor_method_inline {
108 my $meta = $self->associated_metaclass;
112 $meta->_inline_new_object,
116 warn join("\n", @source) if $self->options->{debug};
119 $self->_compile_code(\@source);
122 my $source = join("\n", @source);
123 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$_";
131 # ABSTRACT: Method Meta Object for constructors
139 use Class::MOP::Method::Constructor;
141 my $constructor = Class::MOP::Method::Constructor->new(
142 metaclass => $metaclass,
144 debug => 1, # this is all for now
148 # calling the constructor ...
149 $constructor->body->execute($metaclass->name, %params);
153 This is a subclass of C<Class::MOP::Method> which generates
160 =item B<< Class::MOP::Method::Constructor->new(%options) >>
162 This creates a new constructor object. It accepts a hash reference of
169 This should be a L<Class::MOP::Class> object. It is required.
173 The method name (without a package name). This is required.
177 The package name for the method. This is required.
181 This indicates whether or not the constructor should be inlined. This
186 =item B<< $metamethod->is_inline >>
188 Returns a boolean indicating whether or not the constructor is
191 =item B<< $metamethod->associated_metaclass >>
193 This returns the L<Class::MOP::Class> object for the method.