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

#actor(name, &blk) ⇒ Object



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

def actor(name, &blk)
  StateMachine::StateContext.send(:define_method, name.to_sym) do |event_name, options|
    event_name = :"#{name}_#{event_name}"
    event(event_name, options)
  end
  flow_actors(name, &blk)
end

#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



30
31
32
33
34
# File 'lib/call_center/state_machine_ext.rb', line 30

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

#duplicate_to(clazz) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/call_center/state_machine_ext.rb', line 73

def duplicate_to(clazz)
  new_copy = self.clone
  new_copy.owner_class = clazz
  new_copy.define_helpers
  new_copy.define_scopes(@plural)
  new_copy.events.each { |event| event.send(:add_actions) }
  new_copy.states.each { |state| state.send(:add_predicate) }
  new_copy
end

#flow_actors(name, &blk) ⇒ Object



36
37
38
39
# File 'lib/call_center/state_machine_ext.rb', line 36

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



41
42
43
44
45
46
# File 'lib/call_center/state_machine_ext.rb', line 41

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

#setup_failure_blocksObject



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

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



64
65
66
67
68
69
70
71
# File 'lib/call_center/state_machine_ext.rb', line 64

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



48
49
50
51
# File 'lib/call_center/state_machine_ext.rb', line 48

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