Commit | Line | Data |
ddccc6a2 |
1 | package Reaction::UI::ViewPort::Field::Role::Mutable; |
2 | |
c8fbb8ad |
3 | use Reaction::Role; |
4 | |
ddccc6a2 |
5 | use aliased 'Reaction::InterfaceModel::Action'; |
6 | use aliased 'Reaction::Meta::InterfaceModel::Action::ParameterAttribute'; |
7 | |
81393881 |
8 | use namespace::clean -except => [ qw(meta) ]; |
9 | |
10 | has model => (is => 'ro', isa => Action, required => 1); |
11 | has attribute => (is => 'ro', isa => ParameterAttribute, required => 1); |
12 | |
13 | has value => ( |
14 | is => 'rw', lazy_build => 1, trigger_adopt('value'), |
15 | clearer => 'clear_value', |
16 | ); |
17 | has needs_sync => (is => 'rw', isa => 'Int', default => 0); |
114916fc |
18 | |
81393881 |
19 | has message => (is => 'rw', isa => 'Str', clearer => 'clear_message'); |
20 | |
029c34a8 |
21 | has is_modified => ( |
22 | is => 'ro', writer => '_set_modified', |
23 | required => 1, default => 1, init_arg => undef |
24 | ); |
25 | |
81393881 |
26 | after clear_value => sub { |
27 | my $self = shift; |
28 | $self->clear_message if $self->has_message; |
29 | $self->needs_sync(1); |
30 | }; |
114916fc |
31 | |
81393881 |
32 | sub adopt_value { |
33 | my ($self) = @_; |
34 | $self->clear_message if $self->has_message; |
35 | $self->needs_sync(1); # if $self->has_attribute; |
114916fc |
36 | } |
37 | |
28a7e0a7 |
38 | |
81393881 |
39 | sub can_sync_to_action { |
40 | my $self = shift; |
28a7e0a7 |
41 | |
42 | # if field is already sync'ed, it can be sync'ed again |
43 | # this will make sync_to_action no-op if needs_sync is 0 |
44 | return 1 unless $self->needs_sync; |
81393881 |
45 | my $attr = $self->attribute; |
46 | |
47 | if ($self->has_value) { |
48 | my $value = $self->value; |
49 | if (my $tc = $attr->type_constraint) { |
50 | $value = $tc->coercion->coerce($value) if ($tc->has_coercion); |
51 | if (defined (my $error = $tc->validate($value))) { |
52 | $self->message($error); |
53 | return; |
f25cb331 |
54 | } |
5ea9eefd |
55 | } |
81393881 |
56 | } else { |
86db4be7 |
57 | if( $self->model->attribute_is_required($attr) ){ |
58 | if(my $error = $self->model->error_for($self->attribute) ){ |
59 | $self->message( $error ); |
60 | } |
61 | return; |
62 | } |
81393881 |
63 | } |
64 | return 1; |
65 | }; |
28a7e0a7 |
66 | |
67 | |
81393881 |
68 | sub sync_to_action { |
69 | my ($self) = @_; |
28a7e0a7 |
70 | |
71 | # don't sync if we're already synced |
72 | return unless $self->needs_sync; |
73 | |
74 | # if we got here, needs_sync is 1 |
75 | # can_sync_to_action will do coercion checks, etc. |
81393881 |
76 | return unless $self->can_sync_to_action; |
77 | |
78 | my $attr = $self->attribute; |
79 | |
80 | if ($self->has_value) { |
81 | my $value = $self->value; |
82 | if (my $tc = $attr->type_constraint) { |
83 | #this will go away when we have moose dbic. until then though... |
84 | $value = $tc->coercion->coerce($value) if ($tc->has_coercion); |
ddccc6a2 |
85 | } |
81393881 |
86 | my $writer = $attr->get_write_method; |
87 | confess "No writer for attribute" unless defined($writer); |
88 | $self->model->$writer($value); |
89 | } else { |
90 | my $predicate = $attr->get_predicate_method; |
91 | confess "No predicate for attribute" unless defined($predicate); |
92 | if ($self->model->$predicate) { |
93 | my $clearer = $attr->get_clearer_method; |
94 | confess "${predicate} returned true but no clearer for attribute" |
95 | unless defined($clearer); |
96 | $self->model->$clearer; |
97 | } |
98 | } |
99 | $self->needs_sync(0); |
100 | }; |
101 | sub sync_from_action { |
102 | my ($self) = @_; |
5ad52d3a |
103 | return if $self->needs_sync; |
81393881 |
104 | if( !$self->has_message ){ |
105 | if(my $error = $self->model->error_for($self->attribute) ){ |
106 | $self->message( $error ); |
577fe414 |
107 | } |
81393881 |
108 | } |
109 | }; |
110 | |
111 | around accept_events => sub { ('value', shift->(@_)) }; |
ddccc6a2 |
112 | |
ddccc6a2 |
113 | |
c8fbb8ad |
114 | |
115 | 1; |
2dba7201 |
116 | |
117 | =head1 NAME |
118 | |
119 | Reaction::UI::ViewPort::Role::Actions |
120 | |
121 | =head1 DESCRIPTION |
122 | |
123 | A role to ease attaching actions to L<Reaction::InterfaceModel::Object>s |
124 | |
125 | =head1 ATTRIBUTES |
126 | |
127 | =head2 needs_sync |
128 | |
129 | =head2 message |
130 | |
131 | =head2 model |
132 | |
133 | =head2 attribute |
134 | |
135 | =head2 value |
136 | |
137 | =head1 METHODS |
138 | |
139 | =head2 accept_events |
140 | |
141 | =head2 sync_from_action |
142 | |
143 | =head2 sync_to_action |
144 | |
145 | =head2 adopt_value |
146 | |
147 | =head1 AUTHORS |
148 | |
149 | See L<Reaction::Class> for authors. |
150 | |
151 | =head1 LICENSE |
152 | |
153 | See L<Reaction::Class> for the license. |
154 | |
155 | =cut |