Commit | Line | Data |
ddccc6a2 |
1 | package Reaction::UI::ViewPort::Field::Role::Mutable; |
2 | |
9be81e19 |
3 | use Reaction::Role::Parameterized; |
c8fbb8ad |
4 | |
ddccc6a2 |
5 | use aliased 'Reaction::InterfaceModel::Action'; |
6 | use aliased 'Reaction::Meta::InterfaceModel::Action::ParameterAttribute'; |
7b5e71ad |
7 | use MooseX::Types::Moose qw/Int Str/; |
81393881 |
8 | use namespace::clean -except => [ qw(meta) ]; |
9 | |
22a2b243 |
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 | |
81393881 |
29 | has model => (is => 'ro', isa => Action, required => 1); |
30 | has attribute => (is => 'ro', isa => ParameterAttribute, required => 1); |
31 | |
32 | has value => ( |
22a2b243 |
33 | is => 'rw', lazy_build => 1, trigger => sub { shift->adopt_value }, |
22a2b243 |
34 | $p->has_value_type? (isa => $p->value_type) : () |
81393881 |
35 | ); |
22a2b243 |
36 | |
7b5e71ad |
37 | has needs_sync => (is => 'rw', isa => Int, default => 0); #should be bool? |
114916fc |
38 | |
7b5e71ad |
39 | has message => (is => 'rw', isa => Str, clearer => 'clear_message'); |
81393881 |
40 | |
7b5e71ad |
41 | has is_modified => ( #sould be bool? |
22a2b243 |
42 | is => 'ro', writer => '_set_modified', |
029c34a8 |
43 | required => 1, default => 1, init_arg => undef |
44 | ); |
45 | |
81393881 |
46 | after clear_value => sub { |
47 | my $self = shift; |
48 | $self->clear_message if $self->has_message; |
49 | $self->needs_sync(1); |
50 | }; |
114916fc |
51 | |
81393881 |
52 | sub adopt_value { |
53 | my ($self) = @_; |
54 | $self->clear_message if $self->has_message; |
55 | $self->needs_sync(1); # if $self->has_attribute; |
114916fc |
56 | } |
57 | |
28a7e0a7 |
58 | |
81393881 |
59 | sub can_sync_to_action { |
60 | my $self = shift; |
28a7e0a7 |
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; |
81393881 |
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; |
f25cb331 |
74 | } |
5ea9eefd |
75 | } |
81393881 |
76 | } else { |
86db4be7 |
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 | } |
81393881 |
83 | } |
84 | return 1; |
85 | }; |
28a7e0a7 |
86 | |
87 | |
81393881 |
88 | sub sync_to_action { |
89 | my ($self) = @_; |
28a7e0a7 |
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. |
81393881 |
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); |
ddccc6a2 |
105 | } |
81393881 |
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) = @_; |
5ad52d3a |
123 | return if $self->needs_sync; |
81393881 |
124 | if( !$self->has_message ){ |
125 | if(my $error = $self->model->error_for($self->attribute) ){ |
126 | $self->message( $error ); |
577fe414 |
127 | } |
81393881 |
128 | } |
129 | }; |
130 | |
131 | around accept_events => sub { ('value', shift->(@_)) }; |
ddccc6a2 |
132 | |
22a2b243 |
133 | }; |
ddccc6a2 |
134 | |
c8fbb8ad |
135 | |
136 | 1; |
2dba7201 |
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 |