Commit | Line | Data |
7adfd53f |
1 | package Reaction::InterfaceModel::Action::DBIC::ResultSet::Create; |
2 | |
e739c9a2 |
3 | use Reaction::Types::DBIC 'ResultSet'; |
7adfd53f |
4 | use Reaction::Class; |
5 | use Reaction::InterfaceModel::Action; |
6 | use Reaction::InterfaceModel::Action::DBIC::Role::CheckUniques; |
7 | |
81393881 |
8 | use namespace::clean -except => [ qw(meta) ]; |
336b3bb0 |
9 | extends 'Reaction::InterfaceModel::Action::DBIC::ResultSet'; |
81393881 |
10 | |
11 | with 'Reaction::InterfaceModel::Action::DBIC::Role::CheckUniques'; |
12 | |
81393881 |
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) { |
d98c38f8 |
19 | my $tm_attr = $new->meta->find_attribute_by_name($name) or next; |
81393881 |
20 | my $tm_writer = $tm_attr->get_write_method; |
21 | unless ($tm_writer) { |
22 | warn "Unable to find writer for ${name}"; |
23 | next; |
7adfd53f |
24 | } |
81393881 |
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; |
7adfd53f |
38 | }; |
39 | |
81393881 |
40 | __PACKAGE__->meta->make_immutable; |
41 | |
7adfd53f |
42 | 1; |
43 | |
1c601a76 |
44 | __END__; |
45 | |
7adfd53f |
46 | =head1 NAME |
47 | |
48 | Reaction::InterfaceModel::Action::DBIC::ResultSet::Create |
49 | |
50 | =head1 DESCRIPTION |
51 | |
1c601a76 |
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 |
7adfd53f |
63 | |
1c601a76 |
64 | =head1 SEE ALSO |
7adfd53f |
65 | |
1c601a76 |
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>, |
7adfd53f |
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 |