Class: AASM::StateMachineStore

Inherits:
Object
  • Object
show all
Defined in:
lib/aasm/state_machine_store.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStateMachineStore

Returns a new instance of StateMachineStore.


43
44
45
# File 'lib/aasm/state_machine_store.rb', line 43

def initialize
  @machines = Concurrent::Map.new
end

Class Method Details

.[]=Object

do not overwrite existing state machines, which could have been created by inheritance, see AASM::ClassMethods method inherited


25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/aasm/state_machine_store.rb', line 25

def register(klass, overwrite = false, state_machine = nil)
  raise "Cannot register #{klass}" unless klass.is_a?(Class)

  case name = template = overwrite
    when FalseClass then stores[klass.to_s] ||= new
    when TrueClass then stores[klass.to_s] = new
    when Class then stores[klass.to_s] = stores[template.to_s].clone
    when Symbol then stores[klass.to_s].register(name, state_machine)
    when String then stores[klass.to_s].register(name, state_machine)
    else raise "Don't know what to do with #{overwrite}"
  end
end

.fetch(klass, fallback = nil) ⇒ Object Also known as: []


27
28
29
30
31
32
33
34
35
# File 'lib/aasm/state_machine_store.rb', line 27

def fetch(klass, fallback = nil)
  stores[klass.to_s] || fallback && begin
    match = klass.ancestors.find do |ancestor|
      ancestor.include? AASM and stores[ancestor.to_s]
    end

    stores[match.to_s]
  end
end

.register(klass, overwrite = false, state_machine = nil) ⇒ Object

do not overwrite existing state machines, which could have been created by inheritance, see AASM::ClassMethods method inherited


13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/aasm/state_machine_store.rb', line 13

def register(klass, overwrite = false, state_machine = nil)
  raise "Cannot register #{klass}" unless klass.is_a?(Class)

  case name = template = overwrite
    when FalseClass then stores[klass.to_s] ||= new
    when TrueClass then stores[klass.to_s] = new
    when Class then stores[klass.to_s] = stores[template.to_s].clone
    when Symbol then stores[klass.to_s].register(name, state_machine)
    when String then stores[klass.to_s].register(name, state_machine)
    else raise "Don't know what to do with #{overwrite}"
  end
end

.storesObject


7
8
9
# File 'lib/aasm/state_machine_store.rb', line 7

def stores
  @stores
end

.unregister(klass) ⇒ Object


38
39
40
# File 'lib/aasm/state_machine_store.rb', line 38

def unregister(klass)
  stores.delete(klass.to_s)
end

Instance Method Details

#cloneObject


47
48
49
50
51
52
53
# File 'lib/aasm/state_machine_store.rb', line 47

def clone
  StateMachineStore.new.tap do |store|
    @machines.each_pair do |name, machine|
      store.register(name, machine.clone)
    end
  end
end

#machine(name) ⇒ Object Also known as: []


55
56
57
# File 'lib/aasm/state_machine_store.rb', line 55

def machine(name)
  @machines[name.to_s]
end

#machine_namesObject Also known as: keys


60
61
62
# File 'lib/aasm/state_machine_store.rb', line 60

def machine_names
  @machines.keys
end

#register(name, machine, force = false) ⇒ Object


65
66
67
68
69
70
71
72
73
74
# File 'lib/aasm/state_machine_store.rb', line 65

def register(name, machine, force = false)
  raise "Cannot use #{name.inspect} for machine name" unless name.is_a?(Symbol) or name.is_a?(String)
  raise "Cannot use #{machine.inspect} as a machine" unless machine.is_a?(AASM::StateMachine)

  if force
    @machines[name.to_s] = machine
  else
    @machines[name.to_s] ||= machine
  end
end