Class: StateJacket::Catalog
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- StateJacket::Catalog
- Defined in:
- lib/state_jacket/catalog.rb
Overview
A simple class that allows users to intuitively define states and transitions.
Instance Method Summary collapse
- #add(state) ⇒ Object
- #can_transition?(from_to) ⇒ Boolean
-
#initialize ⇒ Catalog
constructor
A new instance of Catalog.
- #lock ⇒ Object
- #supports_state?(state) ⇒ Boolean
- #terminator?(state) ⇒ Boolean
- #terminators ⇒ Object
- #transitioner?(state) ⇒ Boolean
- #transitioners ⇒ Object
Constructor Details
#initialize ⇒ Catalog
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
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 |
#lock ⇒ Object
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
59 60 61 |
# File 'lib/state_jacket/catalog.rb', line 59 def supports_state?(state) keys.include?(state.to_s) end |
#terminator?(state) ⇒ Boolean
45 46 47 |
# File 'lib/state_jacket/catalog.rb', line 45 def terminator?(state) terminators.include?(state.to_s) end |
#terminators ⇒ Object
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
35 36 37 |
# File 'lib/state_jacket/catalog.rb', line 35 def transitioner?(state) transitioners.include?(state.to_s) end |
#transitioners ⇒ Object
29 30 31 32 33 |
# File 'lib/state_jacket/catalog.rb', line 29 def transitioners keys.select do |state| self[state] != nil end end |