File Coverage

blib/lib/Data/TreeValidator/Sugar.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package Data::TreeValidator::Sugar;
2             {
3             $Data::TreeValidator::Sugar::VERSION = '0.04';
4             }
5             # ABSTRACT: Syntatic sugar for easily creating tree validators
6 2     2   49946 use strict;
  2         5  
  2         72  
7 2     2   10 use warnings;
  2         3  
  2         59  
8              
9 2     2   2830 use aliased 'Data::TreeValidator::Branch';
  2         1765  
  2         11  
10             use aliased 'Data::TreeValidator::Leaf';
11             use aliased 'Data::TreeValidator::RepeatingBranch';
12              
13             use Data::TreeValidator::Constraints 'type';
14              
15             use Sub::Exporter -setup => {
16             exports => [ qw( branch leaf repeating ) ],
17             };
18              
19             use MooseX::Params::Validate;
20             use MooseX::Types::Moose qw( CodeRef );
21              
22             sub _branch {
23             my ($class, $code) = @_;
24             my %children = $code->();
25             return $class->new( children => \%children );
26             }
27              
28             sub branch (&;) {
29             my ($code) = pos_validated_list(\@_,
30             { isa => CodeRef }
31             );
32             return _branch(Branch, $code);
33             }
34              
35             sub leaf {
36             my @args = @_ == 1 ? (constraints => [ type($_[0]) ] )
37             : @_;
38             return Leaf->new(@args);
39             }
40              
41             sub repeating (&;) {
42             my ($code) = pos_validated_list(\@_,
43             { isa => CodeRef }
44             );
45             return _branch(RepeatingBranch, $code);
46             }
47              
48             1;
49              
50              
51             __END__
52             =pod
53              
54             =encoding utf-8
55              
56             =head1 NAME
57              
58             Data::TreeValidator::Sugar - Syntatic sugar for easily creating tree validators
59              
60             =head1 DESCRIPTION
61              
62             This module exports a few helper functions which allow you to build up a
63             validation tree in a declarative manner.
64              
65             All methods below are available for import into calling modules.
66              
67             =head1 METHODS
68              
69             =head2 branch { children... }
70              
71             Create a L<Data::TreeValidator::Branch> object, with children. Children are
72             specified in a code ref, that should return a hash (not a reference) of all
73             children as <name, node> pairs.
74              
75             =head2 leaf
76              
77             Constructs a L<Data::TreeValidator::Leaf> object. All parameters passed are
78             passed through to the Leaf constructor.
79              
80             =head2 repeating { children... }
81              
82             Just like L<Data::TreeValidator::Sugar/branch>, but creates a
83             L<Data::TreeValidator::RepeatingBranch> instead of a standard branch.
84              
85             =head1 AUTHOR
86              
87             Oliver Charles
88              
89             =head1 COPYRIGHT AND LICENSE
90              
91             This software is copyright (c) 2012 by Oliver Charles <oliver.g.charles@googlemail.com>.
92              
93             This is free software; you can redistribute it and/or modify it under
94             the same terms as the Perl 5 programming language system itself.
95              
96             =cut
97