my $pre_immutable_hook = $p->pre_immutable_hook;
method $name => sub {
- my ($app, $name, $config, $methods) = @_;
+ my ($app, $name, $config) = @_;
+
+ $config ||= {};
my $appclass = blessed($app) || $app;
my $type = $name;
$app->$pre_immutable_hook($meta) if $p->has_pre_immutable_hook;
- $methods ||= {};
- foreach my $name (keys %$methods) {
- $meta->add_method($name => $methods->{$name});
+ foreach my $name (keys %{ $config->{methods}||{} }) {
+ $meta->add_method($name => $config->{methods}->{$name});
}
$meta->make_immutable;
my @roles = @{ $config->{roles}||[] };
@roles = uniq @roles, 'CatalystX::ModelToControllerReflector::ControllerRole';
$config->{roles} = \@roles;
- $app->_setup_dynamic_controller( $controller_name, $config, \%controller_methods );
+ $config->{methods} = \%controller_methods;
+ $app->_setup_dynamic_controller( $controller_name, $config );
}
sub generate_reflected_controller_action_method {
sub _setup_dynamic_models {
my ($app) = @_;
-
+
my $app_name = blessed($app) || $app;
my $model_prefix = 'Model::';
my $config = $app->config || {};
-
+
foreach my $model_name ( grep { /^$model_prefix/ } keys %$config ) {
my $model_class_name = $app_name . '::' . $model_name;
-
+
$app->_setup_dynamic_model( $model_class_name, $config->{$model_name} );
}
}
use strict;
use warnings;
-use Test::More tests => 10;
+use Test::More tests => 15;
use Test::Exception;
use Moose ();
is($name->can('COMPONENT'), $COMPONENT, 'Supplied COMPONENT method is on $app::Model::Foo');
}
+{
+ package My::Model;
+ use Moose;
+ extends qw/Catalyst::Model/;
+ __PACKAGE__->meta->make_immutable;
+}
+{
+ package My::Role;
+ use Moose::Role;
+ sub _some_method_from_role {}
+}
+{
+ my $app_meta = generate_testapp({
+ name => 'dynamic_component_method',
+ });
+ my $app = $app_meta->name;
+ my $extra_config = {
+ superclasses => ['My::Model'],
+ roles => ['My::Role'],
+ methods => {
+ my_injected_method => sub { 'quuux' },
+ }
+ };
+ $app->dynamic_component_method( $app . "::Model::Foo", $extra_config );
+ my $model = $app->model('Foo');
+ isa_ok($model, 'My::Model');
+ 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('my_injected_method'), 'Injected method there as expected';
+ is $model->my_injected_method, 'quuux', 'Injected method returns correct val';
+}