do not include .git directory
[catagits/Reaction.git] / lib / Reaction / InterfaceModel / Action / DBIC / Result / Update.pm
1 package Reaction::InterfaceModel::Action::DBIC::Result::Update;
2
3 use Reaction::Class;
4 use namespace::clean -except => [ qw(meta) ];
5
6 extends 'Reaction::InterfaceModel::Action::DBIC::Result';
7 with 'Reaction::InterfaceModel::Action::DBIC::Role::CheckUniques';
8
9 sub BUILD {
10   my ($self) = @_;
11   my $tm = $self->target_model;
12   foreach my $attr ($self->parameter_attributes) {
13     my $writer = $attr->get_write_method;
14     my $name = $attr->name;
15     my $tm_attr = $tm->meta->find_attribute_by_name($name);
16     next unless ref $tm_attr;
17     my $tm_reader = $tm_attr->get_read_method;
18     $self->$writer($tm->$tm_reader) if defined($tm->$tm_reader);
19   }
20 }
21
22 sub do_apply {
23   my $self = shift;
24   my $args = $self->parameter_hashref;
25   my $model = $self->target_model;
26   foreach my $name (keys %$args) {
27     my $tm_attr = $model->meta->find_attribute_by_name($name);
28     next unless ref $tm_attr;
29     my $tm_writer = $tm_attr->get_write_method;
30     $model->$tm_writer($args->{$name});
31   }
32   $model->update;
33   return $model;
34 }
35
36 __PACKAGE__->meta->make_immutable;
37
38 1;
39
40 __END__;
41
42 =head1 NAME
43
44 Reaction::InterfaceModel::Action::DBIC::Result::Update
45
46 =head1 DESCRIPTION
47
48 Update the target model and sync the Action's parameter attributes to
49 the target model.
50
51 C<Update> is a subclass of
52 L<Action::DBIC::Result|Reaction::InterfaceModel::Action::DBIC::Result> that cponsumes
53 L<Role::CheckUniques|'Reaction::InterfaceModel::Action::DBIC::Role::CheckUniques>
54
55 =head2 BUILD
56
57 Sync the values from the target model's parameter attributes to the action's
58 parameter attributes
59
60 =head2 do_apply
61
62 Sync the target model's parameter attributes to the values returned by
63 C<parameter_hashref>, call C<update> and return the C<target_model>.
64
65 =head1 SEE ALSO
66
67 L<Create|Reaction::InterfaceModel::Action::DBIC::ResultSet::Create>,
68 L<DeleteAll|Reaction::InterfaceModel::Action::DBIC::ResultSet::DeleteAll>,
69 L<Delete|Reaction::InterfaceModel::Action::DBIC::Result::Delete>,
70
71 =head1 AUTHORS
72
73 See L<Reaction::Class> for authors.
74
75 =head1 LICENSE
76
77 See L<Reaction::Class> for the license.
78
79 =cut