From: t0m Date: Sun, 7 Jun 2009 02:35:26 +0000 (+0100) Subject: Able to use strings rather than having to use hash refs X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalystX-DynamicComponent.git;a=commitdiff_plain;h=72b522424833b0e7ecf67dda4e307396cd0f25ce Able to use strings rather than having to use hash refs --- diff --git a/lib/CatalystX/DynamicComponent.pm b/lib/CatalystX/DynamicComponent.pm index cb35da4..a8e489c 100644 --- a/lib/CatalystX/DynamicComponent.pm +++ b/lib/CatalystX/DynamicComponent.pm @@ -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 { diff --git a/t/03_dynamiccomponent.t b/t/03_dynamiccomponent.t index 77be38d..b4037b2 100644 --- a/t/03_dynamiccomponent.t +++ b/t/03_dynamiccomponent.t @@ -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'; +}