use Catalyst::Utils;
use Moose::Util::TypeConstraints;
use List::MoreUtils qw/uniq/;
+use Moose::Autobox;
use namespace::autoclean;
enum __PACKAGE__ . '::ResolveStrategy' => qw/
predicate => 'has_pre_immutable_hook',
);
+my $coerceablearray = subtype ArrayRef;
+coerce $coerceablearray, from Str, via { [ $_ ] };
+
my %parameters = (
methods => {
isa =>HashRef,
resolve_strategy => 'merge',
},
roles => {
- isa => ArrayRef,
+ isa => $coerceablearray, coerce => 1,
default => sub { [] },
resolve_strategy => 'merge',
},
superclasses => {
- isa => ArrayRef,
+ isa => $coerceablearray, coerce => 1,
default => sub { [] },
resolve_strategy => 'replace',
},
# Code refs to implement the strategy types
my %strategies = ( # Right hand precedence where appropriate
- replace => sub { $_[1] ? $_[1] : $_[0]; },
+ replace => sub {
+ $_[0] = [ $_[0] ] if $_[0] && !ref $_[0];
+ $_[1] = [ $_[1] ] if $_[1] && !ref $_[1];
+ $_[1] ? $_[1] : $_[0];
+ },
merge => sub {
- if (ref($_[0]) eq 'ARRAY') {
+ $_[0] = [ $_[0] ] if $_[0] && !ref $_[0];
+ $_[1] = [ $_[1] ] if $_[1] && !ref $_[1];
+ if (ref($_[0]) eq 'ARRAY' || ref($_[1]) eq 'ARRAY') {
[ uniq( @{ $_[0] }, @{ $_[1] } ) ];
}
else {
use strict;
use warnings;
-use Test::More tests => 32;
+use Test::More tests => 38;
use Test::Exception;
use Moose ();
ok !$model->can('_some_method_from_other_role'),
'Role application replaces as set to []';
}
+{
+ # Test that base strings get listified.
+ my $app_meta = generate_testapp({
+ %generator_config,
+ });
+
+ my $app = $app_meta->name;
+ $app->dynamic_component_method( "Model::Foo", { superclasses => 'My::Model', roles => 'My::Role', } );
+ my $model = $app->model('Foo');
+
+ ok $model->can('my_other_injected_method'),
+ 'Injected method present';
+
+ ok(!$model->isa('My::Other::Superclass'),
+ 'superclasses replaced');
+
+ ok $model->can('_some_method_from_other_role'),
+ 'Role application merges';
+}
+
+{
+ # Test that base strings get listified.
+ my $app_meta = generate_testapp({
+ %generator_config,
+ roles => 'My::Other::Role',
+ superclasses => 'My::Other::Superclass',
+ });
+
+ my $app = $app_meta->name;
+ $app->dynamic_component_method( "Model::Foo", { } );
+ my $model = $app->model('Foo');
+
+ ok $model->can('my_other_injected_method'),
+ 'Injected method present';
+
+ ok($model->isa('My::Other::Superclass'),
+ 'superclasses from string');
+
+ ok $model->can('_some_method_from_other_role'),
+ 'Role application merges';
+}