From: Tomas Doran (t0m) Date: Mon, 25 May 2009 19:57:30 +0000 (+0100) Subject: Update DynamicComponent so that you just ask for 'Model::Foo' not 'MyApp::Model:... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=96e54fc3183408e78ded3e7ee86d0b8e4b45d8f0;hp=1efcc5a1ce645fea9458e0bd50aa83911084f367;p=catagits%2FCatalystX-DynamicComponent.git Update DynamicComponent so that you just ask for 'Model::Foo' not 'MyApp::Model::Foo', as the component generator is called on anyway so can find 'MyApp for itself --- diff --git a/lib/CatalystX/DynamicComponent.pm b/lib/CatalystX/DynamicComponent.pm index 3207802..b99e35d 100644 --- a/lib/CatalystX/DynamicComponent.pm +++ b/lib/CatalystX/DynamicComponent.pm @@ -86,16 +86,17 @@ role { $config ||= {}; my $appclass = blessed($app) || $app; + my $type = $name; - $type =~ s/^${appclass}:://; # FIXME - I think there is shit in C::Utils to do this. $type =~ s/::.*$//; + $name = $appclass . '::' . $name; my $meta = Moose->init_meta( for_class => $name ); my @superclasses = @{ $get_resolved_config->('superclasses', $p, $config) }; push(@superclasses, 'Catalyst::' . $type) unless @superclasses; $meta->superclasses(@superclasses); - + my $methods = $get_resolved_config->('methods', $p, $config); foreach my $name (keys %$methods) { $meta->add_method($name => $methods->{$name}); @@ -104,7 +105,7 @@ role { if (my @roles = @{ $get_resolved_config->('roles', $p, $config) }) { Moose::Util::apply_all_roles( $name, @roles); } - + if ($p->has_pre_immutable_hook) { if (!ref($pre_immutable_hook)) { $app->$pre_immutable_hook($meta, $config); diff --git a/lib/CatalystX/DynamicComponent/ModelToControllerReflector.pm b/lib/CatalystX/DynamicComponent/ModelToControllerReflector.pm index 64da97f..a180285 100644 --- a/lib/CatalystX/DynamicComponent/ModelToControllerReflector.pm +++ b/lib/CatalystX/DynamicComponent/ModelToControllerReflector.pm @@ -36,7 +36,7 @@ sub _reflect_model_to_controller { my $class = blessed($app) || $app; my $controller_name = $model_name; - $controller_name =~ s/::Model::/::Controller::/; + $controller_name =~ s/^.*::Model::/Controller::/; my $suffix = $model_name; $suffix =~ s/^.*::Model:://; @@ -51,12 +51,9 @@ sub _reflect_model_to_controller { $controller_methods{$method_name} = $app->generate_reflected_controller_action_method($suffix, $model_methods->{$method_name}) } - my $config_name = $controller_name; - $config_name =~ s/^[^:]+:://; - # Shallow copy so we don't stuff method refs in config - my $config = { %{$app->config->{$config_name}||{}} }; - + my $config = { %{$app->config->{$controller_name}||{}} }; + $config->{methods} = \%controller_methods; $app->_setup_dynamic_controller( $controller_name, $config ); } diff --git a/lib/CatalystX/DynamicComponent/ModelsFromConfig.pm b/lib/CatalystX/DynamicComponent/ModelsFromConfig.pm index da35555..62e2c80 100644 --- a/lib/CatalystX/DynamicComponent/ModelsFromConfig.pm +++ b/lib/CatalystX/DynamicComponent/ModelsFromConfig.pm @@ -29,7 +29,6 @@ after 'setup_components' => sub { shift->_setup_dynamic_models(@_); }; sub _setup_dynamic_models { my ($app) = @_; - my $app_name = blessed($app) || $app; my $model_prefix = 'Model::'; my $config = $app->config || {}; @@ -43,9 +42,7 @@ sub _setup_dynamic_models { next if $model_name =~ /$exc/; } - my $model_class_name = $app_name . '::' . $model_name; - - $app->_setup_dynamic_model( $model_class_name, $config->{$model_name} ); + $app->_setup_dynamic_model( $model_name, $config->{$model_name} ); } } diff --git a/t/03_dynamiccomponent.t b/t/03_dynamiccomponent.t index 33cd878..77be38d 100644 --- a/t/03_dynamiccomponent.t +++ b/t/03_dynamiccomponent.t @@ -24,7 +24,7 @@ throws_ok { generate_testapp(); } my $app_meta = generate_testapp({ name => 'dynamic_component_method' }); my $app = $app_meta->name; ok $app->can('dynamic_component_method'), 'dynamic component method added'; - $app->dynamic_component_method( $app . "::Model::Foo", {} ); + $app->dynamic_component_method( "Model::Foo", {} ); my $foo = $app->model('Foo'); ok $foo, 'Have added Foo component'; isa_ok($foo, 'Catalyst::Component'); @@ -40,7 +40,7 @@ throws_ok { generate_testapp(); } }); my $app = $app_meta->name; ok $app->can('dynamic_component_method'), 'dynamic component method added'; - $app->dynamic_component_method( $app . "::Model::Foo", {} ); + $app->dynamic_component_method( "Model::Foo", {} ); my $foo = $app->model('Foo'); ok $foo, 'Have added Foo component'; isa_ok($foo, 'TestClass', 'COMPONENT method returned totally different class'); @@ -93,7 +93,7 @@ my $extra_config = { %generator_config, }); my $app = $app_meta->name; - $app->dynamic_component_method( $app . "::Model::Foo", $extra_config ); + $app->dynamic_component_method( "Model::Foo", $extra_config ); my $model = $app->model('Foo'); isa_ok($model, 'My::Model', 'Correct superclass'); ok(!$model->isa('My::Other::Superclass'), @@ -101,7 +101,7 @@ my $extra_config = { ok $model->can('_some_method_from_role'), 'Has had role applied'; ok !My::Model->can('_some_method_from_role'), 'Role applied at right place'; - + ok $model->can('_some_method_from_other_role'), 'Role application merges by default'; @@ -120,7 +120,7 @@ my $extra_config = { methods_resolve_strategy => 'replace', }); my $app = $app_meta->name; - $app->dynamic_component_method( $app . "::Model::Foo", $extra_config ); + $app->dynamic_component_method( "Model::Foo", $extra_config ); my $model = $app->model('Foo'); isa_ok($model, 'My::Model', 'Correct superclass'); isa_ok($model, 'My::Other::Superclass', @@ -128,7 +128,7 @@ my $extra_config = { ok $model->can('_some_method_from_role'), 'Has had role applied'; ok !My::Model->can('_some_method_from_role'), 'Role applied at right place'; - + ok !$model->can('_some_method_from_other_role'), 'Role application replaces when configured'; @@ -148,7 +148,7 @@ my $extra_config = { }); my $app = $app_meta->name; - $app->dynamic_component_method( $app . "::Model::Foo", {} ); + $app->dynamic_component_method( "Model::Foo", {} ); my $model = $app->model('Foo'); ok $model->can('my_other_injected_method'), @@ -170,7 +170,7 @@ my $extra_config = { }); my $app = $app_meta->name; - $app->dynamic_component_method( $app . "::Model::Foo", { superclasses => [], roles => [], methods => {} } ); + $app->dynamic_component_method( "Model::Foo", { superclasses => [], roles => [], methods => {} } ); my $model = $app->model('Foo'); ok !$model->can('my_other_injected_method'),