File Coverage

blib/lib/DBIx/Class/Smooth/Q.pm
Criterion Covered Total %
statement 74 89 83.1
branch 13 18 72.2
condition 10 15 66.6
subroutine 15 16 93.7
pod 0 4 0.0
total 112 142 78.8


line stmt bran cond sub pod time code
1 3     3   97984 use 5.20.0;
  3         20  
2 3     3   18 use strict;
  3         11  
  3         59  
3 3     3   14 use warnings;
  3         8  
  3         194  
4              
5             package DBIx::Class::Smooth::Q;
6              
7             # ABSTRACT: Short intro
8             our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
9             our $VERSION = '0.0101';
10              
11 3     3   18 use Carp qw/croak/;
  3         6  
  3         191  
12 3     3   433 use Safe::Isa qw/$_isa/;
  3         452  
  3         341  
13 3     3   594 use List::SomeUtils qw/any/;
  3         12836  
  3         187  
14 3     3   1643 use Moo;
  3         20899  
  3         17  
15 3         78 use Sub::Exporter::Progressive -setup => {
16             exports => [qw/Q/],
17             groups => {
18             default => [qw/Q/],
19             },
20 3     3   4471 };
  3         9  
21              
22             use overload
23 3         22 '&' => 'do_and',
24             '|' => 'do_or',
25 3     3   1680 '~' => 'do_not';
  3         982  
26              
27 3     3   798 use experimental qw/signatures postderef/;
  3         3363  
  3         21  
28              
29             has value => (
30             is => 'rw',
31             );
32              
33 37     37 0 9607 sub Q(@args) {
  37         76  
  37         58  
34 37 100 66     105 if(scalar @args == 1 && $args[0]->$_isa('DBIx::Class::Smooth::Q')) {
35 3         62 return $args[0];
36             }
37 34         590 return DBIx::Class::Smooth::Q->new(value => [-and => \@args]);
38             }
39              
40 12     12   17 sub _prepare_do_and($self, $other) {
  12         16  
  12         17  
  12         14  
41 12         47 my %self_values_as_flathash = ($self->value->[1]->@*);
42 12         32 my @self_valuekeys = keys %self_values_as_flathash;
43              
44 12 50   20   61 if(any { $_ eq '-and' } @self_valuekeys) {
  20         44  
45             ATTEMPT:
46 0         0 while(1) {
47             VALUE:
48 0         0 for (my $i = 0; $i < scalar $self->value->[1]->@*; $i += 2) {
49 0         0 my $key = $self->value->[1][$i];
50 0 0       0 if($key eq '-and') {
51 0         0 push $other->value->[1]->@* => $self->value->[1][$i+1]->@*;
52 0         0 splice $self->value->[1]->@*, $i, 2;
53 0         0 next ATTEMPT;
54             }
55             }
56 0         0 last ATTEMPT;
57             }
58             }
59              
60 12         53 return $self, $other;
61             }
62              
63 12     12 0 144 sub do_and($self, $other, $swap) {
  12         19  
  12         17  
  12         15  
  12         17  
64 12         26 ($self, $other) = $self->_prepare_do_and($other);
65 12         25 my $self_value = $self->value;
66 12         19 my $other_value = $other->value;
67              
68 12 100       27 if($self_value->[0] eq '-and') {
69 9         16 $self_value = $self_value->[1];
70             }
71 12 100       24 if($other_value->[0] eq '-and') {
72 9         14 $other_value = $other_value->[1];
73             }
74              
75 12         34 my $value = [-and => [$self_value->@*, $other_value->@* ]];
76              
77 12         214 return DBIx::Class::Smooth::Q->new(value => $value);
78             }
79              
80 15     15 0 247 sub do_or($self, $other, $swap) {
  15         23  
  15         23  
  15         23  
  15         19  
81 15         30 my $self_value = $self->value;
82 15         28 my $other_value = $other->value;
83              
84 15 100 100     55 if($self_value->[0] eq '-or' && $self_value->[1][0] eq '-or') {
85 4         19 splice $self_value->[1]->@*, 0, 2, $self_value->[1][1]->@*;
86             }
87 15 50 33     37 if($other_value->[0] eq '-or' && $other_value->[1][0] eq '-or') {
88 0         0 splice $other_value->[1]->@*, 0, 2, $other_value->[1][1]->@*;
89             }
90              
91 15 100 100     52 if($self_value->[0] eq '-and' && scalar $self_value->[1]->@* == 2) {
92 6         12 $self_value = $self_value->[1];
93             }
94 15 50 33     49 if($other_value->[0] eq '-and' && scalar $other_value->[1]->@* == 2) {
95 15         21 $other_value = $other_value->[1];
96             }
97 15         43 my $value = [-or => [$self_value->@*, $other_value->@* ]];
98              
99 15         264 return DBIx::Class::Smooth::Q->new(value => $value);
100             }
101              
102 0     0 0   sub do_not($self, $undef, $swap) {
  0            
  0            
  0            
  0            
103 0           return DBIx::Class::Smooth::Q->new(value => [-not_bool => [$self->value->@*]]);
104             }
105              
106             1;
107              
108             __END__
109              
110             =pod
111              
112             =encoding UTF-8
113              
114             =head1 NAME
115              
116             DBIx::Class::Smooth::Q - Short intro
117              
118             =head1 VERSION
119              
120             Version 0.0101, released 2018-11-29.
121              
122             =head1 SOURCE
123              
124             L<https://github.com/Csson/p5-DBIx-Class-Smooth>
125              
126             =head1 HOMEPAGE
127              
128             L<https://metacpan.org/release/DBIx-Class-Smooth>
129              
130             =head1 AUTHOR
131              
132             Erik Carlsson <info@code301.com>
133              
134             =head1 COPYRIGHT AND LICENSE
135              
136             This software is copyright (c) 2018 by Erik Carlsson.
137              
138             This is free software; you can redistribute it and/or modify it under
139             the same terms as the Perl 5 programming language system itself.
140              
141             =cut