r31711@martha (orig r1246): groditi | 2009-10-02 17:01:17 -0400
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Field / Role / Mutable.pm
1 package Reaction::UI::ViewPort::Field::Role::Mutable;
2
3 use MooseX::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   clearer => 'clear_value',
35   $p->has_value_type? (isa => $p->value_type) : ()
36 );
37
38 has needs_sync => (is => 'rw', isa => Int, default => 0); #should be bool?
39
40 has message => (is => 'rw', isa => Str, clearer => 'clear_message');
41
42 has is_modified => ( #sould be bool?
43   is => 'ro', writer => '_set_modified',
44   required => 1, default => 1, init_arg => undef
45 );
46
47 after clear_value => sub {
48   my $self = shift;
49   $self->clear_message if $self->has_message;
50   $self->needs_sync(1);
51 };
52
53 sub adopt_value {
54   my ($self) = @_;
55   $self->clear_message if $self->has_message;
56   $self->needs_sync(1); # if $self->has_attribute;
57 }
58
59
60 sub can_sync_to_action {
61   my $self = shift;
62
63   # if field is already sync'ed, it can be sync'ed again
64   # this will make sync_to_action no-op if needs_sync is 0
65   return 1 unless $self->needs_sync;
66   my $attr = $self->attribute;
67
68   if ($self->has_value) {
69     my $value = $self->value;
70     if (my $tc = $attr->type_constraint) {
71       $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
72       if (defined (my $error = $tc->validate($value))) {
73         $self->message($error);
74         return;
75       }
76     }
77   } else {
78     if( $self->model->attribute_is_required($attr) ){
79       if(my $error = $self->model->error_for($self->attribute) ){
80         $self->message( $error );
81       }
82       return;
83     }
84   }
85   return 1;
86 };
87
88
89 sub sync_to_action {
90   my ($self) = @_;
91
92   # don't sync if we're already synced
93   return unless $self->needs_sync;
94
95   # if we got here, needs_sync is 1
96   # can_sync_to_action will do coercion checks, etc.
97   return unless $self->can_sync_to_action;
98
99   my $attr = $self->attribute;
100
101   if ($self->has_value) {
102     my $value = $self->value;
103     if (my $tc = $attr->type_constraint) {
104       #this will go away when we have moose dbic. until then though...
105       $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
106     }
107     my $writer = $attr->get_write_method;
108     confess "No writer for attribute" unless defined($writer);
109     $self->model->$writer($value);
110   } else {
111     my $predicate = $attr->get_predicate_method;
112     confess "No predicate for attribute" unless defined($predicate);
113     if ($self->model->$predicate) {
114       my $clearer = $attr->get_clearer_method;
115       confess "${predicate} returned true but no clearer for attribute"
116         unless defined($clearer);
117       $self->model->$clearer;
118     }
119   }
120   $self->needs_sync(0);
121 };
122 sub sync_from_action {
123   my ($self) = @_;
124   return if $self->needs_sync;
125   if( !$self->has_message ){
126     if(my $error = $self->model->error_for($self->attribute) ){
127       $self->message( $error );
128     }
129   }
130 };
131
132 around accept_events => sub { ('value', shift->(@_)) };
133
134 };
135
136
137 1;
138
139 =head1 NAME
140
141 Reaction::UI::ViewPort::Role::Actions
142
143 =head1 DESCRIPTION
144
145 A role to ease attaching actions to L<Reaction::InterfaceModel::Object>s
146
147 =head1 ATTRIBUTES
148
149 =head2 needs_sync
150
151 =head2 message
152
153 =head2 model
154
155 =head2 attribute
156
157 =head2 value
158
159 =head1 METHODS
160
161 =head2 accept_events
162
163 =head2 sync_from_action
164
165 =head2 sync_to_action
166
167 =head2 adopt_value
168
169 =head1 AUTHORS
170
171 See L<Reaction::Class> for authors.
172
173 =head1 LICENSE
174
175 See L<Reaction::Class> for the license.
176
177 =cut