Class: ScottBarron::Acts::StateMachine::SupportingClasses::StateTransition
- Inherits:
-
Object
- Object
- ScottBarron::Acts::StateMachine::SupportingClasses::StateTransition
- Defined in:
- lib/acts_as_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.
44 45 46 47 48 49 |
# File 'lib/acts_as_state_machine.rb', line 44 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.
42 43 44 |
# File 'lib/acts_as_state_machine.rb', line 42 def from @from end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
42 43 44 |
# File 'lib/acts_as_state_machine.rb', line 42 def opts @opts end |
#to ⇒ Object (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 |