Able to use strings rather than having to use hash refs
t0m [Sun, 7 Jun 2009 02:35:26 +0000 (03:35 +0100)]
lib/CatalystX/DynamicComponent.pm
t/03_dynamiccomponent.t

index cb35da4..a8e489c 100644 (file)
@@ -4,6 +4,7 @@ use MooseX::Types::Moose qw/Str CodeRef HashRef ArrayRef/;
 use Catalyst::Utils;
 use Moose::Util::TypeConstraints;
 use List::MoreUtils qw/uniq/;
+use Moose::Autobox;
 use namespace::autoclean;
 
 enum __PACKAGE__ . '::ResolveStrategy' => qw/
@@ -23,6 +24,9 @@ parameter 'pre_immutable_hook' => (
     predicate => 'has_pre_immutable_hook',
 );
 
+my $coerceablearray = subtype ArrayRef;
+coerce $coerceablearray, from Str, via { [ $_ ] };
+
 my %parameters = (
     methods => {
         isa =>HashRef, 
@@ -30,12 +34,12 @@ my %parameters = (
         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',
     },
@@ -55,9 +59,15 @@ foreach my $name (keys %parameters) {
 
 # 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 {
index 77be38d..b4037b2 100644 (file)
@@ -1,7 +1,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 32;
+use Test::More tests => 38;
 use Test::Exception;
 
 use Moose ();
@@ -182,4 +182,45 @@ my $extra_config = {
     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';
+}