subtype 'A2', as 'ArrayRef', where { @$_ < 2 };
subtype 'A3', as 'ArrayRef[Int]', where { ( sum(@$_) || 0 ) < 5 };
+ subtype 'A5', as 'ArrayRef';
+ coerce 'A5', from 'Str', via { [ $_ ] };
+
no Moose::Util::TypeConstraints;
}
push_a3 => 'push',
},
);
-
has a4 => (
traits => ['Array'],
is => 'rw',
default => 'invalid',
clearer => '_clear_a4',
handles => {
- get_a4 => 'get',
+ get_a4 => 'get',
push_a4 => 'push',
accessor_a4 => 'accessor',
},
);
+ has a5 => (
+ traits => ['Array'],
+ is => 'rw',
+ isa => 'A5',
+ coerce => 1,
+ lazy => 1,
+ default => 'invalid',
+ clearer => '_clear_a5',
+ handles => {
+ get_a5 => 'get',
+ push_a5 => 'push',
+ accessor_a5 => 'accessor',
+ },
+ );
}
my $foo = Foo->new;
);
}
+{
+ my $foo = Foo->new;
+
+ is(
+ $foo->accessor_a5(0), 'invalid',
+ 'lazy default is coerced when trying to read via accessor'
+ );
+
+ $foo->_clear_a5;
+
+ $foo->accessor_a5( 1 => 'thing' );
+
+ is_deeply(
+ $foo->a5,
+ [ 'invalid', 'thing' ],
+ 'lazy default is coerced when trying to write via accessor'
+ );
+
+ $foo->_clear_a5;
+
+ $foo->push_a5('thing');
+
+ is_deeply(
+ $foo->a5,
+ [ 'invalid', 'thing' ],
+ 'lazy default is coerced when trying to push'
+ );
+
+ $foo->_clear_a5;
+
+ is(
+ $foo->get_a5(0), 'invalid',
+ 'lazy default is coerced when trying to get'
+ );
+}
+
done_testing;