From 62ffa273e3c709800ea7f4a19f9f28e9b1da5867 Mon Sep 17 00:00:00 2001 From: matthewt Date: Sun, 3 Feb 2008 14:03:21 +0000 Subject: simple mutable fields now use value_string (ChooseMany unconverted) --- lib/Reaction/UI/ViewPort/Field/Mutable/Boolean.pm | 7 ++++- .../UI/ViewPort/Field/Mutable/ChooseOne.pm | 34 +++++++++----------- lib/Reaction/UI/ViewPort/Field/Mutable/DateTime.pm | 18 +---------- lib/Reaction/UI/ViewPort/Field/Mutable/Integer.pm | 8 ++++- lib/Reaction/UI/ViewPort/Field/Mutable/Number.pm | 7 ++++- lib/Reaction/UI/ViewPort/Field/Mutable/Password.pm | 8 ++++- lib/Reaction/UI/ViewPort/Field/Mutable/String.pm | 8 ++++- lib/Reaction/UI/ViewPort/Field/Mutable/Text.pm | 8 ++++- lib/Reaction/UI/ViewPort/Field/Role/Mutable.pm | 2 +- .../UI/ViewPort/Field/Role/Mutable/Simple.pm | 36 ++++++++++++++++++++++ 10 files changed, 92 insertions(+), 44 deletions(-) create mode 100644 lib/Reaction/UI/ViewPort/Field/Role/Mutable/Simple.pm (limited to 'lib/Reaction/UI/ViewPort/Field') diff --git a/lib/Reaction/UI/ViewPort/Field/Mutable/Boolean.pm b/lib/Reaction/UI/ViewPort/Field/Mutable/Boolean.pm index 5293b11..633f910 100644 --- a/lib/Reaction/UI/ViewPort/Field/Mutable/Boolean.pm +++ b/lib/Reaction/UI/ViewPort/Field/Mutable/Boolean.pm @@ -3,7 +3,12 @@ package Reaction::UI::ViewPort::Field::Mutable::Boolean; use Reaction::Class; class Boolean is 'Reaction::UI::ViewPort::Field::Boolean', which{ - does 'Reaction::UI::ViewPort::Field::Role::Mutable'; + does 'Reaction::UI::ViewPort::Field::Role::Mutable::Simple'; + + implements adopt_value_string => as { + my ($self) = @_; + $self->value($self->value_string); + }; implements BUILD => as { my($self) = @_; diff --git a/lib/Reaction/UI/ViewPort/Field/Mutable/ChooseOne.pm b/lib/Reaction/UI/ViewPort/Field/Mutable/ChooseOne.pm index f16ea66..9033528 100644 --- a/lib/Reaction/UI/ViewPort/Field/Mutable/ChooseOne.pm +++ b/lib/Reaction/UI/ViewPort/Field/Mutable/ChooseOne.pm @@ -5,28 +5,23 @@ use Scalar::Util (); class ChooseOne is 'Reaction::UI::ViewPort::Field', which { - does 'Reaction::UI::ViewPort::Field::Role::Mutable'; + does 'Reaction::UI::ViewPort::Field::Role::Mutable::Simple'; does 'Reaction::UI::ViewPort::Field::Role::Choices'; - around value => sub { - my $orig = shift; - my $self = shift; - return $orig->($self) unless @_; - my $value = shift; - if (defined $value) { - $value = $self->str_to_ident($value) if (!ref $value); - my $attribute = $self->attribute; - my $checked = $attribute->check_valid_value($self->model, $value); - unless (defined $checked) { - require Data::Dumper; - my $serialised = Data::Dumper->new([ $value ])->Indent(0)->Dump; - $serialised =~ s/^\$VAR1 = //; $serialised =~ s/;$//; - confess "${serialised} is not a valid value for ${\$attribute->name} on " - ."${\$attribute->associated_class->name}"; - } - $value = $checked; + implements adopt_value_string => as { + my ($self) = @_; + my $value = $self->value_string; + $value = $self->str_to_ident($value) if (!ref $value); + my $attribute = $self->attribute; + my $checked = $attribute->check_valid_value($self->model, $value); + unless (defined $checked) { + require Data::Dumper; + my $serialised = Data::Dumper->new([ $value ])->Indent(0)->Dump; + $serialised =~ s/^\$VAR1 = //; $serialised =~ s/;$//; + confess "${serialised} is not a valid value for ${\$attribute->name} on " + ."${\$attribute->associated_class->name}"; } - $orig->($self, $value); + $self->value($checked); }; around _value_string_from_value => sub { @@ -47,7 +42,6 @@ class ChooseOne is 'Reaction::UI::ViewPort::Field', which { return $self->obj_to_str($our_value) eq $check_value; }; - }; 1; diff --git a/lib/Reaction/UI/ViewPort/Field/Mutable/DateTime.pm b/lib/Reaction/UI/ViewPort/Field/Mutable/DateTime.pm index 8bde97f..3b38d26 100644 --- a/lib/Reaction/UI/ViewPort/Field/Mutable/DateTime.pm +++ b/lib/Reaction/UI/ViewPort/Field/Mutable/DateTime.pm @@ -7,21 +7,7 @@ use DateTime; class 'Reaction::UI::ViewPort::Field::Mutable::DateTime', is 'Reaction::UI::ViewPort::Field::DateTime', which { - does 'Reaction::UI::ViewPort::Field::Role::Mutable'; - - has value_string => - ( is => 'rw', isa => 'Str', lazy_build => 1, trigger_adopt('value_string') ); - - around value_string => sub { - my $orig = shift; - my $self = shift; - if (@_ && defined($_[0]) && !ref($_[0]) && $_[0] eq '' - && !$self->value_is_required) { - $self->clear_value; - return undef; - } - return $self->$orig(@_); - }; + does 'Reaction::UI::ViewPort::Field::Role::Mutable::Simple'; implements adopt_value_string => as { my ($self) = @_; @@ -35,8 +21,6 @@ class 'Reaction::UI::ViewPort::Field::Mutable::DateTime', } }; - around accept_events => sub { ('value_string', shift->(@_)) }; - }; 1; diff --git a/lib/Reaction/UI/ViewPort/Field/Mutable/Integer.pm b/lib/Reaction/UI/ViewPort/Field/Mutable/Integer.pm index 4882f1e..958150a 100644 --- a/lib/Reaction/UI/ViewPort/Field/Mutable/Integer.pm +++ b/lib/Reaction/UI/ViewPort/Field/Mutable/Integer.pm @@ -3,7 +3,13 @@ package Reaction::UI::ViewPort::Field::Mutable::Integer; use Reaction::Class; class Integer is 'Reaction::UI::ViewPort::Field::Integer', which { - does 'Reaction::UI::ViewPort::Field::Role::Mutable'; + does 'Reaction::UI::ViewPort::Field::Role::Mutable::Simple'; + + implements adopt_value_string => as { + my ($self) = @_; + $self->value($self->value_string); + }; + }; 1; diff --git a/lib/Reaction/UI/ViewPort/Field/Mutable/Number.pm b/lib/Reaction/UI/ViewPort/Field/Mutable/Number.pm index 41308f3..d2be595 100644 --- a/lib/Reaction/UI/ViewPort/Field/Mutable/Number.pm +++ b/lib/Reaction/UI/ViewPort/Field/Mutable/Number.pm @@ -3,7 +3,12 @@ package Reaction::UI::ViewPort::Field::Mutable::Number; use Reaction::Class; class Number is 'Reaction::UI::ViewPort::Field::Number', which { - does 'Reaction::UI::ViewPort::Field::Role::Mutable'; + does 'Reaction::UI::ViewPort::Field::Role::Mutable::Simple'; + + implements adopt_value_string => as { + my ($self) = @_; + $self->value($self->value_string); + }; }; 1; diff --git a/lib/Reaction/UI/ViewPort/Field/Mutable/Password.pm b/lib/Reaction/UI/ViewPort/Field/Mutable/Password.pm index 79319f2..d009698 100644 --- a/lib/Reaction/UI/ViewPort/Field/Mutable/Password.pm +++ b/lib/Reaction/UI/ViewPort/Field/Mutable/Password.pm @@ -3,7 +3,13 @@ package Reaction::UI::ViewPort::Field::Mutable::Password; use Reaction::Class; class Password is 'Reaction::UI::ViewPort::Field::String', which { - does 'Reaction::UI::ViewPort::Field::Role::Mutable'; + does 'Reaction::UI::ViewPort::Field::Role::Mutable::Simple'; + + implements adopt_value_string => as { + my ($self) = @_; + $self->value($self->value_string); + }; + }; 1; diff --git a/lib/Reaction/UI/ViewPort/Field/Mutable/String.pm b/lib/Reaction/UI/ViewPort/Field/Mutable/String.pm index 758673c..11d5d14 100644 --- a/lib/Reaction/UI/ViewPort/Field/Mutable/String.pm +++ b/lib/Reaction/UI/ViewPort/Field/Mutable/String.pm @@ -3,7 +3,13 @@ package Reaction::UI::ViewPort::Field::Mutable::String; use Reaction::Class; class String is 'Reaction::UI::ViewPort::Field::String', which { - does 'Reaction::UI::ViewPort::Field::Role::Mutable'; + does 'Reaction::UI::ViewPort::Field::Role::Mutable::Simple'; + + implements adopt_value_string => as { + my ($self) = @_; + $self->value($self->value_string); + }; + }; 1; diff --git a/lib/Reaction/UI/ViewPort/Field/Mutable/Text.pm b/lib/Reaction/UI/ViewPort/Field/Mutable/Text.pm index 31d3b04..09d2127 100644 --- a/lib/Reaction/UI/ViewPort/Field/Mutable/Text.pm +++ b/lib/Reaction/UI/ViewPort/Field/Mutable/Text.pm @@ -3,7 +3,13 @@ package Reaction::UI::ViewPort::Field::Mutable::Text; use Reaction::Class; class Text is 'Reaction::UI::ViewPort::Field::Text', which { - does 'Reaction::UI::ViewPort::Field::Role::Mutable'; + does 'Reaction::UI::ViewPort::Field::Role::Mutable::Simple'; + + implements adopt_value_string => as { + my ($self) = @_; + $self->value($self->value_string); + }; + }; 1; diff --git a/lib/Reaction/UI/ViewPort/Field/Role/Mutable.pm b/lib/Reaction/UI/ViewPort/Field/Role/Mutable.pm index 4d58745..42899c0 100644 --- a/lib/Reaction/UI/ViewPort/Field/Role/Mutable.pm +++ b/lib/Reaction/UI/ViewPort/Field/Role/Mutable.pm @@ -14,7 +14,7 @@ role Mutable, which { clearer => 'clear_value', ); has needs_sync => (is => 'rw', isa => 'Int', default => 0); - has message => (is => 'rw', isa => 'Str'); + has message => (is => 'rw', isa => 'Str'); after clear_value => sub { shift->needs_sync(1); diff --git a/lib/Reaction/UI/ViewPort/Field/Role/Mutable/Simple.pm b/lib/Reaction/UI/ViewPort/Field/Role/Mutable/Simple.pm new file mode 100644 index 0000000..66fa464 --- /dev/null +++ b/lib/Reaction/UI/ViewPort/Field/Role/Mutable/Simple.pm @@ -0,0 +1,36 @@ +package Reaction::UI::ViewPort::Field::Role::Mutable::Simple; + +use Reaction::Role; + +use aliased 'Reaction::UI::ViewPort::Field::Role::Mutable'; + +role Simple which { + + does Mutable; + + has value_string => ( + is => 'rw', lazy_build => 1, trigger_adopt('value_string'), + clearer => 'clear_value', + ); + + around value_string => sub { + my $orig = shift; + my $self = shift; + if (@_ && defined($_[0]) && !ref($_[0]) && $_[0] eq '' + && !$self->value_is_required) { + $self->clear_value; + return undef; + } + return $self->$orig(@_); + }; + + # the user needs to implement this because, honestly, you're always going + # to need to do something custom and the only common thing really is + # "you probably set $self->value at the end" + requires 'adopt_value_string'; + + around accept_events => sub { ('value_string', shift->(@_)) }; + +}; + +1; -- cgit v1.2.3-54-g00ecf