Skip to content

Commit 7bd83fd

Browse files
Add modulo operator (#21)
* Add modulo operator * Remove unnecessary matching function * Uncomment test cases
1 parent e318471 commit 7bd83fd

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

lib/flagsmith/engine/segments/constants.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module Constants
2222
NOT_EQUAL = 'NOT_EQUAL'
2323
REGEX = 'REGEX'
2424
PERCENTAGE_SPLIT = 'PERCENTAGE_SPLIT'
25+
MODULO = 'MODULO'
2526

2627
CONDITION_OPERATORS = [
2728
EQUAL,
@@ -33,7 +34,8 @@ module Constants
3334
NOT_CONTAINS,
3435
NOT_EQUAL,
3536
REGEX,
36-
PERCENTAGE_SPLIT
37+
PERCENTAGE_SPLIT,
38+
MODULO
3739
].freeze
3840
end
3941
end

lib/flagsmith/engine/segments/models.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,13 @@ def initialize(operator:, value:, property: nil)
5555
end
5656

5757
def match_trait_value?(trait_value)
58+
# handle some exceptions
5859
if @value.is_a?(String) && @value.match?(/:semver$/)
5960
trait_value = Semantic::Version.new(trait_value.gsub(/:semver$/, ''))
6061
end
6162

63+
return match_modulo_value(trait_value) if @operator == MODULO
64+
6265
type_as_trait_value = format_to_type_of(trait_value)
6366
formatted_value = type_as_trait_value ? type_as_trait_value.call(@value) : @value
6467

@@ -78,6 +81,13 @@ def format_to_type_of(input)
7881
end
7982
# rubocop:enable Metrics/AbcSize
8083

84+
def match_modulo_value(trait_value)
85+
divisor, remainder = @value.split('|')
86+
trait_value.is_a?(Numeric) && trait_value % divisor.to_f == remainder.to_f
87+
rescue StandardError
88+
false
89+
end
90+
8191
class << self
8292
def build(json)
8393
new(**json.slice(:operator, :value).merge(property: json[:property_]))

spec/engine/unit/segments/models_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,12 @@
7373
['GREATER_THAN_INCLUSIVE', '1.0.1', '1.0.1:semver', true],
7474
['LESS_THAN_INCLUSIVE', '1.0.0', '1.0.1:semver', true],
7575
['LESS_THAN_INCLUSIVE', '1.0.0', '1.0.0:semver', true],
76-
['LESS_THAN_INCLUSIVE', '1.0.1', '1.0.0:semver', false]
76+
['LESS_THAN_INCLUSIVE', '1.0.1', '1.0.0:semver', false],
77+
['MODULO', 2, '2|0', true],
78+
['MODULO', 3, '2|0', false],
79+
['MODULO', 2.0, '2|0', true],
80+
['MODULO', 'foo', '2|0', false],
81+
['MODULO', 'foo', 'foo|bar', false],
7782
].freeze
7883

7984
RSpec.describe Flagsmith::Engine::Segments::Condition do

0 commit comments

Comments
 (0)