File: constraint.rb

package info (click to toggle)
ruby-unleash 3.2.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 252 kB
  • sloc: ruby: 1,098; makefile: 10; sh: 4
file content (26 lines) | stat: -rw-r--r-- 974 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
module Unleash
  class Constraint
    attr_accessor :context_name, :operator, :values

    VALID_OPERATORS = ['IN', 'NOT_IN'].freeze

    def initialize(context_name, operator, values = [])
      raise ArgumentError, "context_name is not a String" unless context_name.is_a?(String)
      raise ArgumentError, "operator does not hold a valid value:" + VALID_OPERATORS unless VALID_OPERATORS.include? operator
      raise ArgumentError, "values does not hold an Array" unless values.is_a?(Array)

      self.context_name = context_name
      self.operator = operator
      self.values = values
    end

    def matches_context?(context)
      Unleash.logger.debug "Unleash::Constraint matches_context? values: #{self.values} context.get_by_name(#{self.context_name})" \
        " #{context.get_by_name(self.context_name)} "

      is_included = self.values.include? context.get_by_name(self.context_name)

      operator == 'IN' ? is_included : !is_included
    end
  end
end