some boiler plate docs
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Field / Role / Mutable.pm
1 package Reaction::UI::ViewPort::Field::Role::Mutable;
2
3 use Reaction::Role;
4
5 use aliased 'Reaction::InterfaceModel::Action';
6 use aliased 'Reaction::Meta::InterfaceModel::Action::ParameterAttribute';
7
8 role Mutable, which {
9   has model     => (is => 'ro', isa => Action, required => 1);
10   has attribute => (is => 'ro', isa => ParameterAttribute, required => 1);
11
12   has value      => (is => 'rw', lazy_build => 1, trigger_adopt('value'));
13   has needs_sync => (is => 'rw', isa => 'Int', default => 0);
14   has message    => (is => 'rw', isa => 'Str');
15
16   implements adopt_value => as {
17     my ($self) = @_;
18     $self->needs_sync(1); # if $self->has_attribute;
19   };
20
21   implements sync_to_action => as {
22     my ($self) = @_;
23     return unless $self->needs_sync && $self->has_value;
24     my $attr = $self->attribute;
25     if (my $tc = $attr->type_constraint) {
26       my $value = $self->value;
27       $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
28       my $error = $tc->validate($self->value); # should we be checking against $value?
29       if (defined $error) {
30         $self->message($error);
31         return;
32       }
33     }
34     my $writer = $attr->get_write_method;
35     confess "No writer for attribute" unless defined($writer);
36     my $value = $self->value;
37     $self->model->$writer($self->value); #should we be passing $value ?
38     $self->needs_sync(0);
39   };
40
41   implements sync_from_action => as {
42     my ($self) = @_;
43     return unless !$self->needs_sync; # && $self->has_attribute;
44     $self->message($self->model->error_for($self->attribute) || '');
45   };
46
47   around accept_events => sub { ('value', shift->(@_)) };
48
49 };
50
51 1;
52
53 =head1 NAME
54
55 Reaction::UI::ViewPort::Role::Actions
56
57 =head1 DESCRIPTION
58
59 A role to ease attaching actions to L<Reaction::InterfaceModel::Object>s
60
61 =head1 ATTRIBUTES
62
63 =head2 needs_sync
64
65 =head2 message
66
67 =head2 model
68
69 =head2 attribute
70
71 =head2 value
72
73 =head1 METHODS
74
75 =head2 accept_events
76
77 =head2 sync_from_action
78
79 =head2 sync_to_action
80
81 =head2 adopt_value
82
83 =head1 AUTHORS
84
85 See L<Reaction::Class> for authors.
86
87 =head1 LICENSE
88
89 See L<Reaction::Class> for the license.
90
91 =cut