X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FReaction%2FUI%2FViewPort%2FField%2FRole%2FMutable.pm;h=cb48dce93f37c4819aa16ba6965dfa477150ba86;hb=f25cb331e16a59394b9f89d90b6ee9ff77e38f91;hp=8690603fc4595ed159b7916c0bcc16b2f94bed17;hpb=1734a92a996a20dd6d292fc4aea58e876c24249b;p=catagits%2FReaction.git diff --git a/lib/Reaction/UI/ViewPort/Field/Role/Mutable.pm b/lib/Reaction/UI/ViewPort/Field/Role/Mutable.pm index 8690603..cb48dce 100644 --- a/lib/Reaction/UI/ViewPort/Field/Role/Mutable.pm +++ b/lib/Reaction/UI/ViewPort/Field/Role/Mutable.pm @@ -9,10 +9,28 @@ role Mutable, which { has model => (is => 'ro', isa => Action, required => 1); has attribute => (is => 'ro', isa => ParameterAttribute, required => 1); - has value => (is => 'rw', lazy_build => 1, trigger_adopt('value')); + has value => ( + is => 'rw', lazy_build => 1, trigger_adopt('value'), + clearer => 'clear_value', + ); has needs_sync => (is => 'rw', isa => 'Int', default => 0); has message => (is => 'rw', isa => 'Str'); + around value => sub { + my $orig = shift; + my $self = shift; + if (@_ && !ref($_[0]) && defined($_[0]) && !length($_[0])) { # '' + unless ($self->value_is_required) { + return $self->clear_value; + } + } + $self->$orig(@_); + }; + + after clear_value => sub { + shift->needs_sync(1); + }; + implements adopt_value => as { my ($self) = @_; $self->needs_sync(1); # if $self->has_attribute; @@ -22,20 +40,31 @@ role Mutable, which { my ($self) = @_; return unless $self->needs_sync && $self->has_value; my $attr = $self->attribute; - my $writer = $attr->get_write_method; - confess "No writer for attribute" unless defined($writer); - - my $value = $self->value; - if (my $tc = $attr->type_constraint) { - $value = $tc->coercion->coerce($value) if ($tc->has_coercion); - #my $error = $tc->validate($self->value); # should we be checking against $value? - my $error = $tc->validate($value); - if (defined $error) { - $self->message($error); - return; + + if ($self->has_value) { + my $value = $self->value; + if (my $tc = $attr->type_constraint) { + $value = $tc->coercion->coerce($value) if ($tc->has_coercion); + #my $error = $tc->validate($self->value); # should we be checking against $value? + my $error = $tc->validate($value); + if (defined $error) { + $self->message($error); + return; + } + } + my $writer = $attr->get_write_method; + confess "No writer for attribute" unless defined($writer); + $self->model->$writer($value); + } else { + my $predicate = $attr->get_predicate; + confess "No predicate for attribute" unless defined($predicate); + if ($self->model->$predicate) { + my $clearer = $attr->get_clearer; + confess "${predicate} returned true but no clearer for attribute" + unless defined($clearer); + $self->model->$clearer; } } - $self->model->$writer($value); $self->needs_sync(0); };