File Coverage

blib/lib/Data/TreeValidator/Branch.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Data::TreeValidator::Branch;
2             {
3             $Data::TreeValidator::Branch::VERSION = '0.04';
4             }
5             # ABSTRACT: A branch of tree validation
6 2     2   2260 use Moose;
  0            
  0            
7             use namespace::autoclean;
8              
9             use Data::TreeValidator::Types qw( HashTree Node );
10             use MooseX::Types::Moose qw( CodeRef Maybe Str );
11             use MooseX::Types::Structured qw( Map );
12              
13             use aliased 'Data::TreeValidator::Result::Branch' => 'Result',;
14             use MooseX::Params::Validate;
15              
16             with 'Data::TreeValidator::Node';
17              
18             has 'children' => (
19             isa => Map[ Str, Node ],
20             default => sub { {} },
21             traits => [ 'Hash' ],
22             handles => {
23             children => 'values',
24             child_names => 'keys',
25             add_child => 'set',
26             child => 'get',
27             }
28             );
29              
30             has 'cross_validator' => (
31             isa => CodeRef,
32             traits => [ 'Code' ],
33             default => sub { sub {} },
34             handles => {
35             cross_validate => 'execute',
36             }
37             );
38              
39             sub process {
40             my $self = shift;
41             my ($tree) = pos_validated_list([ shift ],
42             { isa => Maybe[HashTree], coerce => 1 }
43             );
44             my %args = @_;
45              
46             my $result = Result->new(
47             input => $tree,
48             results => {
49             map {
50             my $process = $tree->{$_} || $args{initialize}->{$_};
51             $_ => $self->child($_)->process($process)
52             } $self->child_names
53             }
54             );
55              
56             # XXX Try::Tiny doesn't work here -- why?
57             eval {
58             $self->cross_validate($result->clean);
59             };
60             if ($@) {
61             $result->add_error($@);
62             }
63              
64             return $result;
65             }
66              
67             1;
68              
69              
70             __END__
71             =pod
72              
73             =encoding utf-8
74              
75             =head1 NAME
76              
77             Data::TreeValidator::Branch - A branch of tree validation
78              
79             =head1 DESCRIPTION
80              
81             Represents a branch in the tree, ie something that has child nodes. Almost all
82             your validation specifications will have at least one tree (unless it really
83             only takes a single piece of input).
84              
85             =head1 METHODS
86              
87             =head2 children
88              
89             Returns a list of all child nodes of this branch
90              
91             =head2 child_names
92              
93             Returns the names of all child nodes for this branch
94              
95             =head2 add_child($name => $value)
96              
97             Adds a child to the branch, with the name C<$name> and value C<$value>
98              
99             =head2 child($name)
100              
101             Returns the child with name C<$name>, or C<undef>
102              
103             =head2 process($input)
104              
105             Takes the input in C<$input> and validates it against this branch. The general
106             process here is to enumerate over each child of this branch, and fetch the
107             corresponding data from C<$input> and process that.
108              
109             Returns a L<Data::TreeValidator:::Result::Branch>, which can be inspected to
110             determine if the branch validated, and for clean data.
111              
112             =head1 AUTHOR
113              
114             Oliver Charles
115              
116             =head1 COPYRIGHT AND LICENSE
117              
118             This software is copyright (c) 2012 by Oliver Charles <oliver.g.charles@googlemail.com>.
119              
120             This is free software; you can redistribute it and/or modify it under
121             the same terms as the Perl 5 programming language system itself.
122              
123             =cut
124