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);
18 #predicates are autmagically generated for lazy and non-required attrs
19 has message => (is => 'rw', isa => 'Str', clearer => 'clear_message');
21 after clear_value => sub {
23 $self->clear_message if $self->has_message;
28 $self->clear_message if $self->has_message;
29 $self->needs_sync(1); # if $self->has_attribute;
31 sub can_sync_to_action {
33 return 1 unless $self->needs_sync;
34 my $attr = $self->attribute;
36 if ($self->has_value) {
37 my $value = $self->value;
38 if (my $tc = $attr->type_constraint) {
39 $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
40 if (defined (my $error = $tc->validate($value))) {
41 $self->message($error);
46 return if $attr->is_required;
52 return unless $self->can_sync_to_action;
54 my $attr = $self->attribute;
56 if ($self->has_value) {
57 my $value = $self->value;
58 if (my $tc = $attr->type_constraint) {
59 #this will go away when we have moose dbic. until then though...
60 $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
62 my $writer = $attr->get_write_method;
63 confess "No writer for attribute" unless defined($writer);
64 $self->model->$writer($value);
66 my $predicate = $attr->get_predicate_method;
67 confess "No predicate for attribute" unless defined($predicate);
68 if ($self->model->$predicate) {
69 my $clearer = $attr->get_clearer_method;
70 confess "${predicate} returned true but no clearer for attribute"
71 unless defined($clearer);
72 $self->model->$clearer;
77 sub sync_from_action {
79 return unless !$self->needs_sync; # && $self->has_attribute;
80 if( !$self->has_message ){
81 if(my $error = $self->model->error_for($self->attribute) ){
82 $self->message( $error );
87 around accept_events => sub { ('value', shift->(@_)) };
95 Reaction::UI::ViewPort::Role::Actions
99 A role to ease attaching actions to L<Reaction::InterfaceModel::Object>s
117 =head2 sync_from_action
119 =head2 sync_to_action
125 See L<Reaction::Class> for authors.
129 See L<Reaction::Class> for the license.