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;
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);
};