Module: Featurevisor::Conditions

Defined in:
lib/featurevisor/conditions.rb

Overview

Conditions module for evaluating feature flags and segments

Class Method Summary collapse

Class Method Details

.condition_is_matched(condition, context, get_regex) ⇒ Boolean

Check if a condition is matched against context

Parameters:

  • condition (Hash)

    Condition to evaluate

  • context (Hash)

    Context to evaluate against

  • get_regex (Proc)

    Function to get regex for pattern matching

Returns:

  • (Boolean)

    True if condition matches



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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/featurevisor/conditions.rb', line 55

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]

  raw_context_value = get_value_with_presence(context, attribute)
  context_value_from_path = raw_context_value.equal?(MISSING) ? nil : raw_context_value
  attribute_exists = !raw_context_value.equal?(MISSING)

  case operator
  when "equals"
    attribute_exists && strict_equal?(context_value_from_path, value)
  when "notEquals"
    !attribute_exists || !strict_equal?(context_value_from_path, value)
  when "before", "after"
    date_in_context = portable_date(context_value_from_path)
    date_in_condition = portable_date(value)
    return false unless date_in_context && date_in_condition

    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 attribute_exists && value.is_a?(Array) &&
       (context_value_from_path.is_a?(String) || context_value_from_path.is_a?(Numeric) || context_value_from_path.nil?)
      matched = value.any? { |candidate| strict_equal?(candidate, context_value_from_path) }
      if operator == "in"
        matched
      else # notIn
        !matched
      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"
    attribute_exists
  when "notExists"
    !attribute_exists
  when "includes", "notIncludes"
    # includes / notIncludes (where context value is an array)
    if context_value_from_path.is_a?(Array) &&
       (value.is_a?(String) || value.is_a?(Numeric) || value == true || value == false || value.nil?)
      matched = context_value_from_path.any? { |candidate| strict_equal?(candidate, value) }
      if operator == "includes"
        matched
      else # notIncludes
        !matched
      end
    else
      false
    end
  else
    false
  end
end

.get_value_from_context(obj, path) ⇒ Object?

Get value from context object using dot notation path

Parameters:

  • obj (Hash)

    Context object

  • path (String)

    Dot-separated path to the value

Returns:

  • (Object, nil)

    Value at the path or nil if not found



16
17
18
19
20
21
# File 'lib/featurevisor/conditions.rb', line 16

def self.get_value_from_context(obj, path)
  return nil if obj.nil? || path.nil?

  value = get_value_with_presence(obj, path)
  value.equal?(MISSING) ? nil : value
end