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