Class: SplitIoClient::CombiningMatcher

Inherits:
Object
  • 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'.freeze

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of CombiningMatcher.



8
9
10
11
# File 'lib/splitclient-rb/engine/matchers/combining_matcher.rb', line 8

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

Instance Method Details

#equals?(obj) ⇒ Boolean

evaluates if the given object equals the matcher

Parameters:

  • obj (object)

    object to be evaluated

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
# File 'lib/splitclient-rb/engine/matchers/combining_matcher.rb', line 66

def equals?(obj)
  if obj.nil?
    false
  elsif !obj.instance_of?(CombiningMatcher)
    false
  elsif self.equal?(obj)
    true
  else
    false
  end
end

#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



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/splitclient-rb/engine/matchers/combining_matcher.rb', line 44

def eval_and(args)
  # Convert all keys to symbols
  args[:attributes] = args[:attributes].inject({}){ |memo, (k,v)| memo[k.to_sym] = v; memo } 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)


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/splitclient-rb/engine/matchers/combining_matcher.rb', line 22

def match?(args)
  return false if @matchers.empty?

  case @combiner
  when Combiners::AND
    return eval_and(args)
  else
    @logger.error('Invalid combiner type')
  end

  false
end

#match_with_key?(matcher) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/splitclient-rb/engine/matchers/combining_matcher.rb', line 56

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

#to_sObject

function to print string value for this matcher



82
83
84
# File 'lib/splitclient-rb/engine/matchers/combining_matcher.rb', line 82

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