);
Class::MOP::Attribute->meta->add_attribute(
+ Class::MOP::Attribute->new('definition_context' => (
+ reader => { 'definition_context' => \&Class::MOP::Attribute::definition_context },
+ ))
+);
+
+Class::MOP::Attribute->meta->add_attribute(
Class::MOP::Attribute->new('writer' => (
reader => { 'writer' => \&Class::MOP::Attribute::writer },
predicate => { 'has_writer' => \&Class::MOP::Attribute::has_writer },
))
);
+Class::MOP::Method::Generated->meta->add_attribute(
+ Class::MOP::Attribute->new('definition_context' => (
+ reader => { 'definition_context' => \&Class::MOP::Method::Generated::definition_context },
+ ))
+);
+
## --------------------------------------------------------
## Class::MOP::Method::Accessor
my $options = @_ == 1 ? $_[0] : {@_};
bless {
- 'name' => $options->{name},
- 'accessor' => $options->{accessor},
- 'reader' => $options->{reader},
- 'writer' => $options->{writer},
- 'predicate' => $options->{predicate},
- 'clearer' => $options->{clearer},
- 'builder' => $options->{builder},
- 'init_arg' => $options->{init_arg},
- 'default' => $options->{default},
- 'initializer' => $options->{initializer},
+ 'name' => $options->{name},
+ 'accessor' => $options->{accessor},
+ 'reader' => $options->{reader},
+ 'writer' => $options->{writer},
+ 'predicate' => $options->{predicate},
+ 'clearer' => $options->{clearer},
+ 'builder' => $options->{builder},
+ 'init_arg' => $options->{init_arg},
+ 'default' => $options->{default},
+ 'initializer' => $options->{initializer},
+ 'definition_context' => $options->{definition_context},
# keep a weakened link to the
# class we are associated with
'associated_class' => undef,
sub has_default { defined($_[0]->{'default'}) }
sub has_initializer { defined($_[0]->{'initializer'}) }
-sub accessor { $_[0]->{'accessor'} }
-sub reader { $_[0]->{'reader'} }
-sub writer { $_[0]->{'writer'} }
-sub predicate { $_[0]->{'predicate'} }
-sub clearer { $_[0]->{'clearer'} }
-sub builder { $_[0]->{'builder'} }
-sub init_arg { $_[0]->{'init_arg'} }
-sub initializer { $_[0]->{'initializer'} }
+sub accessor { $_[0]->{'accessor'} }
+sub reader { $_[0]->{'reader'} }
+sub writer { $_[0]->{'writer'} }
+sub predicate { $_[0]->{'predicate'} }
+sub clearer { $_[0]->{'clearer'} }
+sub builder { $_[0]->{'builder'} }
+sub init_arg { $_[0]->{'init_arg'} }
+sub initializer { $_[0]->{'initializer'} }
+sub definition_context { $_[0]->{'definition_context'} }
# end bootstrapped away method section.
# (all methods below here are kept intact)
sub process_accessors {
my ($self, $type, $accessor, $generate_as_inline_methods) = @_;
+
+ my $method_ctx;
+
+ if ( my $ctx = $self->definition_context ) {
+ $method_ctx = { %$ctx };
+ }
+
if (ref($accessor)) {
(ref($accessor) eq 'HASH')
|| confess "bad accessor/reader/writer/predicate/clearer format, must be a HASH ref";
$method,
package_name => $self->associated_class->name,
name => $name,
+ definition_context => $method_ctx,
);
$self->associate_method($method);
return ($name, $method);
my $inline_me = ($generate_as_inline_methods && $self->associated_class->instance_metaclass->is_inlinable);
my $method;
eval {
+ if ( $method_ctx ) {
+ my $desc = "accessor $accessor";
+ if ( $accessor ne $self->name ) {
+ $desc .= " of attribute " . $self->name;
+ }
+
+ $method_ctx->{description} = $desc;
+ }
+
$method = $self->accessor_metaclass->new(
attribute => $self,
is_inline => $inline_me,
accessor_type => $type,
package_name => $self->associated_class->name,
name => $accessor,
+ definition_context => $method_ctx,
);
};
confess "Could not create the '$type' method for " . $self->name . " because : $@" if $@;
return $self;
}
+
+sub _prepare_code {
+ my ( $self, %args ) = @_;
+
+ my ( $line, $file );
+
+ if ( my $ctx = ( $args{context} || $self->definition_context ) ) {
+ $line = $ctx->{line};
+ if ( my $desc = $ctx->{description} ) {
+ $file = "$desc defined at $ctx->{file}";
+ } else {
+ $file = $ctx->{file};
+ }
+ } else {
+ ( $line, $file ) = ( 0, "generated method (unknown origin)" );
+ }
+
+ my $code = $args{code};
+
+ # if it's an array of lines, join it up
+ # don't use newlines so that the definition context is more meaningful
+ $code = join(@$code, ' ') if ref $code;
+
+ return qq{#line $line "$file"\n} . $code;
+}
+
sub _new {
my $class = shift;
my $options = @_ == 1 ? $_[0] : {@_};
## accessors
-sub is_inline { (shift)->{'is_inline'} }
+sub is_inline { $_[0]{is_inline} }
+
+sub definition_context { $_[0]{definition_context} }
sub initialize_body {
confess "No body to initialize, " . __PACKAGE__ . " is an abstract base class";
}
-
1;
__END__