59591ad36d2545a6e669b54f1b36033d23764db1
[catagits/Reaction.git] / lib / Reaction / InterfaceModel / Action / DBIC / Result / Update.pm
1 package Reaction::InterfaceModel::Action::DBIC::Result::Update;
2
3 use Reaction::InterfaceModel::Action;
4 use Reaction::Types::DBIC 'Row';
5 use Reaction::Class;
6
7 class Update is 'Reaction::InterfaceModel::Action', which {
8
9   does 'Reaction::InterfaceModel::Action::DBIC::Role::CheckUniques';
10
11   has '+target_model' => (isa => Row);
12
13   implements BUILD => as {
14     my ($self) = @_;
15     my $tm = $self->target_model;
16     foreach my $attr ($self->parameter_attributes) {
17       my $writer = $attr->get_write_method;
18       my $name = $attr->name;
19       my $tm_attr = $tm->meta->find_attribute_by_name($name);
20       next unless ref $tm_attr;
21       my $tm_reader = $tm_attr->get_read_method;
22       $self->$writer($tm->$tm_reader) if defined($tm->$tm_reader);
23     }
24   };
25
26   implements do_apply => as {
27     my $self = shift;
28     my $args = $self->parameter_hashref;
29     my $model = $self->target_model;
30     foreach my $name (keys %$args) {
31       my $tm_attr = $model->meta->find_attribute_by_name($name);
32       next unless ref $tm_attr;
33       my $tm_writer = $tm_attr->get_write_method;
34       $model->$tm_writer($args->{$name});
35     }
36     $model->update;
37     return $model;
38   };
39
40 };
41
42 1;
43
44 =head1 NAME
45
46 Reaction::InterfaceModel::Action::DBIC::Result::Update
47
48 =head1 DESCRIPTION
49
50 =head2 target_model
51
52 =head2 error_for_attribute
53
54 =head2 sync_all
55
56 =head2 BUILD
57
58 =head1 AUTHORS
59
60 See L<Reaction::Class> for authors.
61
62 =head1 LICENSE
63
64 See L<Reaction::Class> for the license.
65
66 =cut