Skip to content

Commit

Permalink
Merge 16e289e into 7027eab
Browse files Browse the repository at this point in the history
  • Loading branch information
gardleopard committed Sep 8, 2022
2 parents 7027eab + 16e289e commit aade176
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
@@ -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
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 aade176

Please sign in to comment.