add warning for people who aren't using Reaction::Role(::Parameterized)?
[catagits/Reaction.git] / lib / Reaction / InterfaceModel / Search / UpdateSpec.pm
1 package Reaction::InterfaceModel::Search::UpdateSpec;
2
3 use Reaction::Role;
4 use Method::Signatures::Simple;
5 use aliased 'Reaction::InterfaceModel::Search::Spec', 'SearchSpec';
6 use namespace::clean -except => 'meta';
7
8 # FIXME - has '+attr' broken, copied from Reaction::InterfaceModel::Action
9 #has '+target_model' => (isa => SearchSpec);
10 has target_model => (
11   isa => SearchSpec,
12   is => 'ro',
13   required => 1,
14   metaclass => 'Reaction::Meta::Attribute'
15 );
16
17 requires '_reflection_info';
18
19 override BUILDARGS => method () {
20   my $args = super;
21   my $model = $args->{target_model};
22   my $reflected = $self->_reflection_info;
23   foreach my $attr (@{$reflected->{empty}||[]}) {
24     if ($model->${\"has_${attr}"}) {
25       $args->{$attr} = $model->$attr;
26     } else {
27       $args->{$attr} = '';
28     }
29   }
30   foreach my $attr (@{$reflected->{normal}||[]}) {
31     my $has = $model->can("has_${attr}")||sub {1};
32     $args->{$attr} = $model->$attr if $model->$has;
33   }
34   $args;
35 };
36
37 method do_apply () {
38   my $data = $self->parameter_hashref;
39   my $spec = $self->target_model;
40   foreach my $name (keys %$data) {
41     # note: this assumes plain is => 'rw' attrs on the backend
42     # which is safe since we control it. Also, we assume '' means
43     # clear - this may not be safe later but is for now
44     if (length(my $value = $data->{$name})) {
45       $spec->$name($value);
46     } else {
47       $spec->${\"clear_${name}"};
48     }
49   }
50   $spec;
51 }
52
53 1;
54