| 1 | package Search::Query::Clause; | ||||||||||||||||||||||
| 2 | 9 | ▶ | use Moo; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 3 | 9 | ▶ | use Carp; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | 9 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | use overload | ||||||||||||||||||||||
| 6 | 4 | ▶ | '""' => sub { $_[0]->stringify; }, | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | 577 | ▶ | 'bool' => sub {1}, | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | 9 | ▶ | fallback => 1; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | |||||||||||||||||||||||
| 10 | 9 | ▶ | use namespace::autoclean; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | |||||||||||||||||||||||
| 12 | 9 | our $VERSION = '0.306'; | |||||||||||||||||||||
| 13 | |||||||||||||||||||||||
| 14 | 9 | has 'field' => ( is => 'rw' ); | |||||||||||||||||||||
| 15 | 9 | has 'op' => ( is => 'rw' ); | |||||||||||||||||||||
| 16 | 9 | has 'value' => ( is => 'rw' ); | |||||||||||||||||||||
| 17 | 9 | has 'quote' => ( is => 'rw' ); | |||||||||||||||||||||
| 18 | 9 | has 'proximity' => ( is => 'rw' ); | |||||||||||||||||||||
| 19 | |||||||||||||||||||||||
| 20 | =head1 NAME | ||||||||||||||||||||||
| 21 | |||||||||||||||||||||||
| 22 | Search::Query::Clause - part of a Dialect | ||||||||||||||||||||||
| 23 | |||||||||||||||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||||||||||||||
| 25 | |||||||||||||||||||||||
| 26 | my $clause = Search::Query::Clause->new( | ||||||||||||||||||||||
| 27 | field => 'color', | ||||||||||||||||||||||
| 28 | op => '=', | ||||||||||||||||||||||
| 29 | value => 'green', | ||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||
| 31 | my $query = Search::Query->parser->parse("color=red"); | ||||||||||||||||||||||
| 32 | $query->add_or_clause( $clause ); | ||||||||||||||||||||||
| 33 | print $query; # +color=red color=green | ||||||||||||||||||||||
| 34 | |||||||||||||||||||||||
| 35 | =head1 DESCRIPTION | ||||||||||||||||||||||
| 36 | |||||||||||||||||||||||
| 37 | A Clause object represents a leaf in a Query Dialect tree. | ||||||||||||||||||||||
| 38 | |||||||||||||||||||||||
| 39 | =head1 METHODS | ||||||||||||||||||||||
| 40 | |||||||||||||||||||||||
| 41 | =head2 field | ||||||||||||||||||||||
| 42 | |||||||||||||||||||||||
| 43 | =head2 op | ||||||||||||||||||||||
| 44 | |||||||||||||||||||||||
| 45 | =head2 value | ||||||||||||||||||||||
| 46 | |||||||||||||||||||||||
| 47 | =head2 quote | ||||||||||||||||||||||
| 48 | |||||||||||||||||||||||
| 49 | =head2 proximity | ||||||||||||||||||||||
| 50 | |||||||||||||||||||||||
| 51 | =head2 is_tree | ||||||||||||||||||||||
| 52 | |||||||||||||||||||||||
| 53 | Returns true if the Clause has child Clauses. | ||||||||||||||||||||||
| 54 | |||||||||||||||||||||||
| 55 | =cut | ||||||||||||||||||||||
| 56 | |||||||||||||||||||||||
| 57 | sub is_tree { | ||||||||||||||||||||||
| 58 | 487 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
is_tree: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 59 | 487 | return blessed( $self->{value} ); | |||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||
| 61 | |||||||||||||||||||||||
| 62 | =head2 has_children | ||||||||||||||||||||||
| 63 | |||||||||||||||||||||||
| 64 | Returns the number of child Clauses if is_tree() is true. | ||||||||||||||||||||||
| 65 | |||||||||||||||||||||||
| 66 | Returns undef if is_tree() is false. | ||||||||||||||||||||||
| 67 | |||||||||||||||||||||||
| 68 | =cut | ||||||||||||||||||||||
| 69 | |||||||||||||||||||||||
| 70 | sub has_children { | ||||||||||||||||||||||
| 71 | 50 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
has_children: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||
| 72 | 50 | ▶ | return undef unless $self->is_tree; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 73 | 50 | my $clauses = 0; | |||||||||||||||||||||
| 74 | $self->{value}->walk( | ||||||||||||||||||||||
| 75 | sub { | ||||||||||||||||||||||
| 76 | 92 | ▶ | my ( $clause, $dialect, $code, $prefix ) = @_; | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||
| 77 | 92 | $clauses++; | |||||||||||||||||||||
| 78 | 92 | ▶ | if ( $clause->is_tree ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 79 | 0 | $clause->{value}->walk($code); | |||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||
| 82 | 50 | ); | |||||||||||||||||||||
| 83 | 50 | return $clauses; | |||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||
| 85 | |||||||||||||||||||||||
| 86 | =head2 stringify | ||||||||||||||||||||||
| 87 | |||||||||||||||||||||||
| 88 | Returns Clause as a string. Like Dialect, string-like operators | ||||||||||||||||||||||
| 89 | are overloaded to call stringify(). | ||||||||||||||||||||||
| 90 | |||||||||||||||||||||||
| 91 | NOTE that stringify() is not necessarily called by the parent | ||||||||||||||||||||||
| 92 | Dialect object when the Dialect object is stringifying itself. | ||||||||||||||||||||||
| 93 | |||||||||||||||||||||||
| 94 | =cut | ||||||||||||||||||||||
| 95 | |||||||||||||||||||||||
| 96 | sub stringify { | ||||||||||||||||||||||
| 97 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
stringify: called CC=9 SCAR=31.5
Pod
documented
| |||||||||||||||||||||||
| 98 | 4 | ▶ | if ( $self->is_tree ) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 99 | 1 | return sprintf( "(%s)", $self->value ); | |||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||
| 101 | elsif ( $self->proximity ) { | ||||||||||||||||||||||
| 102 | 0 | return sprintf( "%s%s%s%s%s~%d", | |||||||||||||||||||||
| 103 | $self->field, $self->op, $self->quote, | ||||||||||||||||||||||
| 104 | $self->value, $self->quote, $self->proximity ); | ||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||
| 106 | elsif ( $self->quote ) { | ||||||||||||||||||||||
| 107 | 0 | return sprintf( "%s%s%s%s%s", | |||||||||||||||||||||
| 108 | $self->field, $self->op, $self->quote, | ||||||||||||||||||||||
| 109 | $self->value, $self->quote ); | ||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||
| 111 | elsif ( ref $self->value ) { | ||||||||||||||||||||||
| 112 | 0 | ▶ | if ( $self->op eq '..' ) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 113 | 0 | return sprintf( "%s=(%s..%s)", | |||||||||||||||||||||
| 114 | $self->field, $self->value->[0], $self->value->[1] ); | ||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||
| 116 | elsif ( $self->op eq '!..' ) { | ||||||||||||||||||||||
| 117 | 0 | return sprintf( "%s!=(%s..%s)", | |||||||||||||||||||||
| 118 | $self->field, $self->value->[0], $self->value->[1] ); | ||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||
| 121 | else { | ||||||||||||||||||||||
| 122 | 3 | ▶ | return sprintf( "%s%s%s", | ||||||||||||||||||||
|
Condition50%2/4
$self->field || ''
$self->op || ''
MC/DC0%0/1
$self->field || ''
$self->field
MC/DC0%0/1
$self->op || ''
$self->op
Truth table50%1/2
A
A= $self->field
Truth table50%1/2
A
A= $self->op
| |||||||||||||||||||||||
| 123 | ( $self->field || '' ), | ||||||||||||||||||||||
| 124 | ( $self->op || '' ), | ||||||||||||||||||||||
| 125 | $self->value, ); | ||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||
| 127 | |||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||
| 129 | |||||||||||||||||||||||
| 130 | 9 | 1; | |||||||||||||||||||||
| 131 | |||||||||||||||||||||||
| 132 | __END__ | ||||||||||||||||||||||