Class: Split::Persistence::DualAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/split/persistence/dual_adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ DualAdapter

Returns a new instance of DualAdapter.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/split/persistence/dual_adapter.rb', line 15

def initialize(context)
  if logged_in = self.class.config[:logged_in]
  else
    raise "Please configure :logged_in"
  end
  if logged_in_adapter = self.class.config[:logged_in_adapter]
  else
    raise "Please configure :logged_in_adapter"
  end
  if logged_out_adapter = self.class.config[:logged_out_adapter]
  else
    raise "Please configure :logged_out_adapter"
  end

  @fallback_to_logged_out_adapter =
    self.class.config[:fallback_to_logged_out_adapter] || false
  @logged_in = logged_in.call(context)
  @logged_in_adapter = logged_in_adapter.new(context)
  @logged_out_adapter = logged_out_adapter.new(context)
  @active_adapter = @logged_in ? @logged_in_adapter : @logged_out_adapter
end

Class Method Details

.configObject



11
12
13
# File 'lib/split/persistence/dual_adapter.rb', line 11

def self.config
  @config ||= {}
end

.with_config(options = {}) ⇒ Object



6
7
8
9
# File 'lib/split/persistence/dual_adapter.rb', line 6

def self.with_config(options = {})
  self.config.merge!(options)
  self
end

Instance Method Details

#[](key) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/split/persistence/dual_adapter.rb', line 45

def [](key)
  if @fallback_to_logged_out_adapter
    @logged_in && @logged_in_adapter[key] || @logged_out_adapter[key]
  else
    @active_adapter[key]
  end
end

#[]=(key, value) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/split/persistence/dual_adapter.rb', line 53

def []=(key, value)
  if @fallback_to_logged_out_adapter
    @logged_in_adapter[key] = value if @logged_in
    old_value = @logged_out_adapter[key]
    @logged_out_adapter[key] = value

    decrement_participation(key, old_value) if decrement_participation?(old_value, value)
  else
    @active_adapter[key] = value
  end
end

#delete(key) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/split/persistence/dual_adapter.rb', line 65

def delete(key)
  if @fallback_to_logged_out_adapter
    @logged_in_adapter.delete(key)
    @logged_out_adapter.delete(key)
  else
    @active_adapter.delete(key)
  end
end

#keysObject



37
38
39
40
41
42
43
# File 'lib/split/persistence/dual_adapter.rb', line 37

def keys
  if @fallback_to_logged_out_adapter
    (@logged_in_adapter.keys + @logged_out_adapter.keys).uniq
  else
    @active_adapter.keys
  end
end