add namespace::clean dep
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Field / Role / Mutable.pm
CommitLineData
ddccc6a2 1package Reaction::UI::ViewPort::Field::Role::Mutable;
2
c8fbb8ad 3use Reaction::Role;
4
ddccc6a2 5use aliased 'Reaction::InterfaceModel::Action';
6use aliased 'Reaction::Meta::InterfaceModel::Action::ParameterAttribute';
7
81393881 8use namespace::clean -except => [ qw(meta) ];
9
10has model => (is => 'ro', isa => Action, required => 1);
11has attribute => (is => 'ro', isa => ParameterAttribute, required => 1);
12
13has value => (
14 is => 'rw', lazy_build => 1, trigger_adopt('value'),
15 clearer => 'clear_value',
16);
17has needs_sync => (is => 'rw', isa => 'Int', default => 0);
114916fc 18
81393881 19has message => (is => 'rw', isa => 'Str', clearer => 'clear_message');
20
21after clear_value => sub {
22 my $self = shift;
23 $self->clear_message if $self->has_message;
24 $self->needs_sync(1);
25};
114916fc 26
81393881 27sub adopt_value {
28 my ($self) = @_;
29 $self->clear_message if $self->has_message;
30 $self->needs_sync(1); # if $self->has_attribute;
114916fc 31}
32
81393881 33sub can_sync_to_action {
34 my $self = shift;
35 return 1 unless $self->needs_sync;
36 my $attr = $self->attribute;
37
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);
44 return;
f25cb331 45 }
5ea9eefd 46 }
81393881 47 } else {
c0d494fb 48 return if $attr->is_required;
81393881 49 }
50 return 1;
51};
52sub sync_to_action {
53 my ($self) = @_;
81393881 54 return unless $self->can_sync_to_action;
55
56 my $attr = $self->attribute;
57
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);
ddccc6a2 63 }
81393881 64 my $writer = $attr->get_write_method;
65 confess "No writer for attribute" unless defined($writer);
66 $self->model->$writer($value);
67 } else {
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;
75 }
76 }
77 $self->needs_sync(0);
78};
79sub sync_from_action {
80 my ($self) = @_;
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 );
577fe414 85 }
81393881 86 }
87};
88
89around accept_events => sub { ('value', shift->(@_)) };
ddccc6a2 90
ddccc6a2 91
c8fbb8ad 92
931;
2dba7201 94
95=head1 NAME
96
97Reaction::UI::ViewPort::Role::Actions
98
99=head1 DESCRIPTION
100
101A role to ease attaching actions to L<Reaction::InterfaceModel::Object>s
102
103=head1 ATTRIBUTES
104
105=head2 needs_sync
106
107=head2 message
108
109=head2 model
110
111=head2 attribute
112
113=head2 value
114
115=head1 METHODS
116
117=head2 accept_events
118
119=head2 sync_from_action
120
121=head2 sync_to_action
122
123=head2 adopt_value
124
125=head1 AUTHORS
126
127See L<Reaction::Class> for authors.
128
129=head1 LICENSE
130
131See L<Reaction::Class> for the license.
132
133=cut