From be443fe39c6673c46db422d608408fc732c98e3c Mon Sep 17 00:00:00 2001 From: Jonathan Rockway Date: Mon, 22 Sep 2008 04:41:41 -0500 Subject: [PATCH] add value trform trait --- lib/Ernst/Interpreter/TRForm/Trait/Value.pm | 41 +++++++++++++++++++++ t/trform-basic.t | 32 +++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 lib/Ernst/Interpreter/TRForm/Trait/Value.pm diff --git a/lib/Ernst/Interpreter/TRForm/Trait/Value.pm b/lib/Ernst/Interpreter/TRForm/Trait/Value.pm new file mode 100644 index 0000000..859708d --- /dev/null +++ b/lib/Ernst/Interpreter/TRForm/Trait/Value.pm @@ -0,0 +1,41 @@ +package Ernst::Interpreter::TRForm::Trait::Value; +use Moose::Role; +use Ernst::Interpreter::TRForm::Utils qw(simple_replace); + +has 'value_replacement_region' => ( + is => 'ro', + isa => 'Str', + required => 1, + default => sub { '//input | //textarea' }, +); + +around transform_attribute => sub { + my ($next, $self, $attribute, $fragment, $instance) = @_; + + my $value = eval { $attribute->get_value($instance) }; + + if(defined $value){ + $fragment = simple_replace( + $fragment, + $self->value_replacement_region, + 'Replace' => sub { + my $n = shift; + my $copy = $n->cloneNode(1); + + my $type = $n->nodeName; + if($type eq 'input'){ + $copy->setAttribute( value => $value ); + } + + else { + $copy->addChild( XML::LibXML::Text->new($value) ); + } + + return $copy; + }, + ); + } + return $self->$next($attribute, $fragment, $instance); +}; + +1; diff --git a/t/trform-basic.t b/t/trform-basic.t index 356e73d..e6aee6e 100644 --- a/t/trform-basic.t +++ b/t/trform-basic.t @@ -1,6 +1,6 @@ use strict; use warnings; -use Test::More tests => 10; +use Test::More tests => 14; use Test::Exception; use ok 'Ernst::Interpreter::TRForm'; @@ -85,3 +85,33 @@ chomp(my $exact = <new_with_traits( + traits => [qw/Namespace Label Instructions Value/], + representation => $html, + class => Class->meta, + namespace => '', +); +ok $i; + +my $instance = Class->new( attribute => 'foo', test => 'bar' ); + +lives_ok { + $result = $i->interpret($instance); +} 'interpret lived'; + +ok( $result = $result->render ); +chomp($exact = < +
+ Attribute Label*:
+
+

Fill in some test data here.

+ Test Field*: +
+ +HERE + +is $result, $exact, 'got exact expected HTML';