Class: Mongoid::StateMachine::SupportingClasses::StateTransition

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ StateTransition

Returns a new instance of StateTransition.



46
47
48
49
50
51
# File 'lib/mongoid/state_machine.rb', line 46

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.



44
45
46
# File 'lib/mongoid/state_machine.rb', line 44

def from
  @from
end

#optsObject (readonly)

Returns the value of attribute opts.



44
45
46
# File 'lib/mongoid/state_machine.rb', line 44

def opts
  @opts
end

#toObject (readonly)

Returns the value of attribute to.



44
45
46
# File 'lib/mongoid/state_machine.rb', line 44

def to
  @to
end

Instance Method Details

#==(obj) ⇒ Object



73
74
75
# File 'lib/mongoid/state_machine.rb', line 73

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

#guard(obj) ⇒ Object



53
54
55
# File 'lib/mongoid/state_machine.rb', line 53

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

#perform(record) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mongoid/state_machine.rb', line 57

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

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

  record.send("#{record.class.state_column}=", next_state.value)
  record.save
end