Sha256: 5681f669809bc9040b9ffc088cf254f230bf1ae447ce625ad2a22d8199d999ee

Contents?: true

Size: 1.55 KB

Versions: 3

Compression:

Stored size: 1.55 KB

Contents

module SplitIoClient

  #
  # class to implement the user defined matcher
  #
  class WhitelistMatcher < NoMethodError

    attr_reader :matcher_type

    # variable that contains the keys of the whitelist
    @whitelist = []

    def initialize(whitelist_data)
      if whitelist_data.instance_of? Array
        @whitelist = whitelist_data unless whitelist_data.nil?
      elsif whitelist_data.instance_of? Hash
        @matcher_type = "ATTR_WHITELIST"
        @attribute = whitelist_data[:attribute]
        @whitelist = whitelist_data[:value] unless whitelist_data[:value].nil?
      end
    end

    def match?(matching_key, _bucketing_key, _evaluator, whitelist_data)
      matches = false
      if !(@matcher_type == "ATTR_WHITELIST")
        matches = @whitelist.include?(matching_key)
      else
        if (!whitelist_data.nil? && whitelist_data.key?(@attribute.to_sym))
          value = whitelist_data[@attribute.to_sym]
          matches = @whitelist.include?(value)
        end
      end
      matches
    end

    #
    # evaluates if the given object equals the matcher
    #
    # @param obj [object] object to be evaluated
    #
    # @returns [boolean] true if obj equals the matcher
    def equals?(obj)
      if obj.nil?
        false
      elsif !obj.instance_of?(WhitelistMatcher)
        false
      elsif self.equal?(obj)
        true
      else
        false
      end
    end

    #
    # function to print string value for this matcher
    #
    # @reutrn [string] string value of this matcher
    def to_s
      'in segment ' + @whitelist.to_s
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
splitclient-rb-4.2.2 lib/splitclient-rb/engine/matchers/whitelist_matcher.rb
splitclient-rb-4.2.1 lib/splitclient-rb/engine/matchers/whitelist_matcher.rb
splitclient-rb-4.2.0 lib/splitclient-rb/engine/matchers/whitelist_matcher.rb