do not include .git directory
[catagits/Reaction.git] / lib / Reaction / InterfaceModel / Action / DBIC / ResultSet / Create.pm
1 package Reaction::InterfaceModel::Action::DBIC::ResultSet::Create;
2
3 use Reaction::Types::DBIC 'ResultSet';
4 use Reaction::Class;
5 use Reaction::InterfaceModel::Action;
6 use Reaction::InterfaceModel::Action::DBIC::Role::CheckUniques;
7
8 use namespace::clean -except => [ qw(meta) ];
9 extends 'Reaction::InterfaceModel::Action::DBIC::ResultSet';
10
11 with 'Reaction::InterfaceModel::Action::DBIC::Role::CheckUniques';
12
13 sub do_apply {
14   my $self = shift;
15   my $args = $self->parameter_hashref;
16   my $new = $self->target_model->new({});
17   my @delay;
18   foreach my $name (keys %$args) {
19     my $tm_attr = $new->meta->find_attribute_by_name($name) or next;
20     my $tm_writer = $tm_attr->get_write_method;
21     unless ($tm_writer) {
22       warn "Unable to find writer for ${name}";
23       next;
24     }
25     if ($tm_attr->type_constraint->name eq 'ArrayRef'
26         || $tm_attr->type_constraint->is_subtype_of('ArrayRef')) {
27       push(@delay, [ $tm_writer, $args->{$name} ]);
28     } else {
29       $new->$tm_writer($args->{$name});
30     }
31   }
32   $new->insert;
33   foreach my $d (@delay) {
34     my ($meth, $val) = @$d;
35     $new->$meth($val);
36   }
37   return $new;
38 };
39
40 __PACKAGE__->meta->make_immutable;
41
42 1;
43
44 __END__;
45
46 =head1 NAME
47
48 Reaction::InterfaceModel::Action::DBIC::ResultSet::Create
49
50 =head1 DESCRIPTION
51
52 Create a new object.
53
54 C<Update> is a subclass of
55 L<Action::DBIC::ResultSet|Reaction::InterfaceModel::Action::DBIC::ResultSet> 
56 that cponsumes L<Role::CheckUniques|'Reaction::InterfaceModel::Action::DBIC::Role::CheckUniques>
57
58 =head2 do_apply
59
60 Create a C<new_result> for the C<target_model>, sync it to the action's
61 C<parameter_attributes> and C<insert> it into the database. Returns the newly
62 inserted object
63
64 =head1 SEE ALSO
65
66 L<DeleteAll|Reaction::InterfaceModel::Action::DBIC::ResultSet::DeleteAll>,
67 L<Update|Reaction::InterfaceModel::Action::DBIC::Result::Update>,
68 L<Delete|Reaction::InterfaceModel::Action::DBIC::Result::Delete>,
69
70 =head1 AUTHORS
71
72 See L<Reaction::Class> for authors.
73
74 =head1 LICENSE
75
76 See L<Reaction::Class> for the license.
77
78 =cut