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 |
# File 'lib/prefab/criteria_evaluator.rb', line 88 def IN_INT_RANGE(criterion, properties) value = value_from_properties(criterion, properties) 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
131 132 133 |
# File 'lib/prefab/criteria_evaluator.rb', line 131 def PROP_AFTER(criterion, properties) evaluate_date_comparison(criterion, properties, COMPARE_TO_OPERATORS[:greater_than]).matched end |
#PROP_BEFORE(criterion, properties) ⇒ Object
127 128 129 |
# File 'lib/prefab/criteria_evaluator.rb', line 127 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
102 103 104 105 106 107 108 109 |
# File 'lib/prefab/criteria_evaluator.rb', line 102 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
119 120 121 |
# File 'lib/prefab/criteria_evaluator.rb', line 119 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
123 124 125 |
# File 'lib/prefab/criteria_evaluator.rb', line 123 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
111 112 113 |
# File 'lib/prefab/criteria_evaluator.rb', line 111 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
115 116 117 |
# File 'lib/prefab/criteria_evaluator.rb', line 115 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
93 94 95 96 97 98 99 100 |
# File 'lib/prefab/criteria_evaluator.rb', line 93 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
139 140 141 |
# File 'lib/prefab/criteria_evaluator.rb', line 139 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
143 144 145 |
# File 'lib/prefab/criteria_evaluator.rb', line 143 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
135 136 137 |
# File 'lib/prefab/criteria_evaluator.rb', line 135 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
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/prefab/criteria_evaluator.rb', line 147 def value_from_properties(criterion, properties) case criterion.property_name when NAMESPACE_KEY @namespace when 'prefab.current-time' Time.now.utc.to_i * 1000 else properties.get(criterion.property_name) end end |