1 package Reaction::UI::ViewPort::Field::Role::Mutable;
5 use aliased 'Reaction::InterfaceModel::Action';
6 use aliased 'Reaction::Meta::InterfaceModel::Action::ParameterAttribute';
7 use MooseX::Types::Moose qw/Int Str/;
9 use namespace::clean -except => [ qw(meta) ];
11 has model => (is => 'ro', isa => Action, required => 1);
12 has attribute => (is => 'ro', isa => ParameterAttribute, required => 1);
15 is => 'rw', lazy_build => 1, trigger_adopt('value'),
16 clearer => 'clear_value',
18 has needs_sync => (is => 'rw', isa => Int, default => 0); #should be bool?
20 has message => (is => 'rw', isa => Str, clearer => 'clear_message');
22 has is_modified => ( #sould be bool?
23 is => 'ro', writer => '_set_modified',
24 required => 1, default => 1, init_arg => undef
27 after clear_value => sub {
29 $self->clear_message if $self->has_message;
35 $self->clear_message if $self->has_message;
36 $self->needs_sync(1); # if $self->has_attribute;
40 sub can_sync_to_action {
43 # if field is already sync'ed, it can be sync'ed again
44 # this will make sync_to_action no-op if needs_sync is 0
45 return 1 unless $self->needs_sync;
46 my $attr = $self->attribute;
48 if ($self->has_value) {
49 my $value = $self->value;
50 if (my $tc = $attr->type_constraint) {
51 $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
52 if (defined (my $error = $tc->validate($value))) {
53 $self->message($error);
58 if( $self->model->attribute_is_required($attr) ){
59 if(my $error = $self->model->error_for($self->attribute) ){
60 $self->message( $error );
72 # don't sync if we're already synced
73 return unless $self->needs_sync;
75 # if we got here, needs_sync is 1
76 # can_sync_to_action will do coercion checks, etc.
77 return unless $self->can_sync_to_action;
79 my $attr = $self->attribute;
81 if ($self->has_value) {
82 my $value = $self->value;
83 if (my $tc = $attr->type_constraint) {
84 #this will go away when we have moose dbic. until then though...
85 $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
87 my $writer = $attr->get_write_method;
88 confess "No writer for attribute" unless defined($writer);
89 $self->model->$writer($value);
91 my $predicate = $attr->get_predicate_method;
92 confess "No predicate for attribute" unless defined($predicate);
93 if ($self->model->$predicate) {
94 my $clearer = $attr->get_clearer_method;
95 confess "${predicate} returned true but no clearer for attribute"
96 unless defined($clearer);
97 $self->model->$clearer;
100 $self->needs_sync(0);
102 sub sync_from_action {
104 return if $self->needs_sync;
105 if( !$self->has_message ){
106 if(my $error = $self->model->error_for($self->attribute) ){
107 $self->message( $error );
112 around accept_events => sub { ('value', shift->(@_)) };
120 Reaction::UI::ViewPort::Role::Actions
124 A role to ease attaching actions to L<Reaction::InterfaceModel::Object>s
142 =head2 sync_from_action
144 =head2 sync_to_action
150 See L<Reaction::Class> for authors.
154 See L<Reaction::Class> for the license.