Class: Mengpaneel::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/mengpaneel/manager.rb

Constant Summary collapse

MODES =
%w(before_setup setup tracking).map(&:to_sym).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller = nil, &block) ⇒ Manager

Returns a new instance of Manager


14
15
16
17
18
19
20
# File 'lib/mengpaneel/manager.rb', line 14

def initialize(controller = nil, &block)
  @controller = controller

  @mode = :tracking

  wrap(&block) if block
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller


9
10
11
# File 'lib/mengpaneel/manager.rb', line 9

def controller
  @controller
end

#flushing_strategyObject

Returns the value of attribute flushing_strategy


12
13
14
# File 'lib/mengpaneel/manager.rb', line 12

def flushing_strategy
  @flushing_strategy
end

#modeObject (readonly)

Returns the value of attribute mode


10
11
12
# File 'lib/mengpaneel/manager.rb', line 10

def mode
  @mode
end

Instance Method Details

#all_callsObject


43
44
45
# File 'lib/mengpaneel/manager.rb', line 43

def all_calls
  Hash[call_proxies.map { |mode, call_proxy| [mode, call_proxy.all_calls] }]
end

#before_setup(&block) ⇒ Object


62
63
64
# File 'lib/mengpaneel/manager.rb', line 62

def before_setup(&block)
  with_mode(:before_setup, &block)
end

#call_proxyObject Also known as: mixpanel


34
35
36
# File 'lib/mengpaneel/manager.rb', line 34

def call_proxy
  call_proxies[@mode]
end

#clear_calls(mode = @mode) ⇒ Object


39
40
41
# File 'lib/mengpaneel/manager.rb', line 39

def clear_calls(mode = @mode)
  call_proxies.delete(mode)
end

#flush_callsObject


83
84
85
# File 'lib/mengpaneel/manager.rb', line 83

def flush_calls
  Flusher.new(self).run
end

#replay_delayed_callsObject


79
80
81
# File 'lib/mengpaneel/manager.rb', line 79

def replay_delayed_calls
  Replayer.new(self).run
end

#setup(&block) ⇒ Object


66
67
68
69
# File 'lib/mengpaneel/manager.rb', line 66

def setup(&block)
  clear_calls(:setup)
  with_mode(:setup, &block)
end

#setup?Boolean

Returns:

  • (Boolean)

75
76
77
# File 'lib/mengpaneel/manager.rb', line 75

def setup?
  call_proxies[:setup].all_calls.length > 0
end

#tracking(&block) ⇒ Object


71
72
73
# File 'lib/mengpaneel/manager.rb', line 71

def tracking(&block)
  with_mode(:tracking, &block)
end

#with_mode(mode, &block) ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mengpaneel/manager.rb', line 47

def with_mode(mode, &block)
  original_mode = @mode
  @mode = mode

  begin
    if block.arity == 1
      yield(mixpanel)
    else
      yield
    end
  ensure
    @mode = original_mode
  end
end

#wrap(&block) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mengpaneel/manager.rb', line 22

def wrap(&block)
  replay_delayed_calls
  
  if block.arity == 1
    yield(self)
  else
    yield
  end
ensure
  flush_calls
end