| 1 | |||||||||||
| 2 | package UR::BoolExpr::Template::PropertyComparison; | ||||||||||
| 3 | |||||||||||
| 4 | 266 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 266 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 266 | our $VERSION = "0.47"; # UR $VERSION; | |||||||||
| 7 | |||||||||||
| 8 | # Define the class metadata. | ||||||||||
| 9 | |||||||||||
| 10 | 266 | require UR; | |||||||||
| 11 | |||||||||||
| 12 | 266 | UR::Object::Type->define( | |||||||||
| 13 | class_name => __PACKAGE__, | ||||||||||
| 14 | is => ['UR::BoolExpr::Template'], | ||||||||||
| 15 | #has => [qw/ | ||||||||||
| 16 | # rule_type | ||||||||||
| 17 | # subject_class_name | ||||||||||
| 18 | # property_name | ||||||||||
| 19 | # comparison_operator | ||||||||||
| 20 | # value | ||||||||||
| 21 | # resolution_code_perl | ||||||||||
| 22 | # resolution_code_sql | ||||||||||
| 23 | #/], | ||||||||||
| 24 | #id_by => ['subject_class_name','logic_string'] | ||||||||||
| 25 | ); | ||||||||||
| 26 | |||||||||||
| 27 | 266 | ▶ | use UR::BoolExpr::Template::PropertyComparison::Equals; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 266 | ▶ | use UR::BoolExpr::Template::PropertyComparison::LessThan; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 266 | ▶ | use UR::BoolExpr::Template::PropertyComparison::In; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 266 | ▶ | use UR::BoolExpr::Template::PropertyComparison::Like; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | |||||||||||
| 32 | 0 | ▶ | sub property_name { | ||||||||
|
Subroutine
property_name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 33 | 0 | (split(' ',$_[0]->logic_detail))[0] | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | |||||||||||
| 37 | 0 | ▶ | sub comparison_operator { | ||||||||
|
Subroutine
comparison_operator: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 38 | 0 | (split(' ',$_[0]->logic_detail))[1] | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 0 | ▶ | sub sub_group { | ||||||||
|
Subroutine
sub_group: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 42 | 0 | my $self = shift; | |||||||||
| 43 | 0 | my $spec = $self->property_name; | |||||||||
| 44 | 0 | ▶ | if ($spec =~ /-/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | #$DB::single = 1; | ||||||||||
| 46 | } | ||||||||||
| 47 | 0 | ▶ | if ($spec =~ /^(.*)+\-(\w+)(\?|)(\..+|)/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 0 | return $2 . $3; | |||||||||
| 49 | } | ||||||||||
| 50 | else { | ||||||||||
| 51 | 0 | return ''; | |||||||||
| 52 | } | ||||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 0 | ▶ | sub get_underlying_rules_for_values { | ||||||||
|
Subroutine
get_underlying_rules_for_values: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 56 | 0 | return; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 0 | ▶ | sub num_values { | ||||||||
|
Subroutine
num_values: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 60 | # Not strictly correct... | ||||||||||
| 61 | 0 | return 1; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 0 | ▶ | sub evaluate_subject_and_values { | ||||||||
|
Subroutine
evaluate_subject_and_values: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 65 | 0 | my ($self,$subject,$comparison_value) = @_; | |||||||||
| 66 | 0 | my @property_values = $subject->__get_attr__($self->property_name); | |||||||||
| 67 | 0 | return $self->_compare($comparison_value, @property_values); | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 0 | ▶ | sub resolve_subclass_for_comparison_operator { | ||||||||
|
Subroutine
resolve_subclass_for_comparison_operator: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 71 | 0 | my $class = shift; | |||||||||
| 72 | 0 | my $comparison_operator = shift; | |||||||||
| 73 | |||||||||||
| 74 | # Remove any escape sequence that may have been put in at UR::BoolExpr::resolve() | ||||||||||
| 75 | 0 | ▶ | $comparison_operator =~ s/-.+$// if $comparison_operator; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | |||||||||||
| 77 | 0 | my $suffix = UR::Util::class_suffix_for_operator($comparison_operator); | |||||||||
| 78 | |||||||||||
| 79 | 0 | my $subclass_name = join('::', $class, $suffix); | |||||||||
| 80 | |||||||||||
| 81 | 0 | my $subclass_meta = UR::Object::Type->get($subclass_name); | |||||||||
| 82 | 0 | ▶ | unless ($subclass_meta) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 0 | Carp::confess("Unknown operator '$comparison_operator'"); | |||||||||
| 84 | } | ||||||||||
| 85 | 0 | return $subclass_name; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 0 | ▶ | sub _get_for_subject_class_name_and_logic_detail { | ||||||||
|
Subroutine
_get_for_subject_class_name_and_logic_detail: not called CC=1 SCAR=6.9
| |||||||||||
| 89 | 0 | my $class = shift; | |||||||||
| 90 | 0 | my $subject_class_name = shift; | |||||||||
| 91 | 0 | my $logic_detail = shift; | |||||||||
| 92 | |||||||||||
| 93 | 0 | my ($property_name, $comparison_operator) = split(' ',$logic_detail, 2); | |||||||||
| 94 | 0 | my $subclass_name = $class->resolve_subclass_for_comparison_operator($comparison_operator); | |||||||||
| 95 | 0 | my $id = $subclass_name->__meta__->resolve_composite_id_from_ordered_values($subject_class_name, 'PropertyComparison', $logic_detail); | |||||||||
| 96 | |||||||||||
| 97 | 0 | return $subclass_name->get($id); | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 0 | ▶ | sub comparison_value_and_escape_character_to_regex { | ||||||||
|
Subroutine
comparison_value_and_escape_character_to_regex: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||
| 101 | 0 | my ($class, $value, $escape) = @_; | |||||||||
| 102 | |||||||||||
| 103 | 0 | ▶ | return '' unless defined($value); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | |||||||||||
| 105 | # anyone who uses the % as an escape character deserves to suffer | ||||||||||
| 106 | 0 | ▶ | if ($value eq '%') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | 0 | return '^.+$'; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 0 | my $regex = $value; | |||||||||
| 111 | |||||||||||
| 112 | # Escape all special characters in the regex. | ||||||||||
| 113 | 0 | $regex =~ s/([\(\)\[\]\{\}\+\*\.\?\|\^\$\-])/\\$1/g; | |||||||||
| 114 | |||||||||||
| 115 | # Handle the escape sequence | ||||||||||
| 116 | 0 | ▶ | if (defined $escape) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | { | ||||||||||
| 118 | 0 | $escape =~ s/\\/\\\\/g; # replace \ with \\ | |||||||||
| 119 | 0 | $regex =~ s/(?<!${escape})\%/\.\*/g; | |||||||||
| 120 | 0 | $regex =~ s/(?<!${escape})\_/./g; | |||||||||
| 121 | #LSF: Take away the escape characters. | ||||||||||
| 122 | 0 | $regex =~ s/$escape\%/\%/g; | |||||||||
| 123 | 0 | $regex =~ s/$escape\_/\_/g; | |||||||||
| 124 | } | ||||||||||
| 125 | else | ||||||||||
| 126 | { | ||||||||||
| 127 | 0 | $regex =~ s/\%/\.\*/g; | |||||||||
| 128 | 0 | $regex =~ s/\_/\./g; | |||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | # Wrap the regex in delimiters. | ||||||||||
| 132 | 0 | $regex = "^${regex}\$"; | |||||||||
| 133 | |||||||||||
| 134 | 0 | my $exception = do { | |||||||||
| 135 | 0 | local $@; | |||||||||
| 136 | 0 | $regex = eval { qr($regex) }; | |||||||||
| 137 | 0 | $@; | |||||||||
| 138 | }; | ||||||||||
| 139 | 0 | ▶ | if ($exception) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | 0 | Carp::confess($exception); | |||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | 0 | return $regex; | |||||||||
| 144 | } | ||||||||||
| 145 | |||||||||||
| 146 | 266 | 1; | |||||||||
| 147 | |||||||||||
| 148 | =head1 NAME | ||||||||||
| 149 | |||||||||||
| 150 | UR::BoolExpr::Template::PropertyComparison - implements logic for rules with a logic_type of "PropertyComparison" | ||||||||||
| 151 | |||||||||||
| 152 | =head1 SEE ALSO | ||||||||||
| 153 | |||||||||||
| 154 | UR::Object(3), UR::BoolExpr::Temmplate(3), UR::BoolExpr(3), UR::BoolExpr::Template::PropertyComparison::* | ||||||||||
| 155 | |||||||||||
| 156 | =cut | ||||||||||
| 157 | |||||||||||