c5176eb9f2d2330572cb186cf0836be939ff6429
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Field / Role / Mutable.pm
1 package Reaction::UI::ViewPort::Field::Role::Mutable;
2
3 use Reaction::Role::Parameterized;
4
5 use aliased 'Reaction::InterfaceModel::Action';
6 use aliased 'Reaction::Meta::InterfaceModel::Action::ParameterAttribute';
7 use MooseX::Types::Moose qw/Int Str/;
8 use namespace::clean -except => [ qw(meta) ];
9
10 =pod
11
12 15:24  mst:» I'm not sure I understand why the +foo is overwriting my after'ed clear_value
13 15:24  mst:» but I would argue it shouldn't do that
14 15:25 @doy:» because has '+foo' is creating an entirely new attribute
15 15:25 @doy:» and just copying the meta-attribute's attributes into it
16 15:25 @doy:» so it's creating a new clearer sub in the subclass
17 15:27  rafl:» mst: for that case, i tend to just parameterize the role on whatever i might want to override in its attribute definitions
18
19 =cut
20
21 parameter value_type => (
22     predicate => 'has_value_type'
23 );
24
25 role {
26
27 my $p = shift;
28
29 has model     => (is => 'ro', isa => Action, required => 1);
30 has attribute => (is => 'ro', isa => ParameterAttribute, required => 1);
31
32 has value      => (
33   is => 'rw', lazy_build => 1, trigger => sub { shift->adopt_value },
34   $p->has_value_type? (isa => $p->value_type) : ()
35 );
36
37 has needs_sync => (is => 'rw', isa => Int, default => 0); #should be bool?
38
39 has message => (is => 'rw', isa => Str, clearer => 'clear_message');
40
41 has is_modified => ( #sould be bool?
42   is => 'ro', writer => '_set_modified',
43   required => 1, default => 1, init_arg => undef
44 );
45
46 after clear_value => sub {
47   my $self = shift;
48   $self->clear_message if $self->has_message;
49   $self->needs_sync(1);
50 };
51
52 sub adopt_value {
53   my ($self) = @_;
54   $self->clear_message if $self->has_message;
55   $self->needs_sync(1); # if $self->has_attribute;
56 }
57
58
59 sub can_sync_to_action {
60   my $self = shift;
61
62   # if field is already sync'ed, it can be sync'ed again
63   # this will make sync_to_action no-op if needs_sync is 0
64   return 1 unless $self->needs_sync;
65   my $attr = $self->attribute;
66
67   if ($self->has_value) {
68     my $value = $self->value;
69     if (my $tc = $attr->type_constraint) {
70       $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
71       if (defined (my $error = $tc->validate($value))) {
72         $self->message($error);
73         return;
74       }
75     }
76   } else {
77     if( $self->model->attribute_is_required($attr) ){
78       if(my $error = $self->model->error_for($self->attribute) ){
79         $self->message( $error );
80       }
81       return;
82     }
83   }
84   return 1;
85 };
86
87
88 sub sync_to_action {
89   my ($self) = @_;
90
91   # don't sync if we're already synced
92   return unless $self->needs_sync;
93
94   # if we got here, needs_sync is 1
95   # can_sync_to_action will do coercion checks, etc.
96   return unless $self->can_sync_to_action;
97
98   my $attr = $self->attribute;
99
100   if ($self->has_value) {
101     my $value = $self->value;
102     if (my $tc = $attr->type_constraint) {
103       #this will go away when we have moose dbic. until then though...
104       $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
105     }
106     my $writer = $attr->get_write_method;
107     confess "No writer for attribute" unless defined($writer);
108     $self->model->$writer($value);
109   } else {
110     my $predicate = $attr->get_predicate_method;
111     confess "No predicate for attribute" unless defined($predicate);
112     if ($self->model->$predicate) {
113       my $clearer = $attr->get_clearer_method;
114       confess "${predicate} returned true but no clearer for attribute"
115         unless defined($clearer);
116       $self->model->$clearer;
117     }
118   }
119   $self->needs_sync(0);
120 };
121 sub sync_from_action {
122   my ($self) = @_;
123   return if $self->needs_sync;
124   if( !$self->has_message ){
125     if(my $error = $self->model->error_for($self->attribute) ){
126       $self->message( $error );
127     }
128   }
129 };
130
131 around accept_events => sub { ('value', shift->(@_)) };
132
133 };
134
135
136 1;
137
138 =head1 NAME
139
140 Reaction::UI::ViewPort::Role::Actions
141
142 =head1 DESCRIPTION
143
144 A role to ease attaching actions to L<Reaction::InterfaceModel::Object>s
145
146 =head1 ATTRIBUTES
147
148 =head2 needs_sync
149
150 =head2 message
151
152 =head2 model
153
154 =head2 attribute
155
156 =head2 value
157
158 =head1 METHODS
159
160 =head2 accept_events
161
162 =head2 sync_from_action
163
164 =head2 sync_to_action
165
166 =head2 adopt_value
167
168 =head1 AUTHORS
169
170 See L<Reaction::Class> for authors.
171
172 =head1 LICENSE
173
174 See L<Reaction::Class> for the license.
175
176 =cut