Skip to content

Commit

Permalink
add value trform trait
Browse files Browse the repository at this point in the history
  • Loading branch information
jrockway committed Sep 22, 2008
1 parent 1db6a62 commit be443fe
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
41 changes: 41 additions & 0 deletions 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;
32 changes: 31 additions & 1 deletion 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';
Expand Down Expand Up @@ -85,3 +85,33 @@ chomp(my $exact = <<HERE);
HERE

is $result, $exact, 'got exact expected HTML';



$i = Ernst::Interpreter::TRForm->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 = <<HERE);
<form>
<div id="attribute">
<span class="label">Attribute Label*</span>: <input type="text" value="foo" name="attribute"/></div>
<div id="test">
<p class="instructions">Fill in some test data here.</p>
<span class="label">Test Field*</span>:
<input type="text" value="bar" name="test"/></div>
</form>
HERE

is $result, $exact, 'got exact expected HTML';

0 comments on commit be443fe

Please sign in to comment.