diff options
author | matthewt <matthewt@03d0b0b2-0e1a-0410-a411-fdb2f4bd65d7> | 2008-02-02 17:07:10 +0000 |
---|---|---|
committer | matthewt <matthewt@03d0b0b2-0e1a-0410-a411-fdb2f4bd65d7> | 2008-02-02 17:07:10 +0000 |
commit | f25cb331e16a59394b9f89d90b6ee9ff77e38f91 (patch) | |
tree | b8495ed1d69ccc885b972cfca7ca2b2665a2a7e4 /lib/Reaction/UI/ViewPort/Field | |
parent | cc0796e628d61c325642cba1317678c82f85daa2 (diff) | |
download | reaction-f25cb331e16a59394b9f89d90b6ee9ff77e38f91.tar.gz reaction-f25cb331e16a59394b9f89d90b6ee9ff77e38f91.zip |
add value_is_required, add logic for sync of clearer on !required attrs
Diffstat (limited to 'lib/Reaction/UI/ViewPort/Field')
-rw-r--r-- | lib/Reaction/UI/ViewPort/Field/Role/Mutable.pm | 55 |
1 files changed, 42 insertions, 13 deletions
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); }; |