Class: Acts::As::Multiple::StateMachines::SupportingClasses::StateTransition

Inherits:
Object
  • Object
show all
Defined in:
lib/acts/as/multiple/state_machines/supporting_classes/state_transition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state_machine, options) ⇒ StateTransition

Returns a new instance of StateTransition.



10
11
12
13
14
15
16
# File 'lib/acts/as/multiple/state_machines/supporting_classes/state_transition.rb', line 10

def initialize(state_machine, options)
  @from  = options[:from].to_s
  @to    = options[:to].to_s
  @guard = options[:guard] || NOOP
  @opts  = options
  @sm = state_machine
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



8
9
10
# File 'lib/acts/as/multiple/state_machines/supporting_classes/state_transition.rb', line 8

def from
  @from
end

#optsObject (readonly)

Returns the value of attribute opts.



8
9
10
# File 'lib/acts/as/multiple/state_machines/supporting_classes/state_transition.rb', line 8

def opts
  @opts
end

#smObject (readonly)

Returns the value of attribute sm.



8
9
10
# File 'lib/acts/as/multiple/state_machines/supporting_classes/state_transition.rb', line 8

def sm
  @sm
end

#toObject (readonly)

Returns the value of attribute to.



8
9
10
# File 'lib/acts/as/multiple/state_machines/supporting_classes/state_transition.rb', line 8

def to
  @to
end

Instance Method Details

#==(obj) ⇒ Object



38
39
40
# File 'lib/acts/as/multiple/state_machines/supporting_classes/state_transition.rb', line 38

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

#guard(obj) ⇒ Object



18
19
20
# File 'lib/acts/as/multiple/state_machines/supporting_classes/state_transition.rb', line 18

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

#perform(record) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/acts/as/multiple/state_machines/supporting_classes/state_transition.rb', line 22

def perform(record)
  return false unless guard(record)
  loopback = sm.current_state(record).to_s == to
  states = sm.state_map
  next_state = states[to]
  old_state = states[sm.current_state(record).to_s]

  next_state.entering(record) unless loopback

  record.update_attribute(sm.state_column, next_state.value)

  next_state.entered(record) unless loopback
  old_state.exited(record) unless loopback
  true
end