blib/lib/Email/MIME/Kit/Validator/Rx.pm

filter: Statementstmt 97.4% 75 / 77 Branchbran 71.4% 10 / 14 Conditioncond 66.6% 6 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 5 totaltotal 86.1% 112 / 130 CC 15 SCAR 13.6
CC
15
Cov
91%
_do_goofy_schema_initialization
9.2
build_default_rx_object
4.1
__ANON__
3.5
CRAP 3.9
↑ Summary
1 package Email::MIME::Kit::Validator::Rx 0.200002;
2 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 with 'Email::MIME::Kit::Role::Validator';
4 # ABSTRACT: validate assembly stash with Rx (from JSON in kit)
5  
6 1 ▶use Data::Rx 0.007;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Data::Rx::TypeBundle::Perl 0.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 #pod =head1 SYNOPSIS
13 #pod
14 #pod Email::MIME::Kit::Validator::Rx is a Validator plugin for Email::MIME::Kit that
15 #pod allows an Rx schema to be used to validate kit assembly data.
16 #pod
17 #pod A simple mkit's manifest might include the following:
18 #pod
19 #pod {
20 #pod "renderer" : "TT",
21 #pod "validator": "Rx",
22 #pod "header" : [ ... mail headers ... ],
23 #pod "type" : "text/plain",
24 #pod "path" : "path/to/template.txt"
25 #pod }
26 #pod
27 #pod In this simple configuration, the use of "Rx" as the validator will load the
28 #pod plugin in its simplest configuration. It will look for a file called
29 #pod F<rx.json> in the kit and will load its contents (as JSON) and use them as a
30 #pod schema to validate the data passed to the it's C<assemble> method.
31 #pod
32 #pod More complex configurations are simple.
33 #pod
34 #pod This configuration supplies an alternate filename for the JSON file:
35 #pod
36 #pod "validator": [ "Rx", { "path": "rx-schema.json" } ],
37 #pod
38 #pod This configuration supplies the schema definition inline:
39 #pod
40 #pod "validator": [
41 #pod "Rx",
42 #pod {
43 #pod "schema": {
44 #pod "type" : "//rec",
45 #pod "required": {
46 #pod "subject": "//str",
47 #pod "rcpt" : { "type": "/perl/obj", "isa": "Email::Address" }
48 #pod }
49 #pod }
50 #pod }
51 #pod ]
52 #pod
53 #pod Notice, above, the C</perl/> prefix. By default,
54 #pod L<Data::Rx::TypeBundle::Perl|Data::Rx::TypeBundle::Perl> is loaded along with
55 #pod the core types.
56 #pod
57 #pod If a C<combine> argument is given, multiple schema definitions may be provided.
58 #pod They will be combined with the logic named by the combine argument. In this
59 #pod release, only "all" is valid, and will require all schemata to match. Here is
60 #pod an example:
61 #pod
62 #pod "validator": [
63 #pod "Rx",
64 #pod {
65 #pod "combine": "all",
66 #pod "path" : "rx.json",
67 #pod "schema" : [
68 #pod { "type": "//rec", "rest": "//any", "required": { "foo": "//int" } },
69 #pod { "type": "//rec", "rest": "//any", "required": { "bar": "//int" } },
70 #pod ]
71 #pod }
72 #pod ]
73 #pod
74 #pod This definition will create an C<//all> schema with three entries: the schema
75 #pod found in F<rx.json> and the two schemata given in the array value of C<schema>.
76 #pod
77 #pod =cut
78  
79 has prefix => (
80 is => 'ro',
81 isa => 'HashRef',
82 3 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
83 1 );
84  
85 has type_plugins => (
86 is => 'ro',
87 isa => 'ArrayRef[Str]',
88 3 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
89 1 );
90  
91 1 has rx => (
92 is => 'ro',
93 isa => class_type('Data::Rx'),
94 lazy => 1,
95 init_arg => undef,
96 builder => 'build_default_rx_object',
97 );
98  
99 3 ▶sub build_default_rx_object {
Subroutine build_default_rx_object: called CC=4 SCAR=14.0
Pod undocumented
100 3 my ($self) = @_;
101 3 my $rx = Data::Rx->new({
102 prefix => $self->prefix,
103 });
104  
105 3 for my $plugin ($self->all_default_type_plugins, @{ $self->type_plugins }) {
106 3 ▶ eval "require $plugin; 1" or die;
Branch: 1 branch
BranchTrueFalseTotal
eval "require $plugin; 1" or die 3 0 3
107 3 $rx->register_type_plugin($plugin);
108 }
109  
110 3 my $prefix = $self->prefix;
111 3 for my $key (keys %$prefix) {
112 0 $rx->add_prefix($key, $prefix->{ $key });
113 }
114  
115 3 return $rx;
116 }
117  
118 3 ▶sub all_default_type_plugins {
Subroutine all_default_type_plugins: called CC=2 SCAR=6.9
Pod undocumented
119 # shamlessly stolen from Moose::Object::BUILDALL -- rjbs, 2009-03-06
120 3 my ($self) = @_;
121 3 my @plugins;
122 3 for my $method (
123 reverse
124 $self->meta->find_all_methods_by_name('accumulate_default_type_plugins')
125 ) {
126 3 push @plugins, $method->{code}->execute($self);
127 }
128  
129 3 return @plugins;
130 }
131  
132 3 ▶sub accumulate_default_type_plugins {
Subroutine accumulate_default_type_plugins: called CC=1 SCAR=0.0
Pod undocumented
133 3 return ('Data::Rx::TypeBundle::Perl');
134 }
135  
136 1 has schema => (
137 reader => 'schema',
138 writer => '_set_schema',
139 isa => 'Object', # It'd be nice to have a better TC -- rjbs, 2009-03-06
140 init_arg => undef,
141 );
142  
143 1 has schema_struct => (
144 reader => '_schema_struct',
145 init_arg => 'schema',
146 );
147  
148 1 has schema_path => (
149 reader => '_schema_path',
150 writer => '_set_schema_path',
151 isa => 'Str',
152 init_arg => 'path',
153 );
154  
155 has combine => (
156 is => 'ro',
157 1 ▶ initializer => sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
158 1 my ($self, $value, $set) = @_;
159 1 ▶ confess "invalid combine logic: $value"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $value eq "all" 0 1 1
Condition33%1/3
defined $value and $value eq "all"
!ll&&!rl&&r
001
MC/DC0%0/2
defined $value and $value eq "all"
defined $value $value eq "all"
Truth table33%1/3
A and B
A= defined $valueB= $value eq "all"
ABresult
0X0
100
111
160 unless defined $value and $value eq 'all';
161 1 $set->($value);
162 },
163 1 );
164  
165 3 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
166 3 my ($self) = @_;
167  
168 3 $self->_do_goofy_schema_initialization;
169 }
170  
171 3 ▶sub _do_goofy_schema_initialization {
Subroutine _do_goofy_schema_initialization: called CC=9 SCAR=22.2
172 3 my ($self) = @_;
173  
174 3 ▶ my @paths = grep { defined } ref $self->_schema_path
Branch: 1 branch
BranchTrueFalseTotal
ref $self->_schema_path ? : 0 3 3
175 0 ? @{ $self->_schema_path }
176 : $self->_schema_path;
177  
178 4 ▶ my @structs = grep { defined } (ref $self->_schema_struct eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
ref $self->_schema_struct eq 'ARRAY' ? : 1 2 3
179 3 ? @{ $self->_schema_struct }
180 : $self->_schema_struct;
181  
182 3 ▶ confess("multiple schemata provided but no combine logic given")
Branch: 1 branch
BranchTrueFalseTotal
if @paths + @structs > 1 and not $self->combine 0 3 3
Condition66%2/3
@paths + @structs > 1 and not $self->combine
!ll&&!rl&&r
210
MC/DC0%0/2
@paths + @structs > 1 and not $self->combine
@paths + @structs > 1 not $self->combine
Truth table66%2/3
A and not(B)
A= @paths + @structs > 1B= not $self->combine
ABresult
0X0
100
111
183 if @paths + @structs > 1 and ! $self->combine;
184  
185 3 ▶ @paths = ('rx.json') unless @paths or @structs;
Branch: 1 branch
BranchTrueFalseTotal
unless @paths or @structs 1 2 3
Condition100%3/3
@paths or @structs
l!l&&r!l&&!r
111
MC/DC100%2/2
@paths or @structs
@paths @structs
Truth table100%3/3
A or B
A= @pathsB= @structs
ABresult
1X1
011
000
186  
187 3 for my $path (@paths) {
188 # Sure, someday we can add another decoder layer here to allow schemata in
189 # YAML. Whatever. -- rjbs, 2009-03-06
190 2 my $rx_json_ref = $self->kit->get_kit_entry($path);
191 2 my $rx_data = JSON->new->decode($$rx_json_ref);
192 2 push @structs, $rx_data;
193 }
194  
195 3 ▶ my $schema = @structs > 1
Branch: 1 branch
BranchTrueFalseTotal
@structs > 1 ? : 1 2 3
196 ? $self->rx->make_schema({ type => '//all', of => \@structs })
197 : $self->rx->make_schema($structs[0]);
198  
199 3 $self->_set_schema($schema);
200 }
201  
202 9 ▶sub validate {
Subroutine validate: called CC=1 SCAR=0.0
Pod undocumented
203 9 my ($self, $stash) = @_;
204  
205 9 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
206 9 $self->schema->assert_valid($stash);
207 3 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
208 3 Carp::confess("assembly parameters don't pass validation: $_");
209 9 };
210  
211 6 return 1;
212 }
213  
214 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
215 1 ▶no Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
216 1 __PACKAGE__->meta->make_immutable;
217 1 1;
218  
219 __END__
↑ Summary