die "Unknown is ${is}";
}
};
+ if (my $pred = $spec->{predicate}) {
+ quote_sub "${into}::${pred}" =>
+ ' '.$self->_generate_simple_has('$_[0]', $name)."\n"
+ ;
+ }
+ if (my $cl = $spec->{clearer}) {
+ quote_sub "${into}::${cl}" =>
+ " delete \$_[0]->{${\perlstring $name}}\n"
+ ;
+ }
quote_sub
"${into}::${name}" => ' '.$body."\n",
$self->{captures}, $quote_opts||{}
sub is_simple_attribute {
my ($self, $name, $spec) = @_;
- return !grep $spec->{$_}, qw(lazy default builder isa trigger);
+ # clearer doesn't have to be listed because it doesn't
+ # affect whether defined/exists makes a difference
+ return !grep $spec->{$_},
+ qw(lazy default builder isa trigger predicate);
}
sub _generate_get {
--- /dev/null
+use strictures 1;
+use Test::More;
+
+{
+ package Foo;
+
+ use Class::Tiny;
+
+ has one => (
+ is => 'ro', lazy => 1, default => sub { 3 },
+ predicate => 'has_one', clearer => 'clear_one'
+ );
+}
+
+my $foo = Foo->new;
+
+ok(!$foo->has_one, 'empty');
+is($foo->one, 3, 'lazy default');
+ok($foo->has_one, 'not empty now');
+is($foo->clear_one, 3, 'clearer returns value');
+ok(!$foo->has_one, 'clearer empties');
+is($foo->one, 3, 'default re-fired');
+ok($foo->has_one, 'not empty again');
+
+done_testing;