class EndState::TransitionConfigurationSet

Attributes

configuration_map[R]
end_state_map[R]

Public Class Methods

new() click to toggle source
# File lib/end_state/transition_configuration_set.rb, line 4
def initialize
  @end_state_map = { any_state: {} }      # [start_state][event] = end_state
  @configuration_map = { any_state: {} }  # [start_state][end_state] = configuration
end

Public Instance Methods

add(start_state, end_state, configuration, event = nil) click to toggle source
# File lib/end_state/transition_configuration_set.rb, line 9
def add(start_state, end_state, configuration, event = nil)
  if event
    end_state_map[start_state] ||= {}
    end_state_map[start_state][event] = end_state
  end

  configuration_map[start_state] ||= {}
  configuration_map[start_state][end_state] = configuration
end
each(&block) click to toggle source
# File lib/end_state/transition_configuration_set.rb, line 48
def each &block
  all_transitions.each(&block)
end
end_states() click to toggle source
# File lib/end_state/transition_configuration_set.rb, line 36
def end_states
  configuration_map.map { |_, v| v.keys }.flatten.uniq
end
event_conflicts?(start_state, event) click to toggle source
# File lib/end_state/transition_configuration_set.rb, line 44
def event_conflicts?(start_state, event)
  !!get_end_state(start_state, event) || (start_state == :any_state && events.include?(event))
end
events() click to toggle source
# File lib/end_state/transition_configuration_set.rb, line 40
def events
  end_state_map.map { |_, v| v.keys }.flatten.uniq
end
get_configuration(start_state, end_state) click to toggle source
# File lib/end_state/transition_configuration_set.rb, line 19
def get_configuration(start_state, end_state)
  local_map = configuration_map[start_state] || {}
  local_map[end_state] || configuration_map[:any_state][end_state]
end
get_end_state(start_state, event) click to toggle source
# File lib/end_state/transition_configuration_set.rb, line 24
def get_end_state(start_state, event)
  local_map = end_state_map[start_state] || {}
  local_map[event] || end_state_map[:any_state][event]
end
start_states() click to toggle source
# File lib/end_state/transition_configuration_set.rb, line 29
def start_states
  states = configuration_map.keys
  states.delete(:any_state)
  states += end_states unless configuration_map[:any_state].empty?
  states.uniq
end

Private Instance Methods

all_transitions() click to toggle source
# File lib/end_state/transition_configuration_set.rb, line 56
def all_transitions
  all_start_states = start_states

  configuration_map.map do |start_state, local_config|
    states = (start_state == :any_state) ? all_start_states : [start_state]
    states.map { |s| transitions_for s, local_config }
  end.flatten(2)
end
event_for(start_state, end_state) click to toggle source
# File lib/end_state/transition_configuration_set.rb, line 71
def event_for start_state, end_state
  (end_state_map[start_state] || {}).each do |k, v|
    return k if v == end_state
  end

  end_state_map[:any_state].each do |k, v|
    return k if v == end_state
  end

  nil
end
transitions_for(start_state, local_map) click to toggle source
# File lib/end_state/transition_configuration_set.rb, line 65
def transitions_for start_state, local_map
  local_map.map do |end_state, config|
    [start_state, end_state, config, event_for(start_state, end_state)]
  end
end