if (my $cl = $spec->{clearer}) {
$methods{$cl} =
quote_sub "${into}::${cl}" =>
- " delete \$_[0]->{${\perlstring $name}}\n"
+ $self->_generate_simple_clear('$_[0]', $name)."\n"
;
}
if (my $hspec = $spec->{handles}) {
"exists ${me}->{${\perlstring $name}}";
}
+sub _generate_simple_clear {
+ my ($self, $me, $name) = @_;
+ " delete ${me}->{${\perlstring $name}}\n"
+}
+
sub generate_get_default {
my $self = shift;
$self->{captures} = {};
$into->can($name);
}
+sub default_construction_string { '{}' }
+
1;
use B 'perlstring';
sub register_attribute_specs {
- my ($self, %spec) = @_;
- @{$self->{attribute_specs}||={}}{keys %spec} = values %spec;
+ my ($self, @new_specs) = @_;
+ my $specs = $self->{attribute_specs}||={};
+ while (my ($name, $new_spec) = splice @new_specs, 0, 2) {
+ $new_spec->{index} = scalar keys %$specs
+ unless exists $new_spec->{index};
+ $specs->{$name} = $new_spec;
+ }
$self;
}
sub construction_string {
my ($self) = @_;
- $self->{construction_string} or 'bless({}, $class);'
+ $self->{construction_string}
+ or 'bless('
+ .$self->accessor_generator->default_construction_string
+ .', $class);'
}
sub install_delayed {
$MAKERS{$target} = {};
_install_coderef "${target}::has" => sub {
my ($name, %spec) = @_;
- ($MAKERS{$target}{accessor} ||= do {
- require Method::Generate::Accessor;
- Method::Generate::Accessor->new
- })->generate_method($target, $name, \%spec);
$class->_constructor_maker_for($target)
->register_attribute_specs($name, \%spec);
+ $class->_accessor_maker_for($target)
+ ->generate_method($target, $name, \%spec);
};
foreach my $type (qw(before after around)) {
_install_coderef "${target}::${type}" => sub {
}
}
+sub _accessor_maker_for {
+ my ($class, $target) = @_;
+ return unless $MAKERS{$target};
+ $MAKERS{$target}{accessor} ||= do {
+ my $maker_class = do {
+ if (my $m = do {
+ if (my $defer_target =
+ (Sub::Defer::defer_info($target->can('new'))||[])->[0]
+ ) {
+ my ($pkg) = ($defer_target =~ /^(.*)::[^:]+$/);
+ $MAKERS{$pkg} && $MAKERS{$pkg}{accessor};
+ } else {
+ undef;
+ }
+ }) {
+ ref($m);
+ } else {
+ require Method::Generate::Accessor;
+ 'Method::Generate::Accessor'
+ }
+ };
+ $maker_class->new;
+ }
+}
+
sub _constructor_maker_for {
my ($class, $target, $select_super) = @_;
return unless $MAKERS{$target};
$moo_constructor = 1; # no other constructor, make a Moo one
}
};
- Method::Generate::Constructor
+ ($con ? ref($con) : 'Method::Generate::Constructor')
->new(
package => $target,
- accessor_generator => do {
- require Method::Generate::Accessor;
- Method::Generate::Accessor->new;
- },
+ accessor_generator => $class->_accessor_maker_for($target),
construction_string => (
$moo_constructor
? ($con ? $con->construction_string : undef)