Class: Prefab::CriteriaEvaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/prefab/criteria_evaluator.rb

Constant Summary collapse

LOOKUP_KEY =
'LOOKUP'
NAMESPACE_KEY =
'NAMESPACE'
NO_MATCHING_ROWS =
[].freeze

Instance Method Summary collapse

Constructor Details

#initialize(config, project_env_id:, resolver:, base_client:) ⇒ CriteriaEvaluator

Returns a new instance of CriteriaEvaluator.



9
10
11
12
13
14
# File 'lib/prefab/criteria_evaluator.rb', line 9

def initialize(config, project_env_id:, resolver:, base_client:)
  @config = config
  @project_env_id = project_env_id
  @resolver = resolver
  @base_client = base_client
end

Instance Method Details

#all_criteria_match?(conditional_value, props) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
# File 'lib/prefab/criteria_evaluator.rb', line 31

def all_criteria_match?(conditional_value, props)
  conditional_value.criteria.all? do |criterion|
    evaluate_criteron(criterion, props)
  end
end

#evaluate(properties) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/prefab/criteria_evaluator.rb', line 16

def evaluate(properties)
  # TODO: optimize this and perhaps do it elsewhere
  props = properties.transform_keys(&:to_s)

  matching_environment_row_values.each do |conditional_value|
    return conditional_value.value if all_criteria_match?(conditional_value, props)
  end

  default_row_values.each do |conditional_value|
    return conditional_value.value if all_criteria_match?(conditional_value, props)
  end

  nil
end

#evaluate_criteron(criterion, properties) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/prefab/criteria_evaluator.rb', line 37

def evaluate_criteron(criterion, properties)
  value_from_properties = properties[criterion.property_name]

  case criterion.operator
  when :LOOKUP_KEY_IN, :PROP_IS_ONE_OF
    matches?(criterion, value_from_properties, properties)
  when :LOOKUP_KEY_NOT_IN, :PROP_IS_NOT_ONE_OF
    !matches?(criterion, value_from_properties, properties)
  when :IN_SEG
    in_segment?(criterion, properties)
  when :NOT_IN_SEG
    !in_segment?(criterion, properties)
  when :PROP_ENDS_WITH_ONE_OF
    return false unless value_from_properties

    criterion.value_to_match.string_list.values.any? do |ending|
      value_from_properties.end_with?(ending)
    end
  when :PROP_DOES_NOT_END_WITH_ONE_OF
    return true unless value_from_properties

    criterion.value_to_match.string_list.values.none? do |ending|
      value_from_properties.end_with?(ending)
    end
  when :HIERARCHICAL_MATCH
    value_from_properties.start_with?(criterion.value_to_match.string)
  when :ALWAYS_TRUE
    true
  else
    @base_client.log.info("Unknown Operator: #{criterion.operator}")
    false
  end
end