1 package Reaction::UI::ViewPort::Field::Role::Mutable;
5 use aliased 'Reaction::InterfaceModel::Action';
6 use aliased 'Reaction::Meta::InterfaceModel::Action::ParameterAttribute';
8 use namespace::clean -except => [ qw(meta) ];
10 has model => (is => 'ro', isa => Action, required => 1);
11 has attribute => (is => 'ro', isa => ParameterAttribute, required => 1);
14 is => 'rw', lazy_build => 1, trigger_adopt('value'),
15 clearer => 'clear_value',
17 has needs_sync => (is => 'rw', isa => 'Int', default => 0);
19 has message => (is => 'rw', isa => 'Str', clearer => 'clear_message');
21 after clear_value => sub {
23 $self->clear_message if $self->has_message;
29 $self->clear_message if $self->has_message;
30 $self->needs_sync(1); # if $self->has_attribute;
33 sub can_sync_to_action {
35 return unless $self->needs_sync;
36 my $attr = $self->attribute;
38 if ($self->has_value) {
39 my $value = $self->value;
40 if (my $tc = $attr->type_constraint) {
41 $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
42 if (defined (my $error = $tc->validate($value))) {
43 $self->message($error);
48 return if $self->model->attribute_is_required($attr);
54 return unless $self->can_sync_to_action;
56 my $attr = $self->attribute;
58 if ($self->has_value) {
59 my $value = $self->value;
60 if (my $tc = $attr->type_constraint) {
61 #this will go away when we have moose dbic. until then though...
62 $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
64 my $writer = $attr->get_write_method;
65 confess "No writer for attribute" unless defined($writer);
66 $self->model->$writer($value);
68 my $predicate = $attr->get_predicate_method;
69 confess "No predicate for attribute" unless defined($predicate);
70 if ($self->model->$predicate) {
71 my $clearer = $attr->get_clearer_method;
72 confess "${predicate} returned true but no clearer for attribute"
73 unless defined($clearer);
74 $self->model->$clearer;
79 sub sync_from_action {
81 return unless !$self->needs_sync; # && $self->has_attribute;
82 if( !$self->has_message ){
83 if(my $error = $self->model->error_for($self->attribute) ){
84 $self->message( $error );
89 around accept_events => sub { ('value', shift->(@_)) };
97 Reaction::UI::ViewPort::Role::Actions
101 A role to ease attaching actions to L<Reaction::InterfaceModel::Object>s
119 =head2 sync_from_action
121 =head2 sync_to_action
127 See L<Reaction::Class> for authors.
131 See L<Reaction::Class> for the license.