update changelog in prep for release
[catagits/CatalystX-Declare.git] / lib / CatalystX / Declare / Controller / ActionPreparation.pm
CommitLineData
ed4a2203 1use MooseX::Declare;
2
3role CatalystX::Declare::Controller::ActionPreparation {
4
5 use aliased 'CatalystX::Declare::Action::CatchValidationError';
6 use aliased 'CatalystX::Declare::Dispatching::ChainTypeSensitivity';
7
ed4a2203 8 method _find_method_type_constraint (Str $name) {
9
10 $self->meta->find_method_type_constraint($name)
11 || do {
12 my $method = $self->meta->find_method_by_name($name);
13 ( $_ = $method->can('type_constraint') )
14 ? $method->$_
15 : undef
16 };
17 }
18
accfac7d 19 method _find_method_named_params (Str $name) {
20
21 return $self->meta->find_method_named_params($name);
22 }
23
24 method _find_method_named_type_constraint (Str $method, Str $param) {
25
26 return $self->meta->find_method_named_type_constraint($method, $param);
27 }
28
ed4a2203 29 method _ensure_applied_dispatchtype_roles {
30
31 my $type = $self->_app->dispatcher->dispatch_type('Chained');
32
33 return
34 if $type->DOES(ChainTypeSensitivity);
35
ed4a2203 36 my $immutable = $type->meta->is_immutable;
56929b96 37 my %immutable_options;
38 if ($immutable) {
39 %immutable_options = $type->meta->immutable_options;
40 $type->meta->make_mutable;
41 }
42
43 # FIXME we really shouldn't have to tweak the dispatch type
ed4a2203 44 ChainTypeSensitivity->meta->apply($type->meta);
56929b96 45
46 $type->meta->make_immutable(%immutable_options)
ed4a2203 47 if $immutable;
48 }
49
50 after register_actions {
51
52 $self->_ensure_applied_dispatchtype_roles;
53 }
54
12b853ff 55
2c3192ac 56 around gather_action_roles(%args) {
57 return (
58 $self->$orig(%args),
12b853ff 59 (map {
60 $self->_qualify_class_name(ActionRole => $_);
61 #$self->_expand_role_shortname($_);
62 } @{ delete($args{attributes}{CatalystX_Declarative_ActionRoles}) || [] }),
63 @{ delete($args{attributes}{CatalystX_Declarative_DefaultActionRoles}) || [] },
2c3192ac 64 );
65 }
ed4a2203 66
2c3192ac 67 around create_action (%args) {
ed4a2203 68 my $action = $self->$orig(%args);
69
12b853ff 70 ## TODO Do we really need this anymore?
ed4a2203 71 return $action
72 if $args{attributes}{Private};
73
ed4a2203 74 return $action
75 unless $action->DOES(CatchValidationError);
76
77 my $tc = $self->_find_method_type_constraint($action->name);
accfac7d 78 my $np = $self->_find_method_named_params($action->name);
ed4a2203 79
80 return $action
81 unless $tc;
82
ed4a2203 83 $action->controller_instance($self);
accfac7d 84 $action->method_type_constraint($tc);
85
86 if ($np) {
87
88 $action->method_named_params($np);
89 $action->method_named_type_constraint({
90 map +($_, $self->_find_method_named_type_constraint($action->name, $_)),
91 @$np,
92 });
93 }
ed4a2203 94
95 return $action;
96 }
97}