my $container_type = $self->container_type;
my $constraint = find_type_constraint($container_type);
- $constraint = subtype('Object', where { $_->isa($container_type) })
- unless $constraint;
+ $constraint = subtype(
+ 'Object',
+ sub {
+ $_->isa($container_type) || ($_->can('does') && $_->does($container_type))
+ }
+ ) unless $constraint;
return $constraint;
}
};
no Moose;
+no Moose::Util::TypeConstraints;
1;
'empty' => 'has_options',
}, '... got the right provies mapping');
-
+is($options->container_type, 'Int', '... got the right container type');
has 'options' => (
metaclass => 'Collection::Hash',
is => 'ro',
- isa => 'HashRef',
+ isa => 'HashRef[Str]',
default => sub { {} },
provides => {
- 'set' => 'set_option',
- 'get' => 'get_option',
+ 'set' => 'set_option',
+ 'get' => 'get_option',
+ 'empty' => 'has_options',
+ 'count' => 'num_options',
}
);
}
my $stuff = Stuff->new();
isa_ok($stuff, 'Stuff');
+can_ok($stuff, $_) for qw[
+ set_option
+ get_option
+ has_options
+ num_options
+];
+
+ok(!$stuff->has_options, '... we have no options');
+is($stuff->num_options, 0, '... we have no options');
+
is_deeply($stuff->options, {}, '... no options yet');
$stuff->set_option(foo => 'bar');
+
+ok($stuff->has_options, '... we have options');
+is($stuff->num_options, 1, '... we have 1 option(s)');
is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
$stuff->set_option(bar => 'baz');
+
+is($stuff->num_options, 2, '... we have 2 option(s)');
is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
is($stuff->get_option('foo'), 'bar', '... got the right option');
has observers => (
metaclass => 'Collection::Array',
is => 'ro',
- isa => 'ArrayRef',
+ isa => 'ArrayRef[Observer]',
auto_deref => 1,
default => sub { [] },
provides => { 'push' => 'add_observer', count => 'count_observers' }
use Moose::Role;
-sub update {
- die 'Forgot to implement' . "\n";
-}
+requires 'update';
###############################################################################