Class: SplitIoClient::CombiningMatcher

Inherits:
Matcher
  • Object
show all
Defined in:
lib/splitclient-rb/engine/matchers/combining_matcher.rb

Overview

class to implement the combining matcher

Constant Summary collapse

MATCHER_TYPE =
'COMBINING_MATCHER'

Instance Method Summary collapse

Methods inherited from Matcher

#equals?, #string_type?

Constructor Details

#initialize(logger, combiner = '', matchers = []) ⇒ CombiningMatcher

Returns a new instance of CombiningMatcher.



10
11
12
13
14
# File 'lib/splitclient-rb/engine/matchers/combining_matcher.rb', line 10

def initialize(logger, combiner = '', matchers = [])
  super(logger)
  @combiner = combiner
  @matchers = matchers
end

Instance Method Details

#eval_and(args) ⇒ boolean

auxiliary method to evaluate each of the matchers within the combiner

Parameters:

  • matching_key (string)

    key value to be matched

  • bucketing_key (string)

    bucketing key to be matched

  • evaluator (Evaluator)

    used in dependency_matcher

  • attributes (hash)

    attributes to pass to the treatment class

Returns:

  • (boolean)

    match value for combiner delegates



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/splitclient-rb/engine/matchers/combining_matcher.rb', line 53

def eval_and(args)
  # Convert all keys to symbols
  args[:attributes] = args[:attributes].each_with_object({}) { |(k, v), memo| memo[k.to_sym] = v } if args && args[:attributes]

  @matchers.all? do |matcher|
    if match_with_key?(matcher)
      matcher.match?(value: args[:matching_key])
    else
      matcher.match?(args)
    end
  end
end

#match?(args) ⇒ boolean

evaluates if the key matches the matchers within the combiner

Parameters:

  • matching_key (string)

    key value to be matched

  • bucketing_key (string)

    bucketing key to be matched

  • evaluator (instance of Evaluator class)
  • attributes (hash)

Returns:

  • (boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/splitclient-rb/engine/matchers/combining_matcher.rb', line 25

def match?(args)
  if @matchers.empty?
    @logger.log_if_debug('[CombiningMatcher] Matchers Empty')
    return false
  end

  case @combiner
  when Combiners::AND
    matches = eval_and(args)
    @logger.log_if_debug("[CombiningMatcher] Combiner AND result -> #{matches}")
    return matches
  else
    @logger.log_if_debug("[CombiningMatcher] Invalid Combiner Type - Combiner -> #{@combiner}")
    @logger.error('Invalid combiner type')
  end

  false
end

#match_with_key?(matcher) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/splitclient-rb/engine/matchers/combining_matcher.rb', line 66

def match_with_key?(matcher)
  matcher.respond_to?(:attribute) && matcher.attribute.nil? && matcher.string_type?
end

#to_sstring

function to print string value for this matcher

Returns:

  • (string)

    string value of this matcher



74
75
76
# File 'lib/splitclient-rb/engine/matchers/combining_matcher.rb', line 74

def to_s
  @matcher_list.map(&:to_s).join("#{@combiner} ")
end