Class: Rule
- Inherits:
-
Object
- Object
- Rule
- Defined in:
- lib/ibm_appconfiguration_ruby_sdk/configurations/models/rule.rb
Overview
Rule model for segment evaluation
Instance Attribute Summary collapse
-
#attribute_name ⇒ Object
readonly
Returns the value of attribute attribute_name.
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Instance Method Summary collapse
-
#evaluate_rule(entity_attributes) ⇒ Boolean
Evaluates rule against entity attributes.
-
#initialize(segment_rules) ⇒ Rule
constructor
A new instance of Rule.
-
#operator_check(key, value) ⇒ Boolean
True if operator check passes.
Constructor Details
#initialize(segment_rules) ⇒ Rule
Returns a new instance of Rule.
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_name ⇒ Object (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 |
#operator ⇒ Object (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 |
#values ⇒ Object (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
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.
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 |