use aliased 'Reaction::InterfaceModel::Action';
use aliased 'Reaction::Meta::InterfaceModel::Action::ParameterAttribute';
+use MooseX::Types::Moose qw/Int Str/;
-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'),
- clearer => 'clear_value',
- );
- has needs_sync => (is => 'rw', isa => 'Int', default => 0);
- has message => (is => 'rw', isa => 'Str');
-
- after clear_value => sub {
- shift->needs_sync(1);
- };
-
- implements adopt_value => as {
- my ($self) = @_;
- $self->needs_sync(1); # if $self->has_attribute;
- };
-
- implements sync_to_action => as {
- my ($self) = @_;
- return unless $self->needs_sync;
- my $attr = $self->attribute;
-
- 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;
- }
+use namespace::clean -except => [ qw(meta) ];
+
+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'),
+ clearer => 'clear_value',
+);
+has needs_sync => (is => 'rw', isa => Int, default => 0); #should be bool?
+
+has message => (is => 'rw', isa => Str, clearer => 'clear_message');
+
+has is_modified => ( #sould be bool?
+ is => 'ro', writer => '_set_modified',
+ required => 1, default => 1, init_arg => undef
+);
+
+after clear_value => sub {
+ my $self = shift;
+ $self->clear_message if $self->has_message;
+ $self->needs_sync(1);
+};
+
+sub adopt_value {
+ my ($self) = @_;
+ $self->clear_message if $self->has_message;
+ $self->needs_sync(1); # if $self->has_attribute;
+}
+
+
+sub can_sync_to_action {
+ my $self = shift;
+
+ # if field is already sync'ed, it can be sync'ed again
+ # this will make sync_to_action no-op if needs_sync is 0
+ return 1 unless $self->needs_sync;
+ my $attr = $self->attribute;
+
+ if ($self->has_value) {
+ my $value = $self->value;
+ if (my $tc = $attr->type_constraint) {
+ $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
+ if (defined (my $error = $tc->validate($value))) {
+ $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->predicate;
- confess "No predicate for attribute" unless defined($predicate);
- if ($self->model->$predicate) {
- my $clearer = $attr->clearer;
- confess "${predicate} returned true but no clearer for attribute"
- unless defined($clearer);
- $self->model->$clearer;
+ }
+ } else {
+ if( $self->model->attribute_is_required($attr) ){
+ if(my $error = $self->model->error_for($self->attribute) ){
+ $self->message( $error );
}
+ return;
}
- $self->needs_sync(0);
- };
+ }
+ return 1;
+};
+
+
+sub sync_to_action {
+ my ($self) = @_;
- implements sync_from_action => as {
- my ($self) = @_;
- return unless !$self->needs_sync; # && $self->has_attribute;
- $self->message($self->model->error_for($self->attribute) || '');
- };
+ # don't sync if we're already synced
+ return unless $self->needs_sync;
- around accept_events => sub { ('value', shift->(@_)) };
+ # if we got here, needs_sync is 1
+ # can_sync_to_action will do coercion checks, etc.
+ return unless $self->can_sync_to_action;
+ my $attr = $self->attribute;
+
+ if ($self->has_value) {
+ my $value = $self->value;
+ if (my $tc = $attr->type_constraint) {
+ #this will go away when we have moose dbic. until then though...
+ $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
+ }
+ my $writer = $attr->get_write_method;
+ confess "No writer for attribute" unless defined($writer);
+ $self->model->$writer($value);
+ } else {
+ my $predicate = $attr->get_predicate_method;
+ confess "No predicate for attribute" unless defined($predicate);
+ if ($self->model->$predicate) {
+ my $clearer = $attr->get_clearer_method;
+ confess "${predicate} returned true but no clearer for attribute"
+ unless defined($clearer);
+ $self->model->$clearer;
+ }
+ }
+ $self->needs_sync(0);
};
+sub sync_from_action {
+ my ($self) = @_;
+ return if $self->needs_sync;
+ if( !$self->has_message ){
+ if(my $error = $self->model->error_for($self->attribute) ){
+ $self->message( $error );
+ }
+ }
+};
+
+around accept_events => sub { ('value', shift->(@_)) };
+
+
1;