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');
22 if (@_ && !ref($_[0]) && defined($_[0]) && !length($_[0])) { # ''
23 unless ($self->value_is_required) {
24 return $self->clear_value;
30 after clear_value => sub {
34 implements adopt_value => as {
36 $self->needs_sync(1); # if $self->has_attribute;
39 implements sync_to_action => as {
41 return unless $self->needs_sync && $self->has_value;
42 my $attr = $self->attribute;
44 if ($self->has_value) {
45 my $value = $self->value;
46 if (my $tc = $attr->type_constraint) {
47 $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
48 #my $error = $tc->validate($self->value); # should we be checking against $value?
49 my $error = $tc->validate($value);
51 $self->message($error);
55 my $writer = $attr->get_write_method;
56 confess "No writer for attribute" unless defined($writer);
57 $self->model->$writer($value);
59 my $predicate = $attr->get_predicate;
60 confess "No predicate for attribute" unless defined($predicate);
61 if ($self->model->$predicate) {
62 my $clearer = $attr->get_clearer;
63 confess "${predicate} returned true but no clearer for attribute"
64 unless defined($clearer);
65 $self->model->$clearer;
71 implements sync_from_action => as {
73 return unless !$self->needs_sync; # && $self->has_attribute;
74 $self->message($self->model->error_for($self->attribute) || '');
77 around accept_events => sub { ('value', shift->(@_)) };
85 Reaction::UI::ViewPort::Role::Actions
89 A role to ease attaching actions to L<Reaction::InterfaceModel::Object>s
107 =head2 sync_from_action
109 =head2 sync_to_action
115 See L<Reaction::Class> for authors.
119 See L<Reaction::Class> for the license.