Class: StateMachina::Event
- Inherits:
-
Object
- Object
- StateMachina::Event
- Defined in:
- lib/state_machina/event.rb
Instance Attribute Summary collapse
-
#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.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(model_name, machine_name, name, metadata: {}) ⇒ Event
constructor
A new instance of Event.
- #permitted? ⇒ Boolean
- #possible? ⇒ Boolean
- #resolve_to_transition ⇒ Object
- #transition(from_state_name, to_state_name, guard: nil, metadata: {}) ⇒ Object
- #transitions ⇒ Object
Constructor Details
#initialize(model_name, machine_name, name, metadata: {}) ⇒ Event
Returns a new instance of Event.
7 8 9 10 11 12 |
# File 'lib/state_machina/event.rb', line 7 def initialize(model_name, machine_name, name, metadata: {}) @model_name = model_name @machine_name = machine_name @name = name.to_s @metadata = end |
Instance Attribute Details
#guard_context ⇒ Object
Returns the value of attribute guard_context.
4 5 6 |
# File 'lib/state_machina/event.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/event.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/event.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/event.rb', line 3 def @metadata end |
#model_name ⇒ Object (readonly)
Returns the value of attribute model_name.
3 4 5 |
# File 'lib/state_machina/event.rb', line 3 def model_name @model_name end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/state_machina/event.rb', line 3 def name @name end |
Instance Method Details
#execute ⇒ Object
31 32 33 |
# File 'lib/state_machina/event.rb', line 31 def execute resolve_to_transition&.execute end |
#permitted? ⇒ Boolean
43 44 45 |
# File 'lib/state_machina/event.rb', line 43 def permitted? transitions.any?(&:permitted?) end |
#possible? ⇒ Boolean
39 40 41 |
# File 'lib/state_machina/event.rb', line 39 def possible? transitions.any?(&:possible?) end |
#resolve_to_transition ⇒ Object
35 36 37 |
# File 'lib/state_machina/event.rb', line 35 def resolve_to_transition transitions.find(&:permitted?) end |
#transition(from_state_name, to_state_name, guard: nil, metadata: {}) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/state_machina/event.rb', line 14 def transition(from_state_name, to_state_name, guard: nil, metadata: {}) machine = StateMachina::Registry.find_machine!(model_name, machine_name) transition = StateMachina::Transition.new(model_name, machine.name, name, from_state_name, to_state_name, guard: guard, metadata: ) StateMachina::Registry.register_transition(transition) end |
#transitions ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/state_machina/event.rb', line 21 def transitions transitions = StateMachina::Registry.find_transitions(model_name, machine_name, name).map(&:dup) transitions.each do |transition| transition.machine = machine transition.guard_context = guard_context end transitions end |