Class: FiniteMachine::EventsMap Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/finite_machine/events_map.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A class responsible for storing mappings between event namess and their transition objects.

Used internally by StateMachine.

Instance Method Summary collapse

Constructor Details

#initializeEventsMap

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a EventsMap



24
25
26
# File 'lib/finite_machine/events_map.rb', line 24

def initialize
  @events_map = Concurrent::Map.new
end

Instance Method Details

#add(name, transition) ⇒ nil

Add transition under name

Parameters:

  • the (Symbol)

    event name

  • transition (Transition)

    the transition to add under event name

Returns:

  • (nil)


54
55
56
57
58
59
60
61
# File 'lib/finite_machine/events_map.rb', line 54

def add(name, transition)
  if exists?(name)
    @events_map[name] << transition
  else
    @events_map[name] = [transition]
  end
  self
end

#can_perform?(name, from_state, *conditions) ⇒ Boolean

Check if event is valid and transition can be performed

Returns:

  • (Boolean)


131
132
133
# File 'lib/finite_machine/events_map.rb', line 131

def can_perform?(name, from_state, *conditions)
  !match_transition_with(name, from_state, *conditions).nil?
end

#choice_transition?(name, from_state) ⇒ Boolean

Check if event has branching choice transitions or not

Examples:

events_map.choice_transition?(:go, :green) # => true

Parameters:

  • name (Symbol)

    the event name

  • from_state (Symbol)

    the transition from state

Returns:

  • (Boolean)

    true if transition has any branches, false otherwise



150
151
152
# File 'lib/finite_machine/events_map.rb', line 150

def choice_transition?(name, from_state)
  find(name).select { |trans| trans.matches?(from_state) }.size > 1
end

#clearself

Reset map

Returns:

  • (self)


238
239
240
241
# File 'lib/finite_machine/events_map.rb', line 238

def clear
  @events_map.clear
  self
end

#eventsArray[Symbol]

Retrieve all event names

Examples:

events_map.events # => [:init, :start, :stop]

Returns:

  • (Array[Symbol])

    All event names



88
89
90
# File 'lib/finite_machine/events_map.rb', line 88

def events
  @events_map.keys
end

#exists?(name) ⇒ Boolean

Check if event is present

Examples:

events_map.exists?(:go) # => true

Parameters:

  • name (Symbol)

    the event name

Returns:

  • (Boolean)

    true if event is present, false otherwise



40
41
42
# File 'lib/finite_machine/events_map.rb', line 40

def exists?(name)
  @events_map.key?(name)
end

#find(name) ⇒ Array[Transition] Also known as: []

Finds transitions for the event name

Examples:

events_map[:start] # => []

Parameters:

  • name (Symbol)

Returns:

  • (Array[Transition])

    the transitions matching event name



74
75
76
# File 'lib/finite_machine/events_map.rb', line 74

def find(name)
  @events_map.fetch(name) { [] }
end

#inspectString

Inspect map content

Examples:

events_map.inspect

Returns:

  • (String)


264
265
266
# File 'lib/finite_machine/events_map.rb', line 264

def inspect
  "<##{self.class} @events_map=#{self}>"
end

#match_transition(name, from_state) ⇒ Transition?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find transition without checking conditions

Parameters:

  • name (Symbol)

    the event name

  • from_state (Symbol)

    the transition from state

Returns:

  • (Transition, nil)

    returns transition, nil otherwise



166
167
168
# File 'lib/finite_machine/events_map.rb', line 166

def match_transition(name, from_state)
  find(name).find { |trans| trans.matches?(from_state) }
end

#match_transition_with(name, from_state, *conditions) ⇒ Transition

Examine transitions for event name that start in from state and find one matching condition.

Parameters:

  • name (Symbol)

    the event name

  • from_state (Symbol)

    the current context from_state

Returns:

  • (Transition)

    The choice transition that matches



183
184
185
186
187
# File 'lib/finite_machine/events_map.rb', line 183

def match_transition_with(name, from_state, *conditions)
  find(name).find do |trans|
    trans.matches?(from_state) && trans.check_conditions(*conditions)
  end
end

#move_to(name, from_state, *conditions) ⇒ Symbol

Find state that this machine can move to

Examples:

evenst_map.move_to(:go, :green) # => :red

Parameters:

  • name (Symbol)

    the event name

  • from_state (Symbol)

    the transition from state

  • conditions (Array)

    the data associated with this transition

Returns:

  • (Symbol)

    the transition ‘to` state



227
228
229
230
231
# File 'lib/finite_machine/events_map.rb', line 227

def move_to(name, from_state, *conditions)
  transition = select_transition(name, from_state, *conditions)
  transition ||= UndefinedTransition.new(name)
  transition.to_state(from_state)
end

#select_transition(name, from_state, *conditions) ⇒ Transition

Select transition that matches conditions

Parameters:

  • name (Symbol)

    the event name

  • from_state (Symbol)

    the transition from state

  • conditions (Array[Object])

    the conditional data

Returns:



201
202
203
204
205
206
207
# File 'lib/finite_machine/events_map.rb', line 201

def select_transition(name, from_state, *conditions)
  if choice_transition?(name, from_state)
    match_transition_with(name, from_state, *conditions)
  else
    match_transition(name, from_state)
  end
end

#state_transitionsArray[Hash]

Retrieves all state transitions

Returns:

  • (Array[Hash])


110
111
112
# File 'lib/finite_machine/events_map.rb', line 110

def state_transitions
  @events_map.values.flatten.map(&:states)
end

#statesArray[Symbol]

Retreive all unique states

Examples:

events_map.states # => [:yellow, :green, :red]

Returns:

  • (Array[Symbol])

    the array of all unique states



101
102
103
# File 'lib/finite_machine/events_map.rb', line 101

def states
  @events_map.values.flatten.map(&:states).map(&:to_a).flatten.uniq
end

#states_for(name) ⇒ Object

Retrieve from states for the event name

Examples:

events_map.states_for(:start) # => [:yellow, :green]

Parameters:

  • event_name (Symbol)


122
123
124
# File 'lib/finite_machine/events_map.rb', line 122

def states_for(name)
  find(name).map(&:states).flat_map(&:keys)
end

#to_sString

Return string representation of this map

Returns:

  • (String)


248
249
250
251
252
253
254
# File 'lib/finite_machine/events_map.rb', line 248

def to_s
  hash = {}
  @events_map.each_pair do |name, trans|
    hash[name] = trans
  end
  hash.to_s
end