Class: Acts::As::Multiple::StateMachines::SupportingClasses::StateTransition
- Inherits:
-
Object
- Object
- Acts::As::Multiple::StateMachines::SupportingClasses::StateTransition
- Defined in:
- lib/acts/as/multiple/state_machines/supporting_classes/state_transition.rb
Instance Attribute Summary collapse
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#sm ⇒ Object
readonly
Returns the value of attribute sm.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
Instance Method Summary collapse
- #==(obj) ⇒ Object
- #guard(obj) ⇒ Object
-
#initialize(state_machine, options) ⇒ StateTransition
constructor
A new instance of StateTransition.
- #perform(record) ⇒ Object
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, ) @from = [:from].to_s @to = [:to].to_s @guard = [:guard] || NOOP @opts = @sm = state_machine end |
Instance Attribute Details
#from ⇒ Object (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 |
#opts ⇒ Object (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 |
#sm ⇒ Object (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 |
#to ⇒ Object (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 |