Class: Flagsmith::Engine::Segments::Condition
- Inherits:
-
Object
- Object
- Flagsmith::Engine::Segments::Condition
- Includes:
- Constants
- Defined in:
- lib/flagsmith/engine/segments/models.rb
Overview
SegmentConditionModel
Constant Summary collapse
- MATCHING_FUNCTIONS =
{ EQUAL => ->(other_value, self_value) { other_value == self_value }, GREATER_THAN => ->(other_value, self_value) { (other_value || false) && other_value > self_value }, GREATER_THAN_INCLUSIVE => ->(other_value, self_value) { (other_value || false) && other_value >= self_value }, LESS_THAN => ->(other_value, self_value) { (other_value || false) && other_value < self_value }, LESS_THAN_INCLUSIVE => ->(other_value, self_value) { (other_value || false) && other_value <= self_value }, NOT_EQUAL => ->(other_value, self_value) { other_value != self_value }, CONTAINS => ->(other_value, self_value) { (other_value || false) && other_value.include?(self_value) }, NOT_CONTAINS => ->(other_value, self_value) { (other_value || false) && !other_value.include?(self_value) }, REGEX => ->(other_value, self_value) { (other_value || false) && other_value.match?(self_value) } }.freeze
Constants included from Constants
Flagsmith::Engine::Segments::Constants::ALL_RULE, Flagsmith::Engine::Segments::Constants::ANY_RULE, Flagsmith::Engine::Segments::Constants::CONDITION_OPERATORS, Flagsmith::Engine::Segments::Constants::CONTAINS, Flagsmith::Engine::Segments::Constants::EQUAL, Flagsmith::Engine::Segments::Constants::GREATER_THAN, Flagsmith::Engine::Segments::Constants::GREATER_THAN_INCLUSIVE, Flagsmith::Engine::Segments::Constants::IN, Flagsmith::Engine::Segments::Constants::IS_NOT_SET, Flagsmith::Engine::Segments::Constants::IS_SET, Flagsmith::Engine::Segments::Constants::LESS_THAN, Flagsmith::Engine::Segments::Constants::LESS_THAN_INCLUSIVE, Flagsmith::Engine::Segments::Constants::MODULO, Flagsmith::Engine::Segments::Constants::NONE_RULE, Flagsmith::Engine::Segments::Constants::NOT_CONTAINS, Flagsmith::Engine::Segments::Constants::NOT_EQUAL, Flagsmith::Engine::Segments::Constants::PERCENTAGE_SPLIT, Flagsmith::Engine::Segments::Constants::REGEX, Flagsmith::Engine::Segments::Constants::RULE_TYPES
Instance Attribute Summary collapse
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
-
#property ⇒ Object
readonly
Returns the value of attribute property.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
-
#format_to_type_of(input) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#initialize(operator:, value:, property: nil) ⇒ Condition
constructor
A new instance of Condition.
- #match_in_value(trait_value) ⇒ Object
-
#match_modulo_value(trait_value) ⇒ Object
rubocop:enable Metrics/AbcSize.
- #match_trait_value?(trait_value) ⇒ Boolean
Constructor Details
#initialize(operator:, value:, property: nil) ⇒ Condition
Returns a new instance of Condition.
51 52 53 54 55 |
# File 'lib/flagsmith/engine/segments/models.rb', line 51 def initialize(operator:, value:, property: nil) @operator = operator @value = value @property = property end |
Instance Attribute Details
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
36 37 38 |
# File 'lib/flagsmith/engine/segments/models.rb', line 36 def operator @operator end |
#property ⇒ Object (readonly)
Returns the value of attribute property.
36 37 38 |
# File 'lib/flagsmith/engine/segments/models.rb', line 36 def property @property end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
36 37 38 |
# File 'lib/flagsmith/engine/segments/models.rb', line 36 def value @value end |
Class Method Details
.build(json) ⇒ Object
97 98 99 |
# File 'lib/flagsmith/engine/segments/models.rb', line 97 def build(json) new(**json.slice(:operator, :value).merge(property: json[:property_])) end |
Instance Method Details
#format_to_type_of(input) ⇒ Object
rubocop:disable Metrics/AbcSize
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/flagsmith/engine/segments/models.rb', line 71 def format_to_type_of(input) { 'String' => ->(v) { v.to_s }, 'Semantic::Version' => ->(v) { Semantic::Version.new(v.to_s.gsub(/:semver$/, '')) }, 'TrueClass' => ->(v) { ['True', 'true', 'TRUE', true, 1, '1'].include?(v) }, 'FalseClass' => ->(v) { !['False', 'false', 'FALSE', false, 0, '0'].include?(v) }, 'Integer' => ->(v) { v.to_i }, 'Float' => ->(v) { v.to_f } }[input.class.to_s] end |
#match_in_value(trait_value) ⇒ Object
90 91 92 93 94 |
# File 'lib/flagsmith/engine/segments/models.rb', line 90 def match_in_value(trait_value) return @value.split(',').include?(trait_value.to_s) if trait_value.is_a?(String) || trait_value.is_a?(Integer) false end |
#match_modulo_value(trait_value) ⇒ Object
rubocop:enable Metrics/AbcSize
83 84 85 86 87 88 |
# File 'lib/flagsmith/engine/segments/models.rb', line 83 def match_modulo_value(trait_value) divisor, remainder = @value.split('|') trait_value.is_a?(Numeric) && trait_value % divisor.to_f == remainder.to_f # rubocop:disable Lint/FloatComparison rescue StandardError false end |
#match_trait_value?(trait_value) ⇒ Boolean
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/flagsmith/engine/segments/models.rb', line 57 def match_trait_value?(trait_value) # handle some exceptions trait_value = Semantic::Version.new(trait_value.gsub(/:semver$/, '')) if @value.is_a?(String) && @value.match?(/:semver$/) return match_in_value(trait_value) if @operator == IN return match_modulo_value(trait_value) if @operator == MODULO type_as_trait_value = format_to_type_of(trait_value) formatted_value = type_as_trait_value ? type_as_trait_value.call(@value) : @value MATCHING_FUNCTIONS[operator]&.call(trait_value, formatted_value) end |