move redirect_to to a role and deprecate it
[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 use MooseX::Types::Moose qw/Int Str/;
8
9 use namespace::clean -except => [ qw(meta) ];
10
11 has model     => (is => 'ro', isa => Action, required => 1);
12 has attribute => (is => 'ro', isa => ParameterAttribute, required => 1);
13
14 has value      => (
15   is => 'rw', lazy_build => 1, trigger_adopt('value'),
16   clearer => 'clear_value',
17 );
18 has needs_sync => (is => 'rw', isa => Int, default => 0); #should be bool?
19
20 has message => (is => 'rw', isa => Str, clearer => 'clear_message');
21
22 has is_modified => ( #sould be bool?
23   is => 'ro', writer => '_set_modified', 
24   required => 1, default => 1, init_arg => undef
25 );
26
27 after clear_value => sub {
28   my $self = shift;
29   $self->clear_message if $self->has_message;
30   $self->needs_sync(1);
31 };
32
33 sub adopt_value {
34   my ($self) = @_;
35   $self->clear_message if $self->has_message;
36   $self->needs_sync(1); # if $self->has_attribute;
37 }
38
39
40 sub can_sync_to_action {
41   my $self = shift;
42
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;
47
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);
54         return;
55       }
56     }
57   } else {
58     if( $self->model->attribute_is_required($attr) ){
59       if(my $error = $self->model->error_for($self->attribute) ){
60         $self->message( $error );
61       }
62       return;
63     }
64   }
65   return 1;
66 };
67
68
69 sub sync_to_action {
70   my ($self) = @_;
71
72   # don't sync if we're already synced
73   return unless $self->needs_sync;
74
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;
78
79   my $attr = $self->attribute;
80
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);
86     }
87     my $writer = $attr->get_write_method;
88     confess "No writer for attribute" unless defined($writer);
89     $self->model->$writer($value);
90   } else {
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;
98     }
99   }
100   $self->needs_sync(0);
101 };
102 sub sync_from_action {
103   my ($self) = @_;
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 );
108     }
109   }
110 };
111
112 around accept_events => sub { ('value', shift->(@_)) };
113
114
115
116 1;
117
118 =head1 NAME
119
120 Reaction::UI::ViewPort::Role::Actions
121
122 =head1 DESCRIPTION
123
124 A role to ease attaching actions to L<Reaction::InterfaceModel::Object>s
125
126 =head1 ATTRIBUTES
127
128 =head2 needs_sync
129
130 =head2 message
131
132 =head2 model
133
134 =head2 attribute
135
136 =head2 value
137
138 =head1 METHODS
139
140 =head2 accept_events
141
142 =head2 sync_from_action
143
144 =head2 sync_to_action
145
146 =head2 adopt_value
147
148 =head1 AUTHORS
149
150 See L<Reaction::Class> for authors.
151
152 =head1 LICENSE
153
154 See L<Reaction::Class> for the license.
155
156 =cut