class Fsm::Machine
Attributes
error[RW]
from[RW]
schema[RW]
to[RW]
Public Class Methods
new(schema:, changeset:)
click to toggle source
# File lib/fsm/machine.rb, line 5 def initialize(schema:, changeset:) raise ArgumentError.new("changeset argument must be an array with only 2 elements") unless (changeset.is_a?(Array) && changeset.length == 2) raise ArgumentError.new("schema is invalid") unless valid_schema?(schema) @schema = schema @from, @to = changeset end
Public Instance Methods
transition()
click to toggle source
# File lib/fsm/machine.rb, line 12 def transition found_transition = schema.dup.select do |mapping| mapping[:from].include?(from.to_sym) && mapping[:to] == to.to_sym end if found_transition.length > 1 @error = TransitionError.new(self) @error.message = "Found an ambiguous number of transitions that can ocurr: #{found_transition.map { |x| x[:call] }}" false elsif found_transition.length < 1 @error = TransitionError.new(self) @error.message = "Could not find a transition from #{from} to #{to}" false else found_transition.pop[:call] end end
transition!()
click to toggle source
# File lib/fsm/machine.rb, line 30 def transition! transition || raise(error) end
valid_schema?(schema)
click to toggle source
# File lib/fsm/machine.rb, line 34 def valid_schema?(schema) return false unless schema.is_a?(Array) schema.all? do |mapping| mapping.keys == [:from, :to, :call] && mapping[:from].is_a?(Array) && mapping[:to].is_a?(Symbol) && mapping[:call].is_a?(Symbol) end end