Class List Method List File List

# Class: TrafficLight

Inherits:
  Object
  - Object
  - TrafficLight
  show all
Defined in:
  traffic_light.rb

## State Machines

This class contains 1 state machine(s).

### state

## Instance Attribute Summary (collapse)

- - (Object) state

  Gets the current attribute value for the machine.

## Class Method Summary (collapse)

- + (String) human_state_event_name(event)

  Gets the humanized name for the given event.

- + (String) human_state_name(state)

  Gets the humanized name for the given state.

## Instance Method Summary (collapse)

- - (Boolean) can_cycle?(requirements = {})

  Checks whether :cycle can be fired.

- - (Boolean) caution?

  Checks whether :caution is the current state.

- - (Boolean) cycle(*args)

  Fires the :cycle event.

- - (Boolean) cycle!(*args)

  Fires the :cycle event, raising an exception if it fails.

- - (StateMachine::Transition) cycle_transition(requirements = {})

  Gets the next transition that would be performed if :cycle were to be fired.

- - (Boolean) fire_state_event(event, *args)

  Fires an arbitrary state event with the given argument list.

- - (String) human_state_name

  Gets the human-readable name of the state for the current value.

- - (Boolean) proceed?

  Checks whether :proceed is the current state.

- - (Boolean) state?(state_name)

  Checks the given state name against the current state.

- - (Array<Symbol>) state_events(requirements = {})

  Gets the list of events that can be fired on the current state (uses the unqualified event names).

- - (Symbol) state_name

  Gets the internal name of the state for the current value.

- - (StateMachine::PathCollection) state_paths(requirements = {})

  Gets the list of sequences of transitions that can be run for the current state.

- - (Array<StateMachine::Transition>) state_transitions(requirements = {})

  Gets the list of transitions that can be made for the current state.

- - (Boolean) stop?

  Checks whether :stop is the current state.

## Instance Attribute Details

- (`Object`) state

Gets the current attribute value for the machine

### Returns:

- The attribute value

    4
    5
    6
    7
    8
    # File 'traffic_light.rb', line 4

    state_machine :initial => :stop do
      event :cycle do
        transition :stop => :proceed, :proceed => :caution, :caution => :stop
      end
    end

## Class Method Details

+ (`String`) human_state_event_name(event)

Gets the humanized name for the given event.

### Parameters:

- event (`Symbol`) —

  The event to look up

### Returns:

- (`String`) —

  The human event name

    4
    5
    6
    7
    8
    # File 'traffic_light.rb', line 4

    state_machine :initial => :stop do
      event :cycle do
        transition :stop => :proceed, :proceed => :caution, :caution => :stop
      end
    end

+ (`String`) human_state_name(state)

Gets the humanized name for the given state.

### Parameters:

- state (`Symbol`) —

  The state to look up

### Returns:

- (`String`) —

  The human state name

    4
    5
    6
    7
    8
    # File 'traffic_light.rb', line 4

    state_machine :initial => :stop do
      event :cycle do
        transition :stop => :proceed, :proceed => :caution, :caution => :stop
      end
    end

## Instance Method Details

- (`Boolean`) can_cycle?(requirements = {})

Checks whether :cycle can be fired.

### Parameters:

- requirements (`Hash`) (defaults to: `{}`) —

  The transition requirements to test against

### Options Hash (`requirements`):

- :from (`Symbol`) — default: `the current state` —

  One or more initial states

- :to (`Symbol`) —

  One or more target states

- :guard (`Boolean`) —

  Whether to guard transitions with conditionals

### Returns:

- (`Boolean`) —

  `true` if :cycle can be fired, otherwise `false`

    4
    5
    6
    7
    8
    # File 'traffic_light.rb', line 4

    state_machine :initial => :stop do
      event :cycle do
        transition :stop => :proceed, :proceed => :caution, :caution => :stop
      end
    end

- (`Boolean`) caution?

Checks whether :caution is the current state.

### Returns:

- (`Boolean`) —

  `true` if this is the current state, otherwise `false`

    4
    5
    6
    7
    8
    # File 'traffic_light.rb', line 4

    state_machine :initial => :stop do
      event :cycle do
        transition :stop => :proceed, :proceed => :caution, :caution => :stop
      end
    end

- (`Boolean`) cycle(*args)

Fires the :cycle event.

### Parameters:

- args (`Array`) —

  Optional arguments to include in transition callbacks

### Returns:

- (`Boolean`) —

  `true` if the transition succeeds, otherwise `false`

    4
    5
    6
    7
    8
    # File 'traffic_light.rb', line 4

    state_machine :initial => :stop do
      event :cycle do
        transition :stop => :proceed, :proceed => :caution, :caution => :stop
      end
    end

- (`Boolean`) cycle!(*args)

Fires the :cycle event, raising an exception if it fails.

### Parameters:

- args (`Array`) —

  Optional arguments to include in transition callbacks

### Returns:

- (`Boolean`) —

  `true` if the transition succeeds

### Raises:

- (`StateMachine::InvalidTransition`) —

  If the transition fails

    4
    5
    6
    7
    8
    # File 'traffic_light.rb', line 4

    state_machine :initial => :stop do
      event :cycle do
        transition :stop => :proceed, :proceed => :caution, :caution => :stop
      end
    end

- (`StateMachine::Transition`) cycle_transition(requirements = {})

Gets the next transition that would be performed if :cycle were to be fired.

### Parameters:

- requirements (`Hash`) (defaults to: `{}`) —

  The transition requirements to test against

### Options Hash (`requirements`):

- :from (`Symbol`) — default: `the current state` —

  One or more initial states

- :to (`Symbol`) —

  One or more target states

- :guard (`Boolean`) —

  Whether to guard transitions with conditionals

### Returns:

- (`StateMachine::Transition`) —

  The transition that would be performed or `nil`

    4
    5
    6
    7
    8
    # File 'traffic_light.rb', line 4

    state_machine :initial => :stop do
      event :cycle do
        transition :stop => :proceed, :proceed => :caution, :caution => :stop
      end
    end

- (`Boolean`) fire_state_event(event, *args)

Fires an arbitrary state event with the given argument list

### Parameters:

- event (`Symbol`) —

  The name of the event to fire

- args —

  Optional arguments to include in the transition

### Returns:

- (`Boolean`) —

  `true` if the event succeeds, otherwise `false`

    4
    5
    6
    7
    8
    # File 'traffic_light.rb', line 4

    state_machine :initial => :stop do
      event :cycle do
        transition :stop => :proceed, :proceed => :caution, :caution => :stop
      end
    end

- (`String`) human_state_name

Gets the human-readable name of the state for the current value.

### Returns:

- (`String`) —

  The human-readable state name

    4
    5
    6
    7
    8
    # File 'traffic_light.rb', line 4

    state_machine :initial => :stop do
      event :cycle do
        transition :stop => :proceed, :proceed => :caution, :caution => :stop
      end
    end

- (`Boolean`) proceed?

Checks whether :proceed is the current state.

### Returns:

- (`Boolean`) —

  `true` if this is the current state, otherwise `false`

    4
    5
    6
    7
    8
    # File 'traffic_light.rb', line 4

    state_machine :initial => :stop do
      event :cycle do
        transition :stop => :proceed, :proceed => :caution, :caution => :stop
      end
    end

- (`Boolean`) state?(state_name)

Checks the given state name against the current state.

### Parameters:

- state_name (`Symbol`) —

  The name of the state to check

### Returns:

- (`Boolean`) —

  True if they are the same state, otherwise false

### Raises:

- (`IndexError`) —

  If the state name is invalid

    4
    5
    6
    7
    8
    # File 'traffic_light.rb', line 4

    state_machine :initial => :stop do
      event :cycle do
        transition :stop => :proceed, :proceed => :caution, :caution => :stop
      end
    end

- (`Array<Symbol>`) state_events(requirements = {})

Gets the list of events that can be fired on the current state (uses the unqualified event names)

### Parameters:

- requirements (`Hash`) (defaults to: `{}`) —

  The transition requirements to test against

### Options Hash (`requirements`):

- :from (`Symbol`) — default: `the current state` —

  One or more initial states

- :to (`Symbol`) —

  One or more target states

- :on (`Symbol`) —

  One or more events that fire the transition

- :guard (`Boolean`) —

  Whether to guard transitions with conditionals

### Returns:

- (`Array<Symbol>`) —

  The list of event names

    4
    5
    6
    7
    8
    # File 'traffic_light.rb', line 4

    state_machine :initial => :stop do
      event :cycle do
        transition :stop => :proceed, :proceed => :caution, :caution => :stop
      end
    end

- (`Symbol`) state_name

Gets the internal name of the state for the current value.

### Returns:

- (`Symbol`) —

  The internal name of the state

    4
    5
    6
    7
    8
    # File 'traffic_light.rb', line 4

    state_machine :initial => :stop do
      event :cycle do
        transition :stop => :proceed, :proceed => :caution, :caution => :stop
      end
    end

- (`StateMachine::PathCollection`) state_paths(requirements = {})

Gets the list of sequences of transitions that can be run for the current state

### Parameters:

- requirements (`Hash`) (defaults to: `{}`) —

  The transition requirements to test against

### Options Hash (`requirements`):

- :from (`Symbol`) — default: `the current state` —

  The initial state

- :to (`Symbol`) —

  The target state

- :deep (`Boolean`) —

  Whether to enable deep searches for the target state

- :guard (`Boolean`) —

  Whether to guard transitions with conditionals

### Returns:

- (`StateMachine::PathCollection`) —

  The collection of paths

    4
    5
    6
    7
    8
    # File 'traffic_light.rb', line 4

    state_machine :initial => :stop do
      event :cycle do
        transition :stop => :proceed, :proceed => :caution, :caution => :stop
      end
    end

- (`Array<StateMachine::Transition>`) state_transitions(requirements = {})

Gets the list of transitions that can be made for the current state

### Parameters:

- requirements (`Hash`) (defaults to: `{}`) —

  The transition requirements to test against

### Options Hash (`requirements`):

- :from (`Symbol`) — default: `the current state` —

  One or more initial states

- :to (`Symbol`) —

  One or more target states

- :on (`Symbol`) —

  One or more events that fire the transition

- :guard (`Boolean`) —

  Whether to guard transitions with conditionals

### Returns:

- (`Array<StateMachine::Transition>`) —

  The available transitions

    4
    5
    6
    7
    8
    # File 'traffic_light.rb', line 4

    state_machine :initial => :stop do
      event :cycle do
        transition :stop => :proceed, :proceed => :caution, :caution => :stop
      end
    end

- (`Boolean`) stop?

Checks whether :stop is the current state.

### Returns:

- (`Boolean`) —

  `true` if this is the current state, otherwise `false`

    4
    5
    6
    7
    8
    # File 'traffic_light.rb', line 4

    state_machine :initial => :stop do
      event :cycle do
        transition :stop => :proceed, :proceed => :caution, :caution => :stop
      end
    end