Sha256: dba61e06f9010c21cd9dd85e929e9fb8c520980c216ee2f193e91bb66dc6f1e9

Contents?: true

Size: 1.49 KB

Versions: 62

Compression:

Stored size: 1.49 KB

Contents

module SplitIoClient
  #
  # class to implement the user defined matcher
  #
  class WhitelistMatcher < NoMethodError
    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 @whitelist.include?(args[:value] || args[:matching_key]) unless @matcher_type == 'ATTR_WHITELIST'

      return false if !args.key?(:attributes) && !args.key?(:value)
      return false if args.key?(:value) && args[:value].nil?
      return false if args.key?(:attributes) && args[:attributes].nil?

      return @whitelist.include?(args[:value] || args[:attributes][@attribute.to_sym])

      false
    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

    def string_type?
      true
    end

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

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
splitclient-rb-4.3.1 lib/splitclient-rb/engine/matchers/whitelist_matcher.rb
splitclient-rb-4.3.0 lib/splitclient-rb/engine/matchers/whitelist_matcher.rb