Class: StateMachina::Transition
- Inherits:
-
Object
- Object
- StateMachina::Transition
- Defined in:
- lib/state_machina/transition.rb
Instance Attribute Summary collapse
-
#event_name ⇒ Object
readonly
Returns the value of attribute event_name.
-
#from_state_name ⇒ Object
readonly
Returns the value of attribute from_state_name.
-
#guard ⇒ Object
readonly
Returns the value of attribute guard.
-
#guard_context ⇒ Object
Returns the value of attribute guard_context.
-
#machine ⇒ Object
(also: #state_machine)
Returns the value of attribute machine.
-
#machine_name ⇒ Object
readonly
Returns the value of attribute machine_name.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#model_name ⇒ Object
readonly
Returns the value of attribute model_name.
-
#to_state_name ⇒ Object
readonly
Returns the value of attribute to_state_name.
Instance Method Summary collapse
- #execute ⇒ Object
- #execute! ⇒ Object
-
#initialize(model_name, machine_name, event_name, from_state_name, to_state_name, guard: nil, metadata: {}) ⇒ Transition
constructor
A new instance of Transition.
- #permitted? ⇒ Boolean
- #possible? ⇒ Boolean
Constructor Details
#initialize(model_name, machine_name, event_name, from_state_name, to_state_name, guard: nil, metadata: {}) ⇒ Transition
Returns a new instance of Transition.
7 8 9 10 11 12 13 14 15 |
# File 'lib/state_machina/transition.rb', line 7 def initialize(model_name, machine_name, event_name, from_state_name, to_state_name, guard: nil, metadata: {}) @model_name = model_name @machine_name = machine_name @event_name = event_name @from_state_name = from_state_name.to_s @to_state_name = to_state_name.to_s @guard = guard @metadata = end |
Instance Attribute Details
#event_name ⇒ Object (readonly)
Returns the value of attribute event_name.
3 4 5 |
# File 'lib/state_machina/transition.rb', line 3 def event_name @event_name end |
#from_state_name ⇒ Object (readonly)
Returns the value of attribute from_state_name.
3 4 5 |
# File 'lib/state_machina/transition.rb', line 3 def from_state_name @from_state_name end |
#guard ⇒ Object (readonly)
Returns the value of attribute guard.
3 4 5 |
# File 'lib/state_machina/transition.rb', line 3 def guard @guard end |
#guard_context ⇒ Object
Returns the value of attribute guard_context.
4 5 6 |
# File 'lib/state_machina/transition.rb', line 4 def guard_context @guard_context end |
#machine ⇒ Object Also known as: state_machine
Returns the value of attribute machine.
4 5 6 |
# File 'lib/state_machina/transition.rb', line 4 def machine @machine end |
#machine_name ⇒ Object (readonly)
Returns the value of attribute machine_name.
3 4 5 |
# File 'lib/state_machina/transition.rb', line 3 def machine_name @machine_name end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
3 4 5 |
# File 'lib/state_machina/transition.rb', line 3 def @metadata end |
#model_name ⇒ Object (readonly)
Returns the value of attribute model_name.
3 4 5 |
# File 'lib/state_machina/transition.rb', line 3 def model_name @model_name end |
#to_state_name ⇒ Object (readonly)
Returns the value of attribute to_state_name.
3 4 5 |
# File 'lib/state_machina/transition.rb', line 3 def to_state_name @to_state_name end |
Instance Method Details
#execute ⇒ Object
42 43 44 45 46 |
# File 'lib/state_machina/transition.rb', line 42 def execute return false if machine.nil? machine.update_state(to_state_name) end |
#execute! ⇒ Object
48 49 50 51 52 |
# File 'lib/state_machina/transition.rb', line 48 def execute! return false if machine.nil? machine.update_state!(to_state_name) end |
#permitted? ⇒ Boolean
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/state_machina/transition.rb', line 23 def permitted? return false unless possible? return true if guard.nil? return false if guard_context.nil? case guard when Symbol guard_context.public_send(guard) # Supports `guard: :meat?` when Proc if guard.arity >= 1 guard.call(guard_context) # Supports `guard: (model) -> { model.meat? }` else guard_context.instance_exec(&guard) # Supports `guard: -> { meat? }` end else guard.call(guard_context) if guard.respond_to?(:call) # Supports `guard: CallableObject` end end |
#possible? ⇒ Boolean
17 18 19 20 21 |
# File 'lib/state_machina/transition.rb', line 17 def possible? return false if machine.nil? from_state_name == machine.current_state_name end |