blib/lib/Gapp/Form/Context.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 99 CC 10 SCAR 23.3
CC
10
Cov
0%
_defaults
20.0
modify
20.0
lookup
12.0
CRAP 10.3
1 package Gapp::Form::Context;
2 {
3 $Gapp::Form::Context::VERSION = '0.60';
4 }
5  
6 use Moose;
7 use MooseX::StrictConstructor;
8 use MooseX::SemiAffordanceAccessor;
9  
10 use Gapp::Form::Context::Node;
11  
12 has 'accessor' => (
13 is => 'rw',
14 isa => 'CodeRef|Undef',
15 default => undef,
16 );
17  
18 has 'reader_prefix' => (
19 is => 'rw',
20 isa => 'Str|Undef',
21 default => undef,
22 );
23  
24 has 'writer_prefix' => (
25 is => 'rw',
26 isa => 'Str|Undef',
27 default => undef,
28 );
29  
30 has 'nodes' => (
31 is => 'rw',
32 isa => 'HashRef',
33 traits => [qw( Hash )],
34 default => sub { { } },
35 handles => {
36 get_node => 'get',
37 set_node => 'set',
38 }
39 );
40  
41  
42 # returns a list of default values to use when creating a new node
43 sub _defaults {
44 my $self = shift;
45 my @defaults;
46 push @defaults, reader_prefix => $self->reader_prefix, if defined $self->reader_prefix;
47 push @defaults, writer_prefix => $self->writer_prefix, if defined $self->writer_prefix;
48 push @defaults, accessor => $self->accessor, if defined $self->accessor;
49 return @defaults;
50 }
51  
52 # create a new node
53 sub add {
54 my ( $self, $name, $content, @args ) = @_;
55  
56 my $node = Gapp::Form::Context::Node->new( content => $content, $self->_defaults, @args );
57 $self->set_node( $name, $node );
58 return $node;
59 }
60  
61 # create a new node
62 sub add_node {
63 my ( $self, $name, $content, @args ) = @_;
64  
65 use Carp qw(carp);
66 carp 'add_node deprecrated. Use add instead.';
67
68 $self->add( $name, $content, @args );
69 }
70  
71 # used to lookup the value of an attribute
72 sub lookup {
73 my ( $self, $path ) = @_;
74 $self->meta->throw_error( 'you must supply a path' ) if ! $path;
75
76 my ( $name, $attr ) = split /\./, $path;
77 my $node = $self->get_node( $name );
78
79 $self->meta->throw_error( qq[could not find node "$name" in context] ) if ! $node;
80 $node->lookup( $attr );
81 }
82  
83 # used to set the value of an attribute
84 sub modify {
85 my ( $self, $path, $value ) = @_;
86 $self->meta->throw_error( 'you must supply a path' ) if ! $path;
87 $self->meta->throw_error( 'you must supply a value' ) if @_ <= 2;
88
89 my ( $name, $attr ) = split /\./, $path;
90
91 my $node = $self->get_node( $name );
92 return if ! $node;
93 $node->modify( $attr, $value );
94 # $self->_value_changed( $path, $value ) if ! $self->in_update( $path );
95 }
96  
97 sub update {
98 my ( $self, $stash ) = @_;
99
100
101 for my $path ( $stash->elements ) {
102 next if $path eq '';
103
104 my $value = $stash->fetch( $path );
105
106 # $self->set_in_update( $path, 1 );
107 $self->modify( $path, $value );
108 # $self->set_in_update( $path, 0 );
109 }
110 }
111  
112 sub update_from_stash {
113 my $self = shift;
114 use Carp qw( cluck );
115
116 cluck '$cx->update_from_stash( $stash ) deprecated, use $cx->update( $stash )';
117 $self->update( @_ );
118 }
119  
120  
121  
122 1;
123  
124  
125  
126 __END__