Class: Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/ibm_appconfiguration_ruby_sdk/configurations/models/rule.rb

Overview

Rule model for segment evaluation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(segment_rules) ⇒ Rule

Returns a new instance of Rule.

Parameters:

  • segment_rules (Hash)

    Rule configuration hash



22
23
24
25
26
# File 'lib/ibm_appconfiguration_ruby_sdk/configurations/models/rule.rb', line 22

def initialize(segment_rules)
  @attribute_name = segment_rules[:attribute_name]
  @operator = segment_rules[:operator]
  @values = segment_rules[:values]
end

Instance Attribute Details

#attribute_nameObject (readonly)

Returns the value of attribute attribute_name.



19
20
21
# File 'lib/ibm_appconfiguration_ruby_sdk/configurations/models/rule.rb', line 19

def attribute_name
  @attribute_name
end

#operatorObject (readonly)

Returns the value of attribute operator.



19
20
21
# File 'lib/ibm_appconfiguration_ruby_sdk/configurations/models/rule.rb', line 19

def operator
  @operator
end

#valuesObject (readonly)

Returns the value of attribute values.



19
20
21
# File 'lib/ibm_appconfiguration_ruby_sdk/configurations/models/rule.rb', line 19

def values
  @values
end

Instance Method Details

#evaluate_rule(entity_attributes) ⇒ Boolean

Evaluates rule against entity attributes

Parameters:

  • entity_attributes (Hash)

    Entity attributes hash

Returns:

  • (Boolean)

    True if evaluation passes



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ibm_appconfiguration_ruby_sdk/configurations/models/rule.rb', line 68

def evaluate_rule(entity_attributes)
  return false unless entity_attributes.is_a?(Hash)

  # Support both string and symbol keys in entity_attributes
  # Check for string key first, then symbol key
  key = if entity_attributes.key?(@attribute_name)
          entity_attributes[@attribute_name]
        elsif entity_attributes.key?(@attribute_name.to_sym)
          entity_attributes[@attribute_name.to_sym]
        else
          return false
        end

  if %w[isNot notContains notStartsWith notEndsWith].include?(@operator)
    @values.all? { |value| operator_check(key, value) }
  else
    @values.any? { |value| operator_check(key, value) }
  end
end

#operator_check(key, value) ⇒ Boolean

Returns True if operator check passes.

Parameters:

  • key (Object)

    Attribute value to check

  • value (Object)

    Value to compare against

Returns:

  • (Boolean)

    True if operator check passes



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
# File 'lib/ibm_appconfiguration_ruby_sdk/configurations/models/rule.rb', line 31

def operator_check(key, value)
  return false if key.nil? || value.nil?

  case @operator
  when "endsWith"
    /#{Regexp.escape(value.to_s)}$/i.match?(key.to_s)
  when "notEndsWith"
    !/#{Regexp.escape(value.to_s)}$/i.match?(key.to_s)
  when "startsWith"
    /^#{Regexp.escape(value.to_s)}/i.match?(key.to_s)
  when "notStartsWith"
    !/^#{Regexp.escape(value.to_s)}/i.match?(key.to_s)
  when "contains"
    key.to_s.include?(value.to_s)
  when "notContains"
    !key.to_s.include?(value.to_s)
  when "is"
    key.is_a?(Numeric) ? key.to_f == value.to_f : key.to_s == value.to_s
  when "isNot"
    key.is_a?(Numeric) ? key.to_f != value.to_f : key.to_s != value.to_s
  when "greaterThan"
    key.to_f > value.to_f
  when "lesserThan"
    key.to_f < value.to_f
  when "greaterThanEquals"
    key.to_f >= value.to_f
  when "lesserThanEquals"
    key.to_f <= value.to_f
  else
    false
  end
end