Skip to content

Commit db51caa

Browse files
authored
Merge pull request #69 from Flagsmith/fix/nil-safe-operators
Make segment operators nil-safe
2 parents 536ad9f + e0918cd commit db51caa

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

lib/flagsmith/engine/segments/models.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ class Condition
3737

3838
MATCHING_FUNCTIONS = {
3939
EQUAL => ->(other_value, self_value) { other_value == self_value },
40-
GREATER_THAN => ->(other_value, self_value) { other_value > self_value },
41-
GREATER_THAN_INCLUSIVE => ->(other_value, self_value) { other_value >= self_value },
42-
LESS_THAN => ->(other_value, self_value) { other_value < self_value },
43-
LESS_THAN_INCLUSIVE => ->(other_value, self_value) { other_value <= self_value },
40+
GREATER_THAN => ->(other_value, self_value) { (other_value || false) && other_value > self_value },
41+
GREATER_THAN_INCLUSIVE => ->(other_value, self_value) { (other_value || false) && other_value >= self_value },
42+
LESS_THAN => ->(other_value, self_value) { (other_value || false) && other_value < self_value },
43+
LESS_THAN_INCLUSIVE => ->(other_value, self_value) { (other_value || false) && other_value <= self_value },
4444
NOT_EQUAL => ->(other_value, self_value) { other_value != self_value },
45-
CONTAINS => ->(other_value, self_value) { other_value.include? self_value },
45+
CONTAINS => ->(other_value, self_value) { (other_value || false) && other_value.include?(self_value) },
4646

47-
NOT_CONTAINS => ->(other_value, self_value) { !other_value.include? self_value },
48-
REGEX => ->(other_value, self_value) { other_value.match? self_value }
47+
NOT_CONTAINS => ->(other_value, self_value) { (other_value || false) && !other_value.include?(self_value) },
48+
REGEX => ->(other_value, self_value) { (other_value || false) && other_value.match?(self_value) }
4949
}.freeze
5050

5151
def initialize(operator:, value:, property: nil)

spec/engine/unit/segments/models_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,28 @@
1313
['EQUAL', true, 'false', false],
1414
['EQUAL', 1.23, '1.23', true],
1515
['EQUAL', 1.23, '4.56', false],
16+
['GREATER_THAN', nil, '1', false],
1617
['GREATER_THAN', 2, '1', true],
1718
['GREATER_THAN', 1, '1', false],
1819
['GREATER_THAN', 0, '1', false],
1920
['GREATER_THAN', 2.1, '2.0', true],
2021
['GREATER_THAN', 2.1, '2.1', false],
2122
['GREATER_THAN', 2.0, '2.1', false],
23+
['GREATER_THAN_INCLUSIVE', nil, '1', false],
2224
['GREATER_THAN_INCLUSIVE', 2, '1', true],
2325
['GREATER_THAN_INCLUSIVE', 1, '1', true],
2426
['GREATER_THAN_INCLUSIVE', 0, '1', false],
2527
['GREATER_THAN_INCLUSIVE', 2.1, '2.0', true],
2628
['GREATER_THAN_INCLUSIVE', 2.1, '2.1', true],
2729
['GREATER_THAN_INCLUSIVE', 2.0, '2.1', false],
30+
['LESS_THAN', nil, '2', false],
2831
['LESS_THAN', 1, '2', true],
2932
['LESS_THAN', 1, '1', false],
3033
['LESS_THAN', 1, '0', false],
3134
['LESS_THAN', 2.0, '2.1', true],
3235
['LESS_THAN', 2.1, '2.1', false],
3336
['LESS_THAN', 2.1, '2.0', false],
37+
['LESS_THAN_INCLUSIVE', nil, '2', false],
3438
['LESS_THAN_INCLUSIVE', 1, '2', true],
3539
['LESS_THAN_INCLUSIVE', 1, '1', true],
3640
['LESS_THAN_INCLUSIVE', 1, '0', false],
@@ -45,12 +49,15 @@
4549
['NOT_EQUAL', false, 'true', true],
4650
['NOT_EQUAL', false, 'false', false],
4751
['NOT_EQUAL', true, 'true', false],
52+
['CONTAINS', nil, 'b', false],
4853
['CONTAINS', 'bar', 'b', true],
4954
['CONTAINS', 'bar', 'bar', true],
5055
['CONTAINS', 'bar', 'baz', false],
56+
['NOT_CONTAINS', nil, 'b', false],
5157
['NOT_CONTAINS', 'bar', 'b', false],
5258
['NOT_CONTAINS', 'bar', 'bar', false],
5359
['NOT_CONTAINS', 'bar', 'baz', true],
60+
['REGEX', nil, /[a-z]+/, false],
5461
['REGEX', 'foo', /[a-z]+/, true],
5562
['REGEX', 'FOO', /[a-z]+/, false],
5663
['REGEX', '1.2.3', /\d/, true],

0 commit comments

Comments
 (0)