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   7334 use strict;
  7         14  
  7         261  
4 7     7   35 use warnings;
  7         13  
  7         555  
5              
6 7     7   475 use parent 'OPTIMADE::Filter::Modifiable';
  7         292  
  7         37  
7 7     7   473 use Scalar::Util qw(blessed);
  7         26  
  7         1552  
8              
9             our $VERSION = '0.11.0'; # VERSION
10              
11 2846     2846   9388 use overload '@{}' => sub { return $_[0]->{name} },
12 581     581   101479 '""' => sub { return $_[0]->to_filter },
13 0     0   0 '==' => sub { return $_[0]->_eq( $_[1] ) },
14 7     7   3752 'eq' => sub { return $_[0]->_eq( $_[1] ) };
  7     1   10493  
  7         78  
  1         290  
15              
16             our $identifier_re = q/([a-z_][a-z0-9_]*)/;
17              
18             sub new {
19 272     272 0 255240 my $class = shift;
20 272         4626 return bless { name => \@_ }, $class;
21             }
22              
23             sub to_filter
24             {
25 843     843 0 1624 my( $self ) = @_;
26              
27             # Validate
28 843         2171 $self->validate;
29 843         1637 for my $name (@$self) {
30 852         1680 my $lc_name = lc $name;
31 852 100       6403 next if $lc_name =~ /^$identifier_re$/;
32 1         9 die "name '$lc_name' does not match identifier syntax: $identifier_re";
33             }
34              
35 842         3740 return join '.', map { lc } @$self;
  849         4615  
36             }
37              
38             sub to_SQL
39             {
40 100     100 0 210 my( $self, $options ) = @_;
41              
42 100 100       217 $options = {} unless $options;
43             my( $delim, $placeholder ) = (
44             $options->{delim},
45             $options->{placeholder},
46 100         312 );
47 100 50       254 $delim = "'" unless $delim;
48              
49             # Validate
50 100         322 $self->validate;
51 100 50       198 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 100         195 my $sql = join '.', map { "${delim}$_${delim}" } @$self;
  102         389  
58              
59 100 100       235 if( wantarray ) {
60 99         355 return ( $sql, [] );
61             } else {
62 1         4 return $sql;
63             }
64             }
65              
66             sub modify
67             {
68 7     7 0 13 my $self = shift;
69 7         11 my $code = shift;
70              
71 7         23 return $code->( $self, @_ );
72             }
73              
74             sub validate
75             {
76 943     943 0 1551 my $self = shift;
77 943 50       2083 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       4 return '' if !blessed( $b );
85 1 50       13 return '' if !$b->isa( OPTIMADE::Filter::Property:: );
86              
87 1 50       9 return '' if @$a != @$b;
88 1         4 for my $i (0..$#$a) {
89 2 50       4 return '' if defined $a->[$i] ^ defined $b->[$i];
90 2 50       4 next if !defined $a->[$i];
91 2 50       3 return '' if $a->[$i] ne $b->[$i];
92             }
93 1         5 return 1;
94             }
95              
96             1;