Class: StateMachine::Machine

Inherits:
Object
  • Object
show all
Defined in:
lib/call_center/state_machine_ext.rb

Overview

Extension for StateMachine::Machine to store and provide render blocks

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#callback_blocksObject

Returns the value of attribute callback_blocks.



4
5
6
# File 'lib/call_center/state_machine_ext.rb', line 4

def callback_blocks
  @callback_blocks
end

#flow_actor_blocksObject

Returns the value of attribute flow_actor_blocks.



5
6
7
# File 'lib/call_center/state_machine_ext.rb', line 5

def flow_actor_blocks
  @flow_actor_blocks
end

#response_blocksObject

Returns the value of attribute response_blocks.



3
4
5
# File 'lib/call_center/state_machine_ext.rb', line 3

def response_blocks
  @response_blocks
end

Instance Method Details

#after(state_name, scope, options, &blk) ⇒ Object



17
18
19
20
# File 'lib/call_center/state_machine_ext.rb', line 17

def after(state_name, scope, options, &blk)
  @callback_blocks ||= []
  @callback_blocks << CallCenter::AfterFlowCallback.create(state_name, scope, options, blk)
end

#before(state_name, scope, options, &blk) ⇒ Object



12
13
14
15
# File 'lib/call_center/state_machine_ext.rb', line 12

def before(state_name, scope, options, &blk)
  @callback_blocks ||= []
  @callback_blocks << CallCenter::FlowCallback.create(state_name, :always, options, blk)
end

#block_accessor(accessor, for_state) ⇒ Object



22
23
24
25
26
# File 'lib/call_center/state_machine_ext.rb', line 22

def block_accessor(accessor, for_state)
  return unless respond_to?(accessor)
  blocks = send(accessor)
  blocks[for_state] if blocks
end

#flow_actors(name, &blk) ⇒ Object



28
29
30
31
# File 'lib/call_center/state_machine_ext.rb', line 28

def flow_actors(name, &blk)
  @flow_actor_blocks ||= {}
  @flow_actor_blocks[name] = blk
end

#response(state_name, &blk) ⇒ Object



7
8
9
10
# File 'lib/call_center/state_machine_ext.rb', line 7

def response(state_name, &blk)
  @response_blocks ||= {}
  @response_blocks[state_name] = blk
end

#setup_call_flow(flow) ⇒ Object



33
34
35
36
37
38
# File 'lib/call_center/state_machine_ext.rb', line 33

def setup_call_flow(flow)
  setup_success_blocks
  setup_failure_blocks
  setup_flow_actor_blocks(flow)
  self
end

#setup_failure_blocksObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/call_center/state_machine_ext.rb', line 45

def setup_failure_blocks
  return unless @callback_blocks
  event_names = events.map(&:name)
  event_names.each do |event_name|
    after_failure :on => event_name do |call, transition|
      callbacks = @callback_blocks.select { |callback| callback.after && callback.state_name == transition.to_name && callback.failure } || []
      callbacks.each { |callback| callback.run(call, transition) unless callback.run_deferred?(call, transition) }
    end
  end
end

#setup_flow_actor_blocks(flow_class) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/call_center/state_machine_ext.rb', line 56

def setup_flow_actor_blocks(flow_class)
  return unless @flow_actor_blocks
  @flow_actor_blocks.each do |actor, block|
    flow_class.send(:define_method, actor) do |event|
      self.instance_exec(self, event, &block) if block
    end
  end
end

#setup_success_blocksObject



40
41
42
43
# File 'lib/call_center/state_machine_ext.rb', line 40

def setup_success_blocks
  return unless @callback_blocks
  @callback_blocks.select { |c| c.before || (c.success && c.after) }.each { |callback| callback.setup(self) }
end