Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions lib/flagsmith/engine/segments/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class Condition
LESS_THAN => ->(other_value, self_value) { (other_value || false) && other_value < self_value },
LESS_THAN_INCLUSIVE => ->(other_value, self_value) { (other_value || false) && other_value <= self_value },
NOT_EQUAL => ->(other_value, self_value) { other_value != self_value },
CONTAINS => ->(other_value, self_value) { (other_value || false) && other_value.include?(self_value) },
CONTAINS => ->(other_value, self_value) { other_value.is_a?(String) && other_value.include?(self_value) },

NOT_CONTAINS => ->(other_value, self_value) { (other_value || false) && !other_value.include?(self_value) },
NOT_CONTAINS => ->(other_value, self_value) { other_value.is_a?(String) && !other_value.include?(self_value) },
REGEX => ->(other_value, self_value) { (other_value || false) && other_value.to_s.match?(self_value) }
}.freeze

Expand Down Expand Up @@ -77,21 +77,17 @@ def match_with_type_conversion(trait_value)
type_as_trait_value = format_to_type_of(trait_value)
formatted_value = type_as_trait_value ? type_as_trait_value.call(@value) : @value
MATCHING_FUNCTIONS[operator]&.call(trait_value, formatted_value)
rescue ArgumentError # Rule value not coercible to the trait value's type, e.g. 2 > '1.5'
false
end

TYPE_CONVERTERS = {
'String' => ->(v) { v.to_s },
'Semantic::Version' => ->(v) { Semantic::Version.new(v.to_s.gsub(/:semver$/, '')) },
'TrueClass' => ->(v) { ['True', 'true', 'TRUE', true, 1, '1'].include?(v) },
'FalseClass' => ->(v) { !['False', 'false', 'FALSE', false].include?(v) },
'Integer' => lambda { |v|
i = v.to_i
i.to_s == v.to_s ? i : v
},
'Float' => lambda { |v|
f = v.to_f
f.to_s == v.to_s ? f : v
}
'Integer' => ->(v) { Integer(v.to_s, 10) },
'Float' => ->(v) { Float(v.to_s) }
}.freeze

def format_to_type_of(input)
Expand Down
14 changes: 14 additions & 0 deletions spec/engine/unit/segments/models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,43 @@
['EQUAL', true, 'false', false],
['EQUAL', 1.23, '1.23', true],
['EQUAL', 1.23, '4.56', false],
['EQUAL', 1.0, '1', true],
['GREATER_THAN', nil, '1', false],
['GREATER_THAN', 2, '1', true],
['GREATER_THAN', 1, '1', false],
['GREATER_THAN', 0, '1', false],
['GREATER_THAN', 2.1, '2.0', true],
['GREATER_THAN', 2.1, '2.1', false],
['GREATER_THAN', 2.0, '2.1', false],
['GREATER_THAN', 2.1, '2', true],
['GREATER_THAN', 2, '1.5', false],
['GREATER_THAN_INCLUSIVE', nil, '1', false],
['GREATER_THAN_INCLUSIVE', 2, '1', true],
['GREATER_THAN_INCLUSIVE', 1, '1', true],
['GREATER_THAN_INCLUSIVE', 0, '1', false],
['GREATER_THAN_INCLUSIVE', 2.1, '2.0', true],
['GREATER_THAN_INCLUSIVE', 2.1, '2.1', true],
['GREATER_THAN_INCLUSIVE', 2.0, '2.1', false],
['GREATER_THAN_INCLUSIVE', 2.0, '2', true],
['GREATER_THAN_INCLUSIVE', 2, '1.5', false],
['LESS_THAN', nil, '2', false],
['LESS_THAN', 1, '2', true],
['LESS_THAN', 1, '1', false],
['LESS_THAN', 1, '0', false],
['LESS_THAN', 2.0, '2.1', true],
['LESS_THAN', 2.1, '2.1', false],
['LESS_THAN', 2.1, '2.0', false],
['LESS_THAN', 1.9, '2', true],
['LESS_THAN', 1, '1.5', false],
['LESS_THAN_INCLUSIVE', nil, '2', false],
['LESS_THAN_INCLUSIVE', 1, '2', true],
['LESS_THAN_INCLUSIVE', 1, '1', true],
['LESS_THAN_INCLUSIVE', 1, '0', false],
['LESS_THAN_INCLUSIVE', 2.0, '2.1', true],
['LESS_THAN_INCLUSIVE', 2.1, '2.1', true],
['LESS_THAN_INCLUSIVE', 2.1, '2.0', false],
['LESS_THAN_INCLUSIVE', 2.0, '2', true],
['LESS_THAN_INCLUSIVE', 1, '1.5', false],
['NOT_EQUAL', 'bar', 'baz', true],
['NOT_EQUAL', 'bar', 'bar', false],
['NOT_EQUAL', 1, '2', true],
Expand All @@ -49,14 +58,19 @@
['NOT_EQUAL', false, 'true', true],
['NOT_EQUAL', false, 'false', false],
['NOT_EQUAL', true, 'true', false],
['NOT_EQUAL', 95, '90.5', false],
['NOT_EQUAL', 1, 'foo', false],
['NOT_EQUAL', 1.5, 'foo', false],
['CONTAINS', nil, 'b', false],
['CONTAINS', 'bar', 'b', true],
['CONTAINS', 'bar', 'bar', true],
['CONTAINS', 'bar', 'baz', false],
['CONTAINS', 95, '9', false],
['NOT_CONTAINS', nil, 'b', false],
['NOT_CONTAINS', 'bar', 'b', false],
['NOT_CONTAINS', 'bar', 'bar', false],
['NOT_CONTAINS', 'bar', 'baz', true],
['NOT_CONTAINS', 95, 'abc', false],
['REGEX', nil, /[a-z]+/, false],
['REGEX', 'foo', /[a-z]+/, true],
['REGEX', 'FOO', /[a-z]+/, false],
Expand Down
Loading