Class: TaliaCore::Workflow::SupportingClasses::StateTransition

Inherits:
Object
  • Object
show all
Defined in:
lib/talia_core/workflow.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ StateTransition

Returns a new instance of StateTransition.



45
46
47
48
# File 'lib/talia_core/workflow.rb', line 45

def initialize(opts)
  @from, @to, @guard = opts[:from], opts[:to], opts[:guard]
  @opts = opts
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



43
44
45
# File 'lib/talia_core/workflow.rb', line 43

def from
  @from
end

#optsObject (readonly)

Returns the value of attribute opts.



43
44
45
# File 'lib/talia_core/workflow.rb', line 43

def opts
  @opts
end

#toObject (readonly)

Returns the value of attribute to.



43
44
45
# File 'lib/talia_core/workflow.rb', line 43

def to
  @to
end

Instance Method Details

#==(obj) ⇒ Object



78
79
80
# File 'lib/talia_core/workflow.rb', line 78

def ==(obj)
  @from == obj.from && @to == obj.to
end

#guard(obj, args) ⇒ Object



50
51
52
# File 'lib/talia_core/workflow.rb', line 50

def guard(obj, args)
  @guard ? obj.send(:run_transition_action, @guard, args) : true
end

#on_transition(obj, args = nil) ⇒ Object



54
55
56
57
# File 'lib/talia_core/workflow.rb', line 54

def on_transition(obj, args = nil)
  action = @opts[:on_transition]
  obj.send(:run_transition_action, action, args) if action
end

#perform(record, args = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/talia_core/workflow.rb', line 59

def perform(record, args = nil)
  return false unless guard(record, args)
  loopback = record.current_state == to
  states = record.class.read_inheritable_attribute(:states)
  next_state = states[to]
  old_state = states[record.current_state]
  
  # permform action
  on_transition(record, args)
    
  next_state.entering(record) unless loopback
  
  record.update_attribute(record.class.state_column, to.to_s)
  
  next_state.entered(record) unless loopback
  old_state.exited(record) unless loopback
  true
end