lib/ldclient-rb/evaluation.rb in ldclient-rb-3.0.3 vs lib/ldclient-rb/evaluation.rb in ldclient-rb-4.0.0
- old
+ new
@@ -112,11 +112,11 @@
# Evaluates a feature flag, returning a hash containing the evaluation result and any events
# generated during prerequisite evaluation. Raises EvaluationError if the flag is not well-formed
# Will return nil, but not raise an exception, indicating that the rules (including fallthrough) did not match
# In that case, the caller should return the default value.
- def evaluate(flag, user, store)
+ def evaluate(flag, user, store, logger)
if flag.nil?
raise EvaluationError, "Flag does not exist"
end
if user.nil? || user[:key].nil?
@@ -124,41 +124,53 @@
end
events = []
if flag[:on]
- res = eval_internal(flag, user, store, events)
-
- return { value: res, events: events } if !res.nil?
+ res = eval_internal(flag, user, store, events, logger)
+ if !res.nil?
+ res[:events] = events
+ return res
+ end
end
- if !flag[:offVariation].nil? && flag[:offVariation] < flag[:variations].length
- value = flag[:variations][flag[:offVariation]]
- return { value: value, events: events }
+ offVariation = flag[:offVariation]
+ if !offVariation.nil? && offVariation < flag[:variations].length
+ value = flag[:variations][offVariation]
+ return { variation: offVariation, value: value, events: events }
end
- { value: nil, events: events }
+ { variation: nil, value: nil, events: events }
end
- def eval_internal(flag, user, store, events)
+ def eval_internal(flag, user, store, events, logger)
failed_prereq = false
# Evaluate prerequisites, if any
(flag[:prerequisites] || []).each do |prerequisite|
prereq_flag = store.get(FEATURES, prerequisite[:key])
if prereq_flag.nil? || !prereq_flag[:on]
failed_prereq = true
else
begin
- prereq_res = eval_internal(prereq_flag, user, store, events)
- variation = get_variation(prereq_flag, prerequisite[:variation])
- events.push(kind: "feature", key: prereq_flag[:key], value: prereq_res, version: prereq_flag[:version], prereqOf: flag[:key])
- if prereq_res.nil? || prereq_res != variation
+ prereq_res = eval_internal(prereq_flag, user, store, events, logger)
+ event = {
+ kind: "feature",
+ key: prereq_flag[:key],
+ variation: prereq_res.nil? ? nil : prereq_res[:variation],
+ value: prereq_res.nil? ? nil : prereq_res[:value],
+ version: prereq_flag[:version],
+ prereqOf: flag[:key],
+ trackEvents: prereq_flag[:trackEvents],
+ debugEventsUntilDate: prereq_flag[:debugEventsUntilDate]
+ }
+ events.push(event)
+ if prereq_res.nil? || prereq_res[:variation] != prerequisite[:variation]
failed_prereq = true
end
rescue => exn
- @config.logger.error { "[LDClient] Error evaluating prerequisite: #{exn.inspect}" }
+ logger.error { "[LDClient] Error evaluating prerequisite: #{exn.inspect}" }
failed_prereq = true
end
end
end
@@ -173,11 +185,13 @@
def eval_rules(flag, user, store)
# Check user target matches
(flag[:targets] || []).each do |target|
(target[:values] || []).each do |value|
- return get_variation(flag, target[:variation]) if value == user[:key]
+ if value == user[:key]
+ return { variation: target[:variation], value: get_variation(flag, target[:variation]) }
+ end
end
end
# Check custom rules
(flag[:rules] || []).each do |rule|
@@ -243,18 +257,20 @@
maybe_negate(clause, match_any(op, val, clause[:values]))
end
def variation_for_user(rule, user, flag)
if !rule[:variation].nil? # fixed variation
- return get_variation(flag, rule[:variation])
+ return { variation: rule[:variation], value: get_variation(flag, rule[:variation]) }
elsif !rule[:rollout].nil? # percentage rollout
rollout = rule[:rollout]
bucket_by = rollout[:bucketBy].nil? ? "key" : rollout[:bucketBy]
bucket = bucket_user(user, flag[:key], bucket_by, flag[:salt])
sum = 0;
rollout[:variations].each do |variate|
sum += variate[:weight].to_f / 100000.0
- return get_variation(flag, variate[:variation]) if bucket < sum
+ if bucket < sum
+ return { variation: variate[:variation], value: get_variation(flag, variate[:variation]) }
+ end
end
nil
else # the rule isn't well-formed
raise EvaluationError, "Rule does not define a variation or rollout"
end