Class: Prefab::CriteriaEvaluator
- Inherits:
-
Object
- Object
- Prefab::CriteriaEvaluator
- Defined in:
- lib/prefab/criteria_evaluator.rb
Overview
This class evaluates a config’s criteria. ‘evaluate` returns the value of the first match based on the provided properties.
Defined Under Namespace
Classes: MatchResult
Constant Summary collapse
- LOG =
Prefab::InternalLogger.new(self)
- NAMESPACE_KEY =
'NAMESPACE'
- NO_MATCHING_ROWS =
[].freeze
- COMPARE_TO_OPERATORS =
{ less_than_or_equal: -> cmp { cmp <= 0 }, less_than: -> cmp { cmp < 0 }, equal_to: -> cmp { cmp == 0 }, greater_than: -> cmp { cmp > 0 }, greater_than_or_equal: -> cmp { cmp >= 0 }, }
Instance Method Summary collapse
- #all_criteria_match?(conditional_value, props) ⇒ Boolean
- #ALWAYS_TRUE(_criterion, _properties) ⇒ Object
- #evaluate(properties) ⇒ Object
- #HIERARCHICAL_MATCH(criterion, properties) ⇒ Object
- #IN_INT_RANGE(criterion, properties) ⇒ Object
- #IN_SEG(criterion, properties) ⇒ Object
-
#initialize(config, project_env_id:, resolver:, namespace:, base_client:) ⇒ CriteriaEvaluator
constructor
A new instance of CriteriaEvaluator.
- #NOT_IN_SEG(criterion, properties) ⇒ Object
- #PROP_AFTER(criterion, properties) ⇒ Object
- #PROP_BEFORE(criterion, properties) ⇒ Object
- #PROP_CONTAINS_ONE_OF(criterion, properties) ⇒ Object
- #PROP_DOES_NOT_CONTAIN_ONE_OF(criterion, properties) ⇒ Object
- #PROP_DOES_NOT_END_WITH_ONE_OF(criterion, properties) ⇒ Object
- #PROP_DOES_NOT_MATCH(criterion, properties) ⇒ Object
- #PROP_DOES_NOT_START_WITH_ONE_OF(criterion, properties) ⇒ Object
- #PROP_ENDS_WITH_ONE_OF(criterion, properties) ⇒ Object
- #PROP_GREATER_THAN(criterion, properties) ⇒ Object
- #PROP_GREATER_THAN_OR_EQUAL(criterion, properties) ⇒ Object
- #PROP_IS_NOT_ONE_OF(criterion, properties) ⇒ Object
- #PROP_IS_ONE_OF(criterion, properties) ⇒ Object
- #PROP_LESS_THAN(criterion, properties) ⇒ Object
- #PROP_LESS_THAN_OR_EQUAL(criterion, properties) ⇒ Object
- #PROP_MATCHES(criterion, properties) ⇒ Object
- #PROP_SEMVER_EQUAL(criterion, properties) ⇒ Object
- #PROP_SEMVER_GREATER_THAN(criterion, properties) ⇒ Object
- #PROP_SEMVER_LESS_THAN(criterion, properties) ⇒ Object
- #PROP_STARTS_WITH_ONE_OF(criterion, properties) ⇒ Object
- #value_from_properties(criterion, properties) ⇒ Object
Constructor Details
#initialize(config, project_env_id:, resolver:, namespace:, base_client:) ⇒ CriteriaEvaluator
Returns a new instance of CriteriaEvaluator.
14 15 16 17 18 19 20 |
# File 'lib/prefab/criteria_evaluator.rb', line 14 def initialize(config, project_env_id:, resolver:, namespace:, base_client:) @config = config @project_env_id = project_env_id @resolver = resolver @namespace = namespace @base_client = base_client end |
Instance Method Details
#all_criteria_match?(conditional_value, props) ⇒ 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| public_send(criterion.operator, criterion, props) end end |
#ALWAYS_TRUE(_criterion, _properties) ⇒ Object
45 46 47 |
# File 'lib/prefab/criteria_evaluator.rb', line 45 def ALWAYS_TRUE(_criterion, _properties) true end |
#evaluate(properties) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/prefab/criteria_evaluator.rb', line 22 def evaluate(properties) rtn = evaluate_for_env(@project_env_id, properties) || evaluate_for_env(0, properties) LOG.trace { "Eval Key #{@config.key} Result #{rtn&.reportable_value} with #{properties.to_h}" } unless @config.config_type == :LOG_LEVEL rtn end |
#HIERARCHICAL_MATCH(criterion, properties) ⇒ Object
83 84 85 86 |
# File 'lib/prefab/criteria_evaluator.rb', line 83 def HIERARCHICAL_MATCH(criterion, properties) value = value_from_properties(criterion, properties) value&.start_with?(criterion.value_to_match.string) end |
#IN_INT_RANGE(criterion, properties) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/prefab/criteria_evaluator.rb', line 88 def IN_INT_RANGE(criterion, properties) value = if criterion.property_name == 'prefab.current-time' Time.now.utc.to_i * 1000 else value_from_properties(criterion, properties) end value && value >= criterion.value_to_match.int_range.start && value < criterion.value_to_match.int_range.end end |
#IN_SEG(criterion, properties) ⇒ Object
37 38 39 |
# File 'lib/prefab/criteria_evaluator.rb', line 37 def IN_SEG(criterion, properties) in_segment?(criterion, properties) end |
#NOT_IN_SEG(criterion, properties) ⇒ Object
41 42 43 |
# File 'lib/prefab/criteria_evaluator.rb', line 41 def NOT_IN_SEG(criterion, properties) !in_segment?(criterion, properties) end |
#PROP_AFTER(criterion, properties) ⇒ Object
136 137 138 |
# File 'lib/prefab/criteria_evaluator.rb', line 136 def PROP_AFTER(criterion, properties) evaluate_date_comparison(criterion, properties, COMPARE_TO_OPERATORS[:greater_than]).matched end |
#PROP_BEFORE(criterion, properties) ⇒ Object
132 133 134 |
# File 'lib/prefab/criteria_evaluator.rb', line 132 def PROP_BEFORE(criterion, properties) evaluate_date_comparison(criterion, properties, COMPARE_TO_OPERATORS[:less_than]).matched end |
#PROP_CONTAINS_ONE_OF(criterion, properties) ⇒ Object
75 76 77 |
# File 'lib/prefab/criteria_evaluator.rb', line 75 def PROP_CONTAINS_ONE_OF(criterion, properties) prop_contains_one_of?(criterion, value_from_properties(criterion, properties)) end |
#PROP_DOES_NOT_CONTAIN_ONE_OF(criterion, properties) ⇒ Object
79 80 81 |
# File 'lib/prefab/criteria_evaluator.rb', line 79 def PROP_DOES_NOT_CONTAIN_ONE_OF(criterion, properties) !PROP_CONTAINS_ONE_OF(criterion, properties) end |
#PROP_DOES_NOT_END_WITH_ONE_OF(criterion, properties) ⇒ Object
63 64 65 |
# File 'lib/prefab/criteria_evaluator.rb', line 63 def PROP_DOES_NOT_END_WITH_ONE_OF(criterion, properties) !PROP_ENDS_WITH_ONE_OF(criterion, properties) end |
#PROP_DOES_NOT_MATCH(criterion, properties) ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/prefab/criteria_evaluator.rb', line 107 def PROP_DOES_NOT_MATCH(criterion, properties) result = check_regex_match(criterion, properties) if result.error false else !result.matched end end |
#PROP_DOES_NOT_START_WITH_ONE_OF(criterion, properties) ⇒ Object
71 72 73 |
# File 'lib/prefab/criteria_evaluator.rb', line 71 def PROP_DOES_NOT_START_WITH_ONE_OF(criterion, properties) !PROP_STARTS_WITH_ONE_OF(criterion, properties) end |
#PROP_ENDS_WITH_ONE_OF(criterion, properties) ⇒ Object
59 60 61 |
# File 'lib/prefab/criteria_evaluator.rb', line 59 def PROP_ENDS_WITH_ONE_OF(criterion, properties) prop_ends_with_one_of?(criterion, value_from_properties(criterion, properties)) end |
#PROP_GREATER_THAN(criterion, properties) ⇒ Object
124 125 126 |
# File 'lib/prefab/criteria_evaluator.rb', line 124 def PROP_GREATER_THAN(criterion, properties) evaluate_number_comparison(criterion, properties, COMPARE_TO_OPERATORS[:greater_than]).matched end |
#PROP_GREATER_THAN_OR_EQUAL(criterion, properties) ⇒ Object
128 129 130 |
# File 'lib/prefab/criteria_evaluator.rb', line 128 def PROP_GREATER_THAN_OR_EQUAL(criterion, properties) evaluate_number_comparison(criterion, properties,COMPARE_TO_OPERATORS[:greater_than_or_equal]) .matched end |
#PROP_IS_NOT_ONE_OF(criterion, properties) ⇒ Object
55 56 57 |
# File 'lib/prefab/criteria_evaluator.rb', line 55 def PROP_IS_NOT_ONE_OF(criterion, properties) !PROP_IS_ONE_OF(criterion, properties) end |
#PROP_IS_ONE_OF(criterion, properties) ⇒ Object
49 50 51 52 53 |
# File 'lib/prefab/criteria_evaluator.rb', line 49 def PROP_IS_ONE_OF(criterion, properties) Array(value_from_properties(criterion, properties)).any? do |prop| matches?(criterion, prop, properties) end end |
#PROP_LESS_THAN(criterion, properties) ⇒ Object
116 117 118 |
# File 'lib/prefab/criteria_evaluator.rb', line 116 def PROP_LESS_THAN(criterion, properties) evaluate_number_comparison(criterion, properties, COMPARE_TO_OPERATORS[:less_than]).matched end |
#PROP_LESS_THAN_OR_EQUAL(criterion, properties) ⇒ Object
120 121 122 |
# File 'lib/prefab/criteria_evaluator.rb', line 120 def PROP_LESS_THAN_OR_EQUAL(criterion, properties) evaluate_number_comparison(criterion, properties, COMPARE_TO_OPERATORS[:less_than_or_equal]).matched end |
#PROP_MATCHES(criterion, properties) ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/prefab/criteria_evaluator.rb', line 98 def PROP_MATCHES(criterion, properties) result = check_regex_match(criterion, properties) if result.error false else result.matched end end |
#PROP_SEMVER_EQUAL(criterion, properties) ⇒ Object
144 145 146 |
# File 'lib/prefab/criteria_evaluator.rb', line 144 def PROP_SEMVER_EQUAL(criterion, properties) evaluate_semver_comparison(criterion, properties, COMPARE_TO_OPERATORS[:equal_to]).matched end |
#PROP_SEMVER_GREATER_THAN(criterion, properties) ⇒ Object
148 149 150 |
# File 'lib/prefab/criteria_evaluator.rb', line 148 def PROP_SEMVER_GREATER_THAN(criterion, properties) evaluate_semver_comparison(criterion, properties, COMPARE_TO_OPERATORS[:greater_than]).matched end |
#PROP_SEMVER_LESS_THAN(criterion, properties) ⇒ Object
140 141 142 |
# File 'lib/prefab/criteria_evaluator.rb', line 140 def PROP_SEMVER_LESS_THAN(criterion, properties) evaluate_semver_comparison(criterion, properties, COMPARE_TO_OPERATORS[:less_than]).matched end |
#PROP_STARTS_WITH_ONE_OF(criterion, properties) ⇒ Object
67 68 69 |
# File 'lib/prefab/criteria_evaluator.rb', line 67 def PROP_STARTS_WITH_ONE_OF(criterion, properties) prop_starts_with_one_of?(criterion, value_from_properties(criterion, properties)) end |
#value_from_properties(criterion, properties) ⇒ Object
152 153 154 |
# File 'lib/prefab/criteria_evaluator.rb', line 152 def value_from_properties(criterion, properties) criterion.property_name == NAMESPACE_KEY ? @namespace : properties.get(criterion.property_name) end |