Module: QueuedStateMachine::ClassMethods
- Defined in:
- lib/queued_state_machine.rb
Instance Attribute Summary collapse
-
#initial_state ⇒ Object
Returns the value of attribute initial_state.
Instance Method Summary collapse
- #callbacks ⇒ Object
- #clean_state(name) ⇒ Object
- #clean_state_list(arg) ⇒ Object
- #inherited(subclass) ⇒ Object
- #on_transition(from: nil, to: nil, &block) ⇒ Object
- #state(name, initial: false) ⇒ Object
- #states ⇒ Object
- #transition(from: nil, to: nil) ⇒ Object
- #transitions ⇒ Object
Instance Attribute Details
#initial_state ⇒ Object
Returns the value of attribute initial_state.
11 12 13 |
# File 'lib/queued_state_machine.rb', line 11 def initial_state @initial_state end |
Instance Method Details
#callbacks ⇒ Object
28 29 30 |
# File 'lib/queued_state_machine.rb', line 28 def callbacks @callbacks ||= [] end |
#clean_state(name) ⇒ Object
70 71 72 |
# File 'lib/queued_state_machine.rb', line 70 def clean_state(name) name.to_s end |
#clean_state_list(arg) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/queued_state_machine.rb', line 74 def clean_state_list(arg) case when arg == nil states when arg.respond_to?(:to_a) list = arg list.map! {|name| clean_state(name) } list else name = arg [clean_state(name)] end end |
#inherited(subclass) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/queued_state_machine.rb', line 13 def inherited(subclass) subclass.instance_variable_set("@states", instance_variable_get("@states").dup) subclass.instance_variable_set("@initial_state", instance_variable_get("@initial_state")) subclass.instance_variable_set("@transitions", instance_variable_get("@transitions").dup) subclass.instance_variable_set("@callbacks", instance_variable_get("@callbacks").dup) end |
#on_transition(from: nil, to: nil, &block) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/queued_state_machine.rb', line 56 def on_transition(from: nil, to: nil, &block) unless block_given? 'Error: no callback defined' end from = clean_state_list(from) to = clean_state_list(to) missing_state = (from + to).find { |state| !states.include?(state) } if missing_state raise "Error: state #{missing_state} not defined" end callbacks << {from: from, to: to, callback: block} nil end |
#state(name, initial: false) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/queued_state_machine.rb', line 32 def state(name, initial: false) unless transitions.empty? && callbacks.empty? raise 'Error: all states must be defined before any transitions of callbacks' end name = clean_state(name) self.initial_state = name if initial states << name nil end |
#states ⇒ Object
20 21 22 |
# File 'lib/queued_state_machine.rb', line 20 def states @states ||= [] end |
#transition(from: nil, to: nil) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/queued_state_machine.rb', line 42 def transition(from: nil, to: nil) if block_given? raise "Error: transitions do not accept blocks, use 'on_transition'" end from = clean_state_list(from) to = clean_state_list(to) missing_state = (from + to).find { |state| !states.include?(state) } if missing_state raise "Error: state #{missing_state} not defined" end transitions << {from: from, to: to} nil end |
#transitions ⇒ Object
24 25 26 |
# File 'lib/queued_state_machine.rb', line 24 def transitions @transitions ||= [] end |