File Coverage

blib/lib/OPTIMADE/Filter/Property.pm
Criterion Covered Total %
statement 53 55 96.3
branch 15 24 62.5
condition n/a
subroutine 14 15 93.3
pod 0 5 0.0
total 82 99 82.8


line stmt bran cond sub pod time code
1             package OPTIMADE::Filter::Property;
2              
3 7     7   946 use strict;
  7         20  
  7         203  
4 7     7   35 use warnings;
  7         11  
  7         212  
5              
6 7     7   541 use parent 'OPTIMADE::Filter::Modifiable';
  7         328  
  7         33  
7 7     7   431 use Scalar::Util qw(blessed);
  7         13  
  7         1288  
8              
9             our $VERSION = '0.10.2'; # VERSION
10              
11 2498     2498   5357 use overload '@{}' => sub { return $_[0]->{name} },
12 505     505   58547 '""' => sub { return $_[0]->to_filter },
13 0     0   0 '==' => sub { return $_[0]->_eq( $_[1] ) },
14 7     7   7879 'eq' => sub { return $_[0]->_eq( $_[1] ) };
  7     1   6282  
  7         124  
  1         214  
15              
16             our $identifier_re = q/([a-z_][a-z0-9_]*)/;
17              
18             sub new {
19 239     239 0 534 my $class = shift;
20 239         1065 return bless { name => \@_ }, $class;
21             }
22              
23             sub to_filter
24             {
25 735     735 0 1265 my( $self ) = @_;
26              
27             # Validate
28 735         1522 $self->validate;
29 735         1318 for my $name (@$self) {
30 744         1367 my $lc_name = lc $name;
31 744 100       3984 next if $lc_name =~ /^$identifier_re$/;
32 1         14 die "name '$lc_name' does not match identifier syntax: $identifier_re";
33             }
34              
35 734         1450 return join '.', map { lc } @$self;
  741         3323  
36             }
37              
38             sub to_SQL
39             {
40 92     92 0 164 my( $self, $options ) = @_;
41              
42 92 100       180 $options = {} unless $options;
43             my( $delim, $placeholder ) = (
44             $options->{delim},
45             $options->{placeholder},
46 92         183 );
47 92 50       167 $delim = "'" unless $delim;
48              
49             # Validate
50 92         205 $self->validate;
51 92 50       156 if( @$self > 2 ) {
52 0         0 die 'no SQL representation for properties of more than two ' .
53             "identifiers\n";
54             }
55              
56             # Construct the SQL
57 92         162 my $sql = join '.', map { "${delim}$_${delim}" } @$self;
  94         329  
58              
59 92 100       216 if( wantarray ) {
60 91         285 return ( $sql, [] );
61             } else {
62 1         5 return $sql;
63             }
64             }
65              
66             sub modify
67             {
68 7     7 0 14 my $self = shift;
69 7         13 my $code = shift;
70              
71 7         19 return $code->( $self, @_ );
72             }
73              
74             sub validate
75             {
76 827     827 0 1230 my $self = shift;
77 827 50       1609 die 'name undefined for OPTIMADE::Filter::Property' if !@$self;
78             }
79              
80             sub _eq
81             {
82 1     1   4 my( $a, $b ) = @_;
83              
84 1 50       7 return '' if !blessed( $b );
85 1 50       10 return '' if !$b->isa( OPTIMADE::Filter::Property:: );
86              
87 1 50       4 return '' if @$a != @$b;
88 1         3 for my $i (0..$#$a) {
89 2 50       3 return '' if defined $a->[$i] ^ defined $b->[$i];
90 2 50       5 next if !defined $a->[$i];
91 2 50       3 return '' if $a->[$i] ne $b->[$i];
92             }
93 1         14 return 1;
94             }
95              
96             1;