Sha256: 0f5f9a378a4c314a801916cfe8f89784bd26b57d719a08c1c93ec46a9e0c2ed3

Contents?: true

Size: 1.71 KB

Versions: 28

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module SplitIoClient
  #
  # class to implement the user defined matcher
  #
  class WhitelistMatcher < Matcher
    MATCHER_TYPE = 'WHITELIST_MATCHER'

    attr_reader :attribute

    def initialize(whitelist_data)
      @whitelist = case whitelist_data
                   when Array
                     whitelist_data
                   when Hash
                     @matcher_type = 'ATTR_WHITELIST'
                     @attribute = whitelist_data[:attribute]

                     whitelist_data[:value]
                   else
                     []
                   end
    end

    def match?(args)
      return matches_user_whitelist(args) unless @matcher_type == 'ATTR_WHITELIST'

      SplitLogger.log_if_debug('[WhitelistMatcher] evaluating value and attributes.')

      return false unless SplitIoClient::Validators.valid_matcher_arguments(args)

      matches_attr_whitelist(args)
    end

    def string_type?
      true
    end

    #
    # function to print string value for this matcher
    #
    # @return [string] string value of this matcher
    def to_s
      "in segment #{@whitelist}"
    end

    private

    def matches_user_whitelist(args)
      matches = @whitelist.include?(args[:value] || args[:matching_key])
      SplitLogger.log_if_debug("[WhitelistMatcher] #{@whitelist} include \
        #{args[:value] || args[:matching_key]} -> #{matches}")
      matches
    end

    def matches_attr_whitelist(args)
      matches = @whitelist.include?(args[:value] || args[:attributes][@attribute.to_sym])
      SplitLogger.log_if_debug("[WhitelistMatcher] #{@whitelist} include \
        #{args[:value] || args[:attributes][@attribute.to_sym]} -> #{matches}")
      matches
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
splitclient-rb-6.4.1-java lib/splitclient-rb/engine/matchers/whitelist_matcher.rb
splitclient-rb-6.4.1 lib/splitclient-rb/engine/matchers/whitelist_matcher.rb
splitclient-rb-6.4.1.pre.rc3-java lib/splitclient-rb/engine/matchers/whitelist_matcher.rb
splitclient-rb-6.4.1.pre.rc3 lib/splitclient-rb/engine/matchers/whitelist_matcher.rb
splitclient-rb-6.4.1.pre.rc2-java lib/splitclient-rb/engine/matchers/whitelist_matcher.rb
splitclient-rb-6.4.1.pre.rc2 lib/splitclient-rb/engine/matchers/whitelist_matcher.rb
splitclient-rb-6.4.1.pre.rc1-java lib/splitclient-rb/engine/matchers/whitelist_matcher.rb
splitclient-rb-6.4.1.pre.rc1 lib/splitclient-rb/engine/matchers/whitelist_matcher.rb
splitclient-rb-6.4.0-java lib/splitclient-rb/engine/matchers/whitelist_matcher.rb
splitclient-rb-6.4.0 lib/splitclient-rb/engine/matchers/whitelist_matcher.rb
splitclient-rb-6.4.0.pre.rc1-java lib/splitclient-rb/engine/matchers/whitelist_matcher.rb
splitclient-rb-6.4.0.pre.rc1 lib/splitclient-rb/engine/matchers/whitelist_matcher.rb
splitclient-rb-6.3.0-java lib/splitclient-rb/engine/matchers/whitelist_matcher.rb
splitclient-rb-6.3.0 lib/splitclient-rb/engine/matchers/whitelist_matcher.rb
splitclient-rb-6.3.0.pre.rc1-java lib/splitclient-rb/engine/matchers/whitelist_matcher.rb
splitclient-rb-6.3.0.pre.rc1 lib/splitclient-rb/engine/matchers/whitelist_matcher.rb
splitclient-rb-6.2.0-java lib/splitclient-rb/engine/matchers/whitelist_matcher.rb
splitclient-rb-6.2.0 lib/splitclient-rb/engine/matchers/whitelist_matcher.rb
splitclient-rb-6.2.0.pre.rc2-java lib/splitclient-rb/engine/matchers/whitelist_matcher.rb
splitclient-rb-6.2.0.pre.rc2 lib/splitclient-rb/engine/matchers/whitelist_matcher.rb