1 package Reaction::UI::ViewPort::Field::Role::Mutable;
3 use MooseX::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 clearer => 'clear_value',
35 $p->has_value_type? (isa => $p->value_type) : ()
38 has needs_sync => (is => 'rw', isa => Int, default => 0); #should be bool?
40 has message => (is => 'rw', isa => Str, clearer => 'clear_message');
42 has is_modified => ( #sould be bool?
43 is => 'ro', writer => '_set_modified',
44 required => 1, default => 1, init_arg => undef
47 after clear_value => sub {
49 $self->clear_message if $self->has_message;
55 $self->clear_message if $self->has_message;
56 $self->needs_sync(1); # if $self->has_attribute;
60 sub can_sync_to_action {
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;
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);
78 if( $self->model->attribute_is_required($attr) ){
79 if(my $error = $self->model->error_for($self->attribute) ){
80 $self->message( $error );
92 # don't sync if we're already synced
93 return unless $self->needs_sync;
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;
99 my $attr = $self->attribute;
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);
107 my $writer = $attr->get_write_method;
108 confess "No writer for attribute" unless defined($writer);
109 $self->model->$writer($value);
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;
120 $self->needs_sync(0);
122 sub sync_from_action {
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 );
132 around accept_events => sub { ('value', shift->(@_)) };
141 Reaction::UI::ViewPort::Role::Actions
145 A role to ease attaching actions to L<Reaction::InterfaceModel::Object>s
163 =head2 sync_from_action
165 =head2 sync_to_action
171 See L<Reaction::Class> for authors.
175 See L<Reaction::Class> for the license.