Class: ScottBarron::Acts::StateMachine::SupportingClasses::StateTransition

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ StateTransition

Returns a new instance of StateTransition.



44
45
46
47
48
49
# File 'lib/acts_as_state_machine.rb', line 44

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

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



42
43
44
# File 'lib/acts_as_state_machine.rb', line 42

def from
  @from
end

#optsObject (readonly)

Returns the value of attribute opts.



42
43
44
# File 'lib/acts_as_state_machine.rb', line 42

def opts
  @opts
end

#toObject (readonly)

Returns the value of attribute to.



42
43
44
# File 'lib/acts_as_state_machine.rb', line 42

def to
  @to
end

Instance Method Details

#==(obj) ⇒ Object



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

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

#guard(obj) ⇒ Object



51
52
53
# File 'lib/acts_as_state_machine.rb', line 51

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

#perform(record) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/acts_as_state_machine.rb', line 55

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

  next_state.entering(record) unless loopback

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

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