1 package Reaction::UI::ViewPort::Field::Role::Mutable;
3 use Reaction::Role::Parameterized;
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) ];
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
21 parameter value_type => (
22 predicate => 'has_value_type'
29 has model => (is => 'ro', isa => Action, required => 1);
30 has attribute => (is => 'ro', isa => ParameterAttribute, required => 1);
33 is => 'rw', lazy_build => 1, trigger => sub { shift->adopt_value },
34 $p->has_value_type? (isa => $p->value_type) : ()
37 has needs_sync => (is => 'rw', isa => Int, default => 0); #should be bool?
39 has message => (is => 'rw', isa => Str, clearer => 'clear_message');
41 has is_modified => ( #sould be bool?
42 is => 'ro', writer => '_set_modified',
43 required => 1, default => 1, init_arg => undef
46 after clear_value => sub {
48 $self->clear_message if $self->has_message;
54 $self->clear_message if $self->has_message;
55 $self->needs_sync(1); # if $self->has_attribute;
59 sub can_sync_to_action {
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;
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);
77 if( $self->model->attribute_is_required($attr) ){
78 if(my $error = $self->model->error_for($self->attribute) ){
79 $self->message( $error );
91 # don't sync if we're already synced
92 return unless $self->needs_sync;
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;
98 my $attr = $self->attribute;
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);
106 my $writer = $attr->get_write_method;
107 confess "No writer for attribute" unless defined($writer);
108 $self->model->$writer($value);
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;
119 $self->needs_sync(0);
121 sub sync_from_action {
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 );
131 around accept_events => sub { ('value', shift->(@_)) };
140 Reaction::UI::ViewPort::Role::Actions
144 A role to ease attaching actions to L<Reaction::InterfaceModel::Object>s
162 =head2 sync_from_action
164 =head2 sync_to_action
170 See L<Reaction::Class> for authors.
174 See L<Reaction::Class> for the license.