Skip to content

Commit

Permalink
fix: support int context in set comparison (#115)
Browse files Browse the repository at this point in the history
This makes it possible to have int values in context and compare it to
sets from unleash which are strings.
  • Loading branch information
gardleopard committed Sep 8, 2022
1 parent 7027eab commit 5bb3aa8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
5 changes: 2 additions & 3 deletions lib/unleash/constraint.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
require 'date'

module Unleash
class Constraint
attr_accessor :context_name, :operator, :value, :inverted, :case_insensitive

OPERATORS = {
IN: ->(context_v, constraint_v){ constraint_v.include? context_v },
NOT_IN: ->(context_v, constraint_v){ !constraint_v.include? context_v },
IN: ->(context_v, constraint_v){ constraint_v.include? context_v.to_s },
NOT_IN: ->(context_v, constraint_v){ !constraint_v.include? context_v.to_s },
STR_STARTS_WITH: ->(context_v, constraint_v){ constraint_v.any?{ |v| context_v.start_with? v } },
STR_ENDS_WITH: ->(context_v, constraint_v){ constraint_v.any?{ |v| context_v.end_with? v } },
STR_CONTAINS: ->(context_v, constraint_v){ constraint_v.any?{ |v| context_v.include? v } },
Expand Down
18 changes: 18 additions & 0 deletions spec/unleash/constraint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@
expect(constraint.matches_context?(context)).to be true
end

it 'matches based on user_id IN/NOT_IN user_id with user_id as int' do
context_params = {
user_id: 123
}
context = Unleash::Context.new(context_params)
constraint = Unleash::Constraint.new('user_id', 'IN', ['123', '456'])
expect(constraint.matches_context?(context)).to be true

constraint = Unleash::Constraint.new('user_id', 'IN', ['456', '789'])
expect(constraint.matches_context?(context)).to be false

constraint = Unleash::Constraint.new('user_id', 'NOT_IN', ['123', '456'])
expect(constraint.matches_context?(context)).to be false

constraint = Unleash::Constraint.new('user_id', 'NOT_IN', ['456', '789'])
expect(constraint.matches_context?(context)).to be true
end

it 'matches based on property STR_STARTS_WITH value' do
context_params = {
properties: {
Expand Down

0 comments on commit 5bb3aa8

Please sign in to comment.