Module: StateMachina::Registry

Defined in:
lib/state_machina/registry.rb

Class Method Summary collapse

Class Method Details

.eventsObject



38
39
40
# File 'lib/state_machina/registry.rb', line 38

def self.events
  @events ||= Concurrent::Map.new
end

.find_events(model_name, machine_name) ⇒ Object



42
43
44
# File 'lib/state_machina/registry.rb', line 42

def self.find_events(model_name, machine_name)
  events.fetch(model_name.to_s, {}).fetch(machine_name.to_s, {})
end

.find_machine!(model_name, machine_name) ⇒ Object



11
12
13
# File 'lib/state_machina/registry.rb', line 11

def self.find_machine!(model_name, machine_name)
  machines.fetch(model_name.to_s, {}).fetch(machine_name.to_s, nil) || raise(NoMachineFoundError, "for model: #{model_name}, with name: #{machine_name}")
end

.find_machines(model_name) ⇒ Object



7
8
9
# File 'lib/state_machina/registry.rb', line 7

def self.find_machines(model_name)
  machines.fetch(model_name.to_s, {}).values
end

.find_states(model_name, machine_name) ⇒ Object



26
27
28
# File 'lib/state_machina/registry.rb', line 26

def self.find_states(model_name, machine_name)
  states.fetch(model_name.to_s, {}).fetch(machine_name.to_s, [])
end

.find_transitions(model_name, machine_name, event_name) ⇒ Object



58
59
60
# File 'lib/state_machina/registry.rb', line 58

def self.find_transitions(model_name, machine_name, event_name)
  transitions.fetch(model_name.to_s, {}).fetch(machine_name.to_s, {}).fetch(event_name.to_s, []).map(&:dup)
end

.machinesObject



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

def self.machines
  @machines ||= Concurrent::Map.new
end

.register_event(event) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/state_machina/registry.rb', line 46

def self.register_event(event)
  events[event.model_name] ||= {}
  events[event.model_name][event.machine_name] ||= {}
  events[event.model_name][event.machine_name][event.name] = event.freeze

  event
end

.register_machine(machine) ⇒ Object



15
16
17
18
19
20
# File 'lib/state_machina/registry.rb', line 15

def self.register_machine(machine)
  machines[machine.model_name] ||= {}
  machines[machine.model_name][machine.name] = machine.freeze

  machine
end

.register_state(state) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/state_machina/registry.rb', line 30

def self.register_state(state)
  states[state.model_name] ||= {}
  states[state.model_name][state.machine_name] ||= {}
  states[state.model_name][state.machine_name][state.name] = state.freeze

  state
end

.register_transition(transition) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/state_machina/registry.rb', line 62

def self.register_transition(transition)
  transitions[transition.model_name] ||= {}
  transitions[transition.model_name][transition.machine_name] ||= {}
  transitions[transition.model_name][transition.machine_name][transition.event_name] ||= []
  transitions[transition.model_name][transition.machine_name][transition.event_name].append(transition.freeze)

  transition
end

.statesObject



22
23
24
# File 'lib/state_machina/registry.rb', line 22

def self.states
  @states ||= Concurrent::Map.new
end

.transitionsObject



54
55
56
# File 'lib/state_machina/registry.rb', line 54

def self.transitions
  @transitions ||= Concurrent::Map.new
end