Sha256: 22854683bcd3485f9d667c642446c852ccef05d785e825fb1a3e0accf885ab89

Contents?: true

Size: 1.22 KB

Versions: 17

Compression:

Stored size: 1.22 KB

Contents

module Unleash
  class VariantOverride
    attr_accessor :context_name, :values

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

      validate
    end

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

    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

    private

    def validate
      raise ArgumentError, 'context_name must be a String' unless self.context_name.is_a?(String)
      raise ArgumentError, 'values must be an Array of strings' unless self.values.is_a?(Array) \
          && self.values.reject{ |v| v.is_a?(String) }.empty?
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
unleash-5.1.1 lib/unleash/variant_override.rb
unleash-5.1.0 lib/unleash/variant_override.rb
unleash-5.0.7 lib/unleash/variant_override.rb
unleash-5.0.6 lib/unleash/variant_override.rb
unleash-5.0.5 lib/unleash/variant_override.rb
unleash-5.0.4 lib/unleash/variant_override.rb
unleash-5.0.3 lib/unleash/variant_override.rb
unleash-5.0.2 lib/unleash/variant_override.rb
unleash-5.0.1 lib/unleash/variant_override.rb
unleash-5.0.0 lib/unleash/variant_override.rb
unleash-4.6.0 lib/unleash/variant_override.rb
unleash-4.5.0 lib/unleash/variant_override.rb
unleash-4.4.4 lib/unleash/variant_override.rb
unleash-4.4.3 lib/unleash/variant_override.rb
unleash-4.4.2 lib/unleash/variant_override.rb
unleash-4.4.1 lib/unleash/variant_override.rb
unleash-4.4.0 lib/unleash/variant_override.rb