Class: SplitIoClient::Cache::Repositories::RuleBasedSegmentsRepository

Inherits:
SplitIoClient::Cache::Repository show all
Defined in:
lib/splitclient-rb/cache/repositories/rule_based_segments_repository.rb

Constant Summary collapse

DEFAULT_CONDITIONS_TEMPLATE =
[{
  conditionType: "ROLLOUT",
  matcherGroup: {
      combiner: "AND",
      matchers: [
      {
          keySelector: nil,
          matcherType: "ALL_KEYS",
          negate: false,
          userDefinedSegmentMatcherData: nil,
          whitelistMatcherData: nil,
          unaryNumericMatcherData: nil,
          betweenMatcherData: nil,
          dependencyMatcherData: nil,
          booleanMatcherData: nil,
          stringMatcherData: nil
      }]
  }
}]
TILL_PREFIX =
'.rbsegments.till'
RB_SEGMENTS_PREFIX =
'.rbsegment.'
REGISTERED_PREFIX =
'.segments.registered'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from SplitIoClient::Cache::Repository

#set_string, #string

Constructor Details

#initialize(config) ⇒ RuleBasedSegmentsRepository

Returns a new instance of RuleBasedSegmentsRepository.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/splitclient-rb/cache/repositories/rule_based_segments_repository.rb', line 31

def initialize(config)
  super(config)
  @adapter = case @config.cache_adapter.class.to_s
  when 'SplitIoClient::Cache::Adapters::RedisAdapter'
    SplitIoClient::Cache::Adapters::CacheAdapter.new(@config)
  else
    @config.cache_adapter
  end
  unless @config.mode.equal?(:consumer)
    @adapter.set_string(namespace_key(TILL_PREFIX), '-1')
    @adapter.initialize_map(namespace_key(REGISTERED_PREFIX))
  end
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



7
8
9
# File 'lib/splitclient-rb/cache/repositories/rule_based_segments_repository.rb', line 7

def adapter
  @adapter
end

Instance Method Details

#clearObject



82
83
84
# File 'lib/splitclient-rb/cache/repositories/rule_based_segments_repository.rb', line 82

def clear
  @adapter.clear(namespace_key)
end

#contains?(segment_names) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
# File 'lib/splitclient-rb/cache/repositories/rule_based_segments_repository.rb', line 86

def contains?(segment_names)
  return false if rule_based_segment_names.empty?

  return segment_names.to_set.subset?(rule_based_segment_names.to_set)
end

#exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/splitclient-rb/cache/repositories/rule_based_segments_repository.rb', line 78

def exists?(name)
  @adapter.exists?(namespace_key("#{RB_SEGMENTS_PREFIX}#{name}"))
end

#get_change_numberObject



66
67
68
# File 'lib/splitclient-rb/cache/repositories/rule_based_segments_repository.rb', line 66

def get_change_number
  @adapter.string(namespace_key(TILL_PREFIX))
end

#get_rule_based_segment(name) ⇒ Object



51
52
53
54
55
# File 'lib/splitclient-rb/cache/repositories/rule_based_segments_repository.rb', line 51

def get_rule_based_segment(name)
  rule_based_segment = @adapter.string(namespace_key("#{RB_SEGMENTS_PREFIX}#{name}"))

  JSON.parse(rule_based_segment, symbolize_names: true) if rule_based_segment
end

#rule_based_segment_namesObject



57
58
59
60
# File 'lib/splitclient-rb/cache/repositories/rule_based_segments_repository.rb', line 57

def rule_based_segment_names
  @adapter.find_strings_by_prefix(namespace_key(RB_SEGMENTS_PREFIX))
    .map { |rule_based_segment_names| rule_based_segment_names.gsub(namespace_key(RB_SEGMENTS_PREFIX), '') }
end

#set_change_number(since) ⇒ Object



62
63
64
# File 'lib/splitclient-rb/cache/repositories/rule_based_segments_repository.rb', line 62

def set_change_number(since)
  @adapter.set_string(namespace_key(TILL_PREFIX), since)
end

#set_segment_names(names) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/splitclient-rb/cache/repositories/rule_based_segments_repository.rb', line 70

def set_segment_names(names)
  return if names.nil? || names.empty?

  names.each do |name|
    @adapter.add_to_set(namespace_key(REGISTERED_PREFIX), name)
  end
end

#update(to_add, to_delete, new_change_number) ⇒ Object



45
46
47
48
49
# File 'lib/splitclient-rb/cache/repositories/rule_based_segments_repository.rb', line 45

def update(to_add, to_delete, new_change_number)
  to_add.each{ |rule_based_segment| add_rule_based_segment(rule_based_segment) }
  to_delete.each{ |rule_based_segment| remove_rule_based_segment(rule_based_segment) }
  set_change_number(new_change_number)
end