Class: Mongoid::StateMachine::SupportingClasses::StateTransition
- Inherits:
-
Object
- Object
- Mongoid::StateMachine::SupportingClasses::StateTransition
- Defined in:
- lib/mongoid/state_machine.rb
Instance Attribute Summary collapse
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
Instance Method Summary collapse
- #==(obj) ⇒ Object
- #guard(obj) ⇒ Object
-
#initialize(options) ⇒ StateTransition
constructor
A new instance of StateTransition.
- #perform(record) ⇒ Object
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() @from = [:from].to_s @to = [:to].to_s @guard = [:guard] || NOOP @opts = end |
Instance Attribute Details
#from ⇒ Object (readonly)
Returns the value of attribute from.
44 45 46 |
# File 'lib/mongoid/state_machine.rb', line 44 def from @from end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
44 45 46 |
# File 'lib/mongoid/state_machine.rb', line 44 def opts @opts end |
#to ⇒ Object (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 |