JsonSQL/Param/ConditionDispatcher.pm

filter: Statementstmt 96.9% 32 / 33 Branchbran 75.0% 3 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 95.6% 44 / 46 CC 4 SCAR 7.4
CC
4
Cov
95%
parse
4.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.1
« JsonSQL/Param/Fields.pm ↑ Summary JsonSQL.pm »
1 # ABSTRACT: A dispatcher module that parses condition statements to create the appropriate JsonSQL Condition objects.
2  
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package JsonSQL::Param::ConditionDispatcher;
9  
10 1 our $VERSION = '0.4'; # VERSION
11  
12 1 ▶use Class::Load qw( try_load_class );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use List::Util qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use JsonSQL::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 ## Define the subclasses that support different condition operators.
19 1 my %opmap = (
20 'TestCondition' => ['eq','ne','gt','ge','lt','le'],
21 'RangeCondition' => ['bt','nb'],
22 'EnumCondition' => ['in','ni'],
23 'NullCondition' => ['isnull','notnull'],
24 'LogicCondition' => ['and','or']
25 );
26  
27  
28  
29 sub parse {
30 7 ▶ my ( $caller, $conditionhashref, $queryObj, $default_table_rules ) = @_;
Subroutine parse: called CC=4 SCAR=13.9
Pod documented
31
32 ## The schema restricts to one condition operator per level of the hashref, so just grab the first key at this level to use as the operator.
33 7 my ( $op ) = keys %{ $conditionhashref };
34
35 ## Determine the appropriate JsonSQL Condition subclass for this operator.
36 7 my $class = "JsonSQL::Param::Conditions::";
37 7 for my $classObj ( keys %opmap ) {
38 35 ▶ if ( any { $_ eq $op } @{ $opmap{$classObj} }) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $_ eq $op; } , @{$opmap{$classObj};})) 7 28 35
39 7 $class .= $classObj;
40 }
41 }
42  
43 ## Load the class and return a new instance if successful. Otherwise return a JsonSQL::Error object.
44 7 my ( $success, $err ) = try_load_class($class);
45 7 ▶ if ( $success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 7 0 7
46 7 return $class->new($conditionhashref, $queryObj, $default_table_rules);
47 } else {
48 0 return JsonSQL::Error->new("jsonsql_condition", $err);
49 }
50 }
51  
52  
53 1 1;
54  
55 __END__
56  
57 =pod
58  
59 =encoding UTF-8
60  
61 =head1 NAME
62  
63 JsonSQL::Param::ConditionDispatcher - A dispatcher module that parses condition statements to create the appropriate JsonSQL Condition objects.
64  
65 =head1 VERSION
66  
67 version 0.4
68  
69 =head1 SYNOPSIS
70  
71 This is a supporting module used by L<JsonSQL::Query> modules for parsing condition operators as parameters for conditional
72 clauses.
73  
74 To use this:
75  
76 my $condObj = JsonSQL::Param::ConditionDispatcher->parse($conditionhashref, $queryObj, $default_table_rules);
77 if ( eval { $condObj->is_error } ) {
78 return "Could not create condition object: $condObj->{message}";
79 } else {
80 ...
81 }
82  
83 The $conditionhashref must contain a single key which is the operator for the condition statement. The operator gets mapped to the
84 appropriate module to load as defined in the %opmap hash. The conditional modules are subclasses of JsonSQL::Param::Condition and
85 reside in JsonSQL::Param::Conditions. The value of the operator key is a hash of args to apply to the operator. For example, a TestCondition
86 needs to have "field" and "value" properties.
87  
88 The collection of conditional modules is fairly complete, but others can be created if need be.
89  
90 =head1 METHODS
91  
92 =head2 Dispatcher parse($conditionhashref, $queryObj, $default_table_rules) -> JsonSQL::Param::Condition
93  
94 Serves as a dispatcher to load the appropriate JsonSQL::Param::Conditions:: class, and create a new instance which is then returned.
95  
96 =head1 AUTHOR
97  
98 Chris Hoefler <bhoefler@draper.com>
99  
100 =head1 COPYRIGHT AND LICENSE
101  
102 This software is copyright (c) 2017 by Chris Hoefler.
103  
104 This is free software; you can redistribute it and/or modify it under
105 the same terms as the Perl 5 programming language system itself.
106  
107 =cut
« JsonSQL/Param/Fields.pm ↑ Summary JsonSQL.pm »