Commit | Line | Data |
ddccc6a2 |
1 | package Reaction::UI::ViewPort::Field::Role::Mutable; |
2 | |
c8fbb8ad |
3 | use Reaction::Role; |
4 | |
ddccc6a2 |
5 | use aliased 'Reaction::InterfaceModel::Action'; |
6 | use aliased 'Reaction::Meta::InterfaceModel::Action::ParameterAttribute'; |
7 | |
8 | role Mutable, which { |
9 | has model => (is => 'ro', isa => Action, required => 1); |
10 | has attribute => (is => 'ro', isa => ParameterAttribute, required => 1); |
11 | |
12 | has value => (is => 'rw', lazy_build => 1, trigger_adopt('value')); |
13 | has needs_sync => (is => 'rw', isa => 'Int', default => 0); |
14 | has message => (is => 'rw', isa => 'Str'); |
15 | |
16 | implements adopt_value => as { |
17 | my ($self) = @_; |
18 | $self->needs_sync(1); # if $self->has_attribute; |
19 | }; |
20 | |
21 | implements sync_to_action => as { |
22 | my ($self) = @_; |
23 | return unless $self->needs_sync && $self->has_value; |
24 | my $attr = $self->attribute; |
1734a92a |
25 | my $writer = $attr->get_write_method; |
26 | confess "No writer for attribute" unless defined($writer); |
27 | |
28 | my $value = $self->value; |
ddccc6a2 |
29 | if (my $tc = $attr->type_constraint) { |
ddccc6a2 |
30 | $value = $tc->coercion->coerce($value) if ($tc->has_coercion); |
1734a92a |
31 | #my $error = $tc->validate($self->value); # should we be checking against $value? |
32 | my $error = $tc->validate($value); |
ddccc6a2 |
33 | if (defined $error) { |
34 | $self->message($error); |
35 | return; |
36 | } |
37 | } |
1734a92a |
38 | $self->model->$writer($value); |
ddccc6a2 |
39 | $self->needs_sync(0); |
40 | }; |
41 | |
42 | implements sync_from_action => as { |
43 | my ($self) = @_; |
44 | return unless !$self->needs_sync; # && $self->has_attribute; |
36d54b14 |
45 | $self->message($self->model->error_for($self->attribute) || ''); |
ddccc6a2 |
46 | }; |
47 | |
48 | around accept_events => sub { ('value', shift->(@_)) }; |
49 | |
50 | }; |
c8fbb8ad |
51 | |
52 | 1; |
2dba7201 |
53 | |
54 | =head1 NAME |
55 | |
56 | Reaction::UI::ViewPort::Role::Actions |
57 | |
58 | =head1 DESCRIPTION |
59 | |
60 | A role to ease attaching actions to L<Reaction::InterfaceModel::Object>s |
61 | |
62 | =head1 ATTRIBUTES |
63 | |
64 | =head2 needs_sync |
65 | |
66 | =head2 message |
67 | |
68 | =head2 model |
69 | |
70 | =head2 attribute |
71 | |
72 | =head2 value |
73 | |
74 | =head1 METHODS |
75 | |
76 | =head2 accept_events |
77 | |
78 | =head2 sync_from_action |
79 | |
80 | =head2 sync_to_action |
81 | |
82 | =head2 adopt_value |
83 | |
84 | =head1 AUTHORS |
85 | |
86 | See L<Reaction::Class> for authors. |
87 | |
88 | =head1 LICENSE |
89 | |
90 | See L<Reaction::Class> for the license. |
91 | |
92 | =cut |