1 package Reaction::UI::ViewPort::Field::Role::Mutable;
5 use aliased 'Reaction::InterfaceModel::Action';
6 use aliased 'Reaction::Meta::InterfaceModel::Action::ParameterAttribute';
9 has model => (is => 'ro', isa => Action, required => 1);
10 has attribute => (is => 'ro', isa => ParameterAttribute, required => 1);
13 is => 'rw', lazy_build => 1, trigger_adopt('value'),
14 clearer => 'clear_value',
16 has needs_sync => (is => 'rw', isa => 'Int', default => 0);
17 has message => (is => 'rw', isa => 'Str');
19 after clear_value => sub {
23 implements adopt_value => as {
25 $self->needs_sync(1); # if $self->has_attribute;
28 implements sync_to_action => as {
30 return unless $self->needs_sync;
31 my $attr = $self->attribute;
33 if ($self->has_value) {
34 my $value = $self->value;
35 if (my $tc = $attr->type_constraint) {
36 $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
37 #my $error = $tc->validate($self->value); # should we be checking against $value?
38 my $error = $tc->validate($value);
40 $self->message($error);
44 my $writer = $attr->get_write_method;
45 confess "No writer for attribute" unless defined($writer);
46 $self->model->$writer($value);
48 my $predicate = $attr->predicate;
49 confess "No predicate for attribute" unless defined($predicate);
50 if ($self->model->$predicate) {
51 my $clearer = $attr->clearer;
52 confess "${predicate} returned true but no clearer for attribute"
53 unless defined($clearer);
54 $self->model->$clearer;
60 implements sync_from_action => as {
62 return unless !$self->needs_sync; # && $self->has_attribute;
63 $self->message($self->model->error_for($self->attribute) || '');
66 around accept_events => sub { ('value', shift->(@_)) };
74 Reaction::UI::ViewPort::Role::Actions
78 A role to ease attaching actions to L<Reaction::InterfaceModel::Object>s
96 =head2 sync_from_action
104 See L<Reaction::Class> for authors.
108 See L<Reaction::Class> for the license.