+ - Support builder => sub {} ala MooseX::AttributeShortcuts
- Fix 'no Moo;' to preserve non-sub package variables
- Switch to testing for Mouse::Util->can('find_meta') to avoid
exploding on ancient Mouse installs
} elsif ($is ne 'bare') {
die "Unknown is ${is}";
}
- $spec->{builder} = '_build_'.$name if ($spec->{builder}||0) eq 1;
- die "Invalid builder for $into->$name - not a valid method name"
- if exists $spec->{builder} and (ref $spec->{builder}
- or $spec->{builder} !~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/);
+ if (exists $spec->{builder}) {
+ if(ref $spec->{builder}) {
+ die "Invalid builder for $into->$name - not a method name, coderef or"
+ . " code-convertible object"
+ unless ref $spec->{builder} eq 'CODE'
+ or (blessed($spec->{builder}) and eval { \&{$spec->{builder}} });
+ $spec->{builder_sub} = $spec->{builder};
+ $spec->{builder} = 1;
+ }
+ $spec->{builder} = '_build_'.$name if ($spec->{builder}||0) eq 1;
+ die "Invalid builder for $into->$name - not a valid method name"
+ if $spec->{builder} !~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/;
+ }
if (($spec->{predicate}||0) eq 1) {
$spec->{predicate} = $name =~ /^_/ ? "_has${name}" : "has_${name}";
}
' '.$self->_generate_simple_has('$_[0]', $name, $spec)."\n"
;
}
+ if (my $pred = $spec->{builder_sub}) {
+ _install_coderef( "${into}::$spec->{builder}" => $spec->{builder_sub} );
+ }
if (my $cl = $spec->{clearer}) {
$methods{$cl} =
quote_sub "${into}::${cl}" =>
$self->$builder;
+The following features come from L<MooseX::AttributeShortcuts>:
+
If you set this to just C<1>, the builder is automatically named
-C<_build_${attr_name}>. This feature comes from L<MooseX::AttributeShortcuts>.
+C<_build_${attr_name}>.
+
+If you set this to a coderef or code-convertible object, that variable will be
+installed under C<$class::_build_${attr_name}> and the builder set to the same
+name.
=item * C<clearer>
use Test::Fatal;
use Method::Generate::Accessor;
+use Sub::Quote 'quote_sub';
my $gen = Method::Generate::Accessor->new;
undef, 'builder - string accepted',
);
-like(
+is(
exception { $gen->generate_method('Foo' => 'eleven' => { is => 'ro', builder => sub {} }) },
- qr/Invalid builder/, 'builder - coderef rejected'
+ undef, 'builder - coderef accepted'
);
like(
undef, 'builder - fully-qualified name accepted',
);
+is(
+ exception { $gen->generate_method('Foo' => 'fifteen' => { is => 'lazy', builder => sub {15} }) },
+ undef, 'builder - coderef accepted'
+);
+
+is(
+ exception { $gen->generate_method('Foo' => 'sixteen' => { is => 'lazy', builder => quote_sub q{ 16 } }) },
+ undef, 'builder - quote_sub accepted'
+);
+
my $foo = Foo->new(one => 1);
is($foo->one, 1, 'ro reads');
$foo->two(-3);
is($foo->two, -3, 'rw writes');
+is($foo->fifteen, 15, 'builder installs code sub');
+is($foo->_build_fifteen, 15, 'builder installs code sub under the correct name');
+
+is($foo->sixteen, 16, 'builder installs quote_sub');
+
done_testing;