Module: Featurevisor::Conditions
- Defined in:
- lib/featurevisor/conditions.rb
Overview
Conditions module for evaluating feature flags and segments
Class Method Summary collapse
-
.condition_is_matched(condition, context, get_regex) ⇒ Boolean
Check if a condition is matched against context.
-
.get_value_from_context(obj, path) ⇒ Object?
Get value from context object using dot notation path.
Class Method Details
.condition_is_matched(condition, context, get_regex) ⇒ Boolean
Check if a condition is matched against context
27 28 29 30 31 32 33 34 35 36 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/featurevisor/conditions.rb', line 27 def self.condition_is_matched(condition, context, get_regex) attribute = condition["attribute"] || condition[:attribute] operator = condition["operator"] || condition[:operator] value = condition["value"] || condition[:value] regex_flags = condition["regexFlags"] || condition[:regexFlags] context_value_from_path = get_value_from_context(context, attribute) case operator when "equals" context_value_from_path == value when "notEquals" context_value_from_path != value when "before", "after" # date comparisons value_in_context = context_value_from_path date_in_context = value_in_context.is_a?(Date) ? value_in_context : Date.parse(value_in_context.to_s) date_in_condition = value.is_a?(Date) ? value : Date.parse(value.to_s) if operator == "before" date_in_context < date_in_condition else date_in_context > date_in_condition end when "in", "notIn" # in / notIn (where condition value is an array) if value.is_a?(Array) && (context_value_from_path.is_a?(String) || context_value_from_path.is_a?(Numeric) || context_value_from_path.nil?) # Check if the attribute key actually exists in the context key_exists = context.key?(attribute.to_sym) || context.key?(attribute.to_s) # If key doesn't exist, notIn should fail (return false), in should also fail if !key_exists return false end value_in_context = context_value_from_path.to_s if operator == "in" value.include?(value_in_context) else # notIn !value.include?(value_in_context) end else false end when "contains", "notContains", "startsWith", "endsWith", "semverEquals", "semverNotEquals", "semverGreaterThan", "semverGreaterThanOrEquals", "semverLessThan", "semverLessThanOrEquals", "matches", "notMatches" # string operations if context_value_from_path.is_a?(String) && value.is_a?(String) value_in_context = context_value_from_path case operator when "contains" value_in_context.include?(value) when "notContains" !value_in_context.include?(value) when "startsWith" value_in_context.start_with?(value) when "endsWith" value_in_context.end_with?(value) when "semverEquals" Featurevisor.compare_versions(value_in_context, value) == 0 when "semverNotEquals" Featurevisor.compare_versions(value_in_context, value) != 0 when "semverGreaterThan" Featurevisor.compare_versions(value_in_context, value) == 1 when "semverGreaterThanOrEquals" Featurevisor.compare_versions(value_in_context, value) >= 0 when "semverLessThan" Featurevisor.compare_versions(value_in_context, value) == -1 when "semverLessThanOrEquals" Featurevisor.compare_versions(value_in_context, value) <= 0 when "matches" regex = get_regex.call(value, regex_flags || "") regex.match?(value_in_context) when "notMatches" regex = get_regex.call(value, regex_flags || "") !regex.match?(value_in_context) end else false end when "greaterThan", "greaterThanOrEquals", "lessThan", "lessThanOrEquals" # numeric operations if context_value_from_path.is_a?(Numeric) && value.is_a?(Numeric) value_in_context = context_value_from_path case operator when "greaterThan" value_in_context > value when "greaterThanOrEquals" value_in_context >= value when "lessThan" value_in_context < value when "lessThanOrEquals" value_in_context <= value end else false end when "exists" context_value_from_path != nil when "notExists" context_value_from_path.nil? when "includes", "notIncludes" # includes / notIncludes (where context value is an array) if context_value_from_path.is_a?(Array) && value.is_a?(String) value_in_context = context_value_from_path if operator == "includes" value_in_context.include?(value) else # notIncludes !value_in_context.include?(value) end else false end else false end rescue => e # Log error but don't stop execution warn "Error in condition evaluation: #{e.message}" false end |
.get_value_from_context(obj, path) ⇒ Object?
Get value from context object using dot notation path
12 13 14 15 16 17 18 19 20 |
# File 'lib/featurevisor/conditions.rb', line 12 def self.get_value_from_context(obj, path) return nil if obj.nil? || path.nil? if path.index(".") == -1 return obj[path.to_sym] || obj[path] end path.split(".").reduce(obj) { |o, i| o&.[](i.to_sym) || o&.[](i) } end |