work in progress, listview still broken
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Field / Role / Mutable.pm
CommitLineData
ddccc6a2 1package Reaction::UI::ViewPort::Field::Role::Mutable;
2
c8fbb8ad 3use Reaction::Role;
4
ddccc6a2 5use aliased 'Reaction::InterfaceModel::Action';
6use aliased 'Reaction::Meta::InterfaceModel::Action::ParameterAttribute';
7
8role 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;
25 if (my $tc = $attr->type_constraint) {
26 my $value = $self->value;
27 $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
28 my $error = $tc->validate($self->value); # should we be checking against $value?
29 if (defined $error) {
30 $self->message($error);
31 return;
32 }
33 }
34 my $writer = $attr->get_write_method;
35 confess "No writer for attribute" unless defined($writer);
36 $self->action->$writer($self->value); #should we be passing $value ?
37 $self->needs_sync(0);
38 };
39
40 implements sync_from_action => as {
41 my ($self) = @_;
42 return unless !$self->needs_sync; # && $self->has_attribute;
43 $self->message($self->action->error_for($self->attribute) || '');
44 };
45
46 around accept_events => sub { ('value', shift->(@_)) };
47
48};
c8fbb8ad 49
501;