Class: StateMachina::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/state_machina/event.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_contextObject

Returns the value of attribute guard_context.



4
5
6
# File 'lib/state_machina/event.rb', line 4

def guard_context
  @guard_context
end

#machineObject 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_nameObject (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

#metadataObject (readonly)

Returns the value of attribute metadata.



3
4
5
# File 'lib/state_machina/event.rb', line 3

def 
  @metadata
end

#model_nameObject (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

#nameObject (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

#executeObject



31
32
33
# File 'lib/state_machina/event.rb', line 31

def execute
  resolve_to_transition&.execute
end

#permitted?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/state_machina/event.rb', line 43

def permitted?
  transitions.any?(&:permitted?)
end

#possible?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/state_machina/event.rb', line 39

def possible?
  transitions.any?(&:possible?)
end

#resolve_to_transitionObject



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

#transitionsObject



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