Class: Unleash::VariantOverride

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context_name, values = []) ⇒ VariantOverride

Returns a new instance of VariantOverride.



5
6
7
8
9
10
# File 'lib/unleash/variant_override.rb', line 5

def initialize(context_name, values = [])
  self.context_name = context_name
  self.values = values || []

  validate
end

Instance Attribute Details

#context_nameObject

Returns the value of attribute context_name.



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

def context_name
  @context_name
end

#valuesObject

Returns the value of attribute values.



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

def values
  @values
end

Instance Method Details

#matches_context?(context) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/unleash/variant_override.rb', line 16

def matches_context?(context)
  raise ArgumentError, 'context must be of class Unleash::Context' unless context.instance_of?(Unleash::Context)

  context_value =
    case self.context_name
    when 'userId'
      context.user_id
    when 'sessionId'
      context.session_id
    when 'remoteAddress'
      context.remote_address
    else
      context.properties.fetch(self.context_name, nil)
    end

  Unleash.logger.debug "VariantOverride: context_name: #{context_name} context_value: #{context_value}"

  self.values.include? context_value.to_s
end

#to_sObject



12
13
14
# File 'lib/unleash/variant_override.rb', line 12

def to_s
  "<VariantOverride: context_name=#{self.context_name},values=#{self.values}>"
end