Class: SplitIoClient::Cache::Repositories::SplitsRepository
- Inherits:
-
SplitIoClient::Cache::Repository
- Object
- SplitIoClient::Cache::Repository
- SplitIoClient::Cache::Repositories::SplitsRepository
- Defined in:
- lib/cache/repositories/splits_repository.rb
Constant Summary collapse
- SPLITS_SLICE =
10
Instance Method Summary collapse
- #add_split(split) ⇒ Object
- #exists?(name) ⇒ Boolean
- #get_change_number ⇒ Object
- #get_split(name) ⇒ Object
- #get_splits(names, slice = SPLITS_SLICE) ⇒ Object
-
#initialize(adapter) ⇒ SplitsRepository
constructor
A new instance of SplitsRepository.
- #remove_split(name) ⇒ Object
- #set_change_number(since) ⇒ Object
- #set_segment_names(names) ⇒ Object
-
#split_names ⇒ Object
Return an array of Split Names excluding control keys like split.till.
- #splits ⇒ Object
Methods inherited from SplitIoClient::Cache::Repository
Constructor Details
#initialize(adapter) ⇒ SplitsRepository
Returns a new instance of SplitsRepository.
9 10 11 12 13 14 |
# File 'lib/cache/repositories/splits_repository.rb', line 9 def initialize(adapter) @adapter = adapter @adapter.set_string(namespace_key('split.till'), '-1') @adapter.initialize_map(namespace_key('segments.registered')) end |
Instance Method Details
#add_split(split) ⇒ Object
16 17 18 |
# File 'lib/cache/repositories/splits_repository.rb', line 16 def add_split(split) @adapter.set_string(namespace_key("split.#{split[:name]}"), split.to_json) end |
#exists?(name) ⇒ Boolean
80 81 82 |
# File 'lib/cache/repositories/splits_repository.rb', line 80 def exists?(name) @adapter.exists?(namespace_key("split.#{name}")) end |
#get_change_number ⇒ Object
68 69 70 |
# File 'lib/cache/repositories/splits_repository.rb', line 68 def get_change_number @adapter.string(namespace_key('split.till')) end |
#get_split(name) ⇒ Object
41 42 43 44 45 |
# File 'lib/cache/repositories/splits_repository.rb', line 41 def get_split(name) split = @adapter.string(namespace_key("split.#{name}")) JSON.parse(split, symbolize_names: true) if split end |
#get_splits(names, slice = SPLITS_SLICE) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/cache/repositories/splits_repository.rb', line 24 def get_splits(names, slice = SPLITS_SLICE) splits = {} names.each_slice(slice) do |splits_slice| splits.merge!( @adapter .multiple_strings(splits_slice.map { |name| namespace_key("split.#{name}") }) .map { |name, data| [name.gsub(namespace_key('split.'), ''), data] }.to_h ) end splits.map do |name, data| parsed_data = data ? JSON.parse(data, symbolize_names: true) : nil [name.to_sym, parsed_data] end.to_h end |
#remove_split(name) ⇒ Object
20 21 22 |
# File 'lib/cache/repositories/splits_repository.rb', line 20 def remove_split(name) @adapter.delete(namespace_key("split.#{name}")) end |
#set_change_number(since) ⇒ Object
64 65 66 |
# File 'lib/cache/repositories/splits_repository.rb', line 64 def set_change_number(since) @adapter.set_string(namespace_key('split.till'), since) end |
#set_segment_names(names) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/cache/repositories/splits_repository.rb', line 72 def set_segment_names(names) return if names.nil? || names.empty? names.each do |name| @adapter.add_to_set(namespace_key('segments.registered'), name) end end |
#split_names ⇒ Object
Return an array of Split Names excluding control keys like split.till
58 59 60 61 62 |
# File 'lib/cache/repositories/splits_repository.rb', line 58 def split_names @adapter.find_strings_by_prefix(namespace_key('split')) .reject { |split| split == namespace_key('split.till') } .map { |split| split.gsub(namespace_key('split.'), '') } end |
#splits ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/cache/repositories/splits_repository.rb', line 47 def splits splits_hash = {} split_names.each do |name| splits_hash[name] = get_split(name) end splits_hash end |