Gapp
view release on metacpan or search on metacpan
lib/Gapp/Form/Context.pm view on Meta::CPAN
set_node => 'set',
}
);
# returns a list of default values to use when creating a new node
sub _defaults {
my $self = shift;
my @defaults;
push @defaults, reader_prefix => $self->reader_prefix, if defined $self->reader_prefix;
push @defaults, writer_prefix => $self->writer_prefix, if defined $self->writer_prefix;
push @defaults, accessor => $self->accessor, if defined $self->accessor;
return @defaults;
}
# create a new node
sub add {
my ( $self, $name, $content, @args ) = @_;
my $node = Gapp::Form::Context::Node->new( content => $content, $self->_defaults, @args );
$self->set_node( $name, $node );
return $node;
}
# create a new node
sub add_node {
my ( $self, $name, $content, @args ) = @_;
use Carp qw(carp);
carp 'add_node deprecrated. Use add instead.';
$self->add( $name, $content, @args );
}
# used to lookup the value of an attribute
sub lookup {
my ( $self, $path ) = @_;
$self->meta->throw_error( 'you must supply a path' ) if ! $path;
my ( $name, $attr ) = split /\./, $path;
my $node = $self->get_node( $name );
$self->meta->throw_error( qq[could not find node "$name" in context] ) if ! $node;
$node->lookup( $attr );
}
# used to set the value of an attribute
sub modify {
my ( $self, $path, $value ) = @_;
$self->meta->throw_error( 'you must supply a path' ) if ! $path;
$self->meta->throw_error( 'you must supply a value' ) if @_ <= 2;
my ( $name, $attr ) = split /\./, $path;
my $node = $self->get_node( $name );
return if ! $node;
$node->modify( $attr, $value );
# $self->_value_changed( $path, $value ) if ! $self->in_update( $path );
}
sub update {
my ( $self, $stash ) = @_;
for my $path ( $stash->elements ) {
next if $path eq '';
my $value = $stash->fetch( $path );
# $self->set_in_update( $path, 1 );
$self->modify( $path, $value );
# $self->set_in_update( $path, 0 );
}
}
sub update_from_stash {
my $self = shift;
use Carp qw( cluck );
cluck '$cx->update_from_stash( $stash ) deprecated, use $cx->update( $stash )';
$self->update( @_ );
}
1;
__END__
=pod
=head1 NAME
Gapp::Form::Context - Form context object
=head1 SYNOPSIS
# use an object
$o = Foo::Character->new( fname => 'Mickey', lname => 'Mouse' );
$cx = Gapp::Form::Context->new(
reader_prefix => 'get_',
writer_prefix => 'set_'
);
$cx->add( 'character', $o );
$cx->lookup( 'character.fname' ); # returns 'Mickey'
# use a hash-ref
$data = { foo => 'bar' };
$cx->add( data => $data,
accessor => sub {
my ( $data, $attr, $value ) = @_;
@_ == 2 ? $data{$attr} : $data{$attr} = $value;
}
);
$cx->lookup( 'data.foo' ); # returns 'Bar'
=head1 DESCRIPTION
The context is used to sync data between objects/data structures and forms.
( run in 1.041 second using v1.01-cache-2.11-cpan-39bf76dae61 )