Class: Unleash::Constraint

Inherits:
Object
  • Object
show all
Defined in:
lib/unleash/constraint.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context_name, operator, values = []) ⇒ Constraint

Returns a new instance of Constraint.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
# File 'lib/unleash/constraint.rb', line 7

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

Instance Attribute Details

#context_nameObject

Returns the value of attribute context_name.



3
4
5
# File 'lib/unleash/constraint.rb', line 3

def context_name
  @context_name
end

#operatorObject

Returns the value of attribute operator.



3
4
5
# File 'lib/unleash/constraint.rb', line 3

def operator
  @operator
end

#valuesObject

Returns the value of attribute values.



3
4
5
# File 'lib/unleash/constraint.rb', line 3

def values
  @values
end

Instance Method Details

#matches_context?(context) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
# File 'lib/unleash/constraint.rb', line 17

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