1 package Reaction::UI::ViewPort::Field::Role::Mutable;
5 use aliased 'Reaction::InterfaceModel::Action';
6 use aliased 'Reaction::Meta::InterfaceModel::Action::ParameterAttribute';
9 has model => (is => 'ro', isa => Action, required => 1);
10 has attribute => (is => 'ro', isa => ParameterAttribute, required => 1);
13 is => 'rw', lazy_build => 1, trigger_adopt('value'),
14 clearer => 'clear_value',
16 has needs_sync => (is => 'rw', isa => 'Int', default => 0);
17 #predicates are autmagically generated for lazy and non-required attrs
18 has message => (is => 'rw', isa => 'Str', clearer => 'clear_message');
20 after clear_value => sub {
22 $self->clear_message if $self->has_message;
26 implements adopt_value => as {
28 $self->clear_message if $self->has_message;
29 $self->needs_sync(1); # if $self->has_attribute;
32 implements can_sync_to_action => as {
34 return 1 unless $self->needs_sync;
35 my $attr = $self->attribute;
37 if ($self->has_value) {
38 my $value = $self->value;
39 if (my $tc = $attr->type_constraint) {
40 $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
41 if (defined (my $error = $tc->validate($value))) {
42 $self->message($error);
47 return if $attr->is_required;
52 implements sync_to_action => as {
54 return unless $self->needs_sync;
55 return unless $self->can_sync_to_action;
57 my $attr = $self->attribute;
59 if ($self->has_value) {
60 my $value = $self->value;
61 if (my $tc = $attr->type_constraint) {
62 #this will go away when we have moose dbic. until then though...
63 $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
65 my $writer = $attr->get_write_method;
66 confess "No writer for attribute" unless defined($writer);
67 $self->model->$writer($value);
69 my $predicate = $attr->predicate;
70 confess "No predicate for attribute" unless defined($predicate);
71 if ($self->model->$predicate) {
72 my $clearer = $attr->clearer;
73 confess "${predicate} returned true but no clearer for attribute"
74 unless defined($clearer);
75 $self->model->$clearer;
81 implements sync_from_action => as {
83 return unless !$self->needs_sync; # && $self->has_attribute;
84 $self->message($self->model->error_for($self->attribute) || '');
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.