Class: StateJacket::Catalog

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/state_jacket/catalog.rb

Overview

A simple class that allows users to intuitively define states and transitions.

Instance Method Summary collapse

Constructor Details

#initializeCatalog

Returns a new instance of Catalog.



7
8
9
10
# File 'lib/state_jacket/catalog.rb', line 7

def initialize
  @inner_hash = {}
  super inner_hash
end

Instance Method Details

#add(state) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/state_jacket/catalog.rb', line 12

def add(state)
  if state.is_a?(Hash)
    self[state.keys.first.to_s] = state.values.first.map(&:to_s)
  else
    self[state.to_s] = nil
  end
end

#can_transition?(from_to) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
# File 'lib/state_jacket/catalog.rb', line 20

def can_transition?(from_to)
  from = from_to.keys.first.to_s
  to = from_to.values.first
  to = [to] unless to.is_a?(Array)
  to = to.map(&:to_s)
  transitions = self[from] || []
  (to & transitions).length == to.length
end

#lockObject



49
50
51
52
53
54
55
56
57
# File 'lib/state_jacket/catalog.rb', line 49

def lock
  values.flatten.each do |value|
    next if value.nil?
    if !keys.include?(value)
      raise "Invalid StateJacket::Catalog! [#{value}] is not a first class state."
    end
  end
  inner_hash.freeze
end

#supports_state?(state) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/state_jacket/catalog.rb', line 59

def supports_state?(state)
  keys.include?(state.to_s)
end

#terminator?(state) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/state_jacket/catalog.rb', line 45

def terminator?(state)
  terminators.include?(state.to_s)
end

#terminatorsObject



39
40
41
42
43
# File 'lib/state_jacket/catalog.rb', line 39

def terminators
  keys.select do |state|
    self[state] == nil
  end
end

#transitioner?(state) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/state_jacket/catalog.rb', line 35

def transitioner?(state)
  transitioners.include?(state.to_s)
end

#transitionersObject



29
30
31
32
33
# File 'lib/state_jacket/catalog.rb', line 29

def transitioners
  keys.select do |state|
    self[state] != nil
  end
end