# Class: Mocha::StateMachine

Inherits:
  Object
  - Object
  - Mocha::StateMachine
  show all

Defined in:
  lib/mocha/state_machine.rb

## Overview

A state machine that is used to constrain the order of invocations. An invocation can be constrained to occur when a state #is, or #is_not, active.

## Defined Under Namespace

Classes: State, StatePredicate

## Instance Method Summary collapse

- #become(next_state_name) ⇒ Object

  Put the StateMachine into the `next_state_name`.

- #is(state_name) ⇒ State

  Provides a mechanism to change the StateMachine into the state specified by `state_name` at some point in the future.

- #is_not(state_name) ⇒ Object

  Provides a mechanism to determine whether the StateMachine is not in the state specified by `state_name` at some point in the future.

- #starts_as(initial_state_name) ⇒ StateMachine

  Put the StateMachine into the state specified by `initial_state_name`.

## Instance Method Details

### #become(next_state_name) ⇒ `Object`

Put the Mocha::StateMachine into the `next_state_name`.

Parameters:

- next_state_name (`String`) —

  name of new state

    72
    73
    74

    # File 'lib/mocha/state_machine.rb', line 72

    def become(next_state_name)
      @current_state = next_state_name
    end

### #is(state_name) ⇒ `State`

Provides a mechanism to change the Mocha::StateMachine into the state specified by `state_name` at some point in the future.

Or provides a mechanism to determine whether the Mocha::StateMachine is in the state specified by `state_name` at some point in the future.

Parameters:

- state_name (`String`) —

  name of new state

Returns:

- (`State`) —

  state which, when activated, will change the Mocha::StateMachine into the state with the specified `state_name`.

    82
    83
    84

    # File 'lib/mocha/state_machine.rb', line 82

    def is(state_name)
      State.new(self, state_name)
    end

### #is_not(state_name) ⇒ `Object`

Provides a mechanism to determine whether the Mocha::StateMachine is not in the state specified by `state_name` at some point in the future. rubocop:disable Naming/PredicateName

    88
    89
    90

    # File 'lib/mocha/state_machine.rb', line 88

    def is_not(state_name)
      StatePredicate.new(self, state_name)
    end

### #starts_as(initial_state_name) ⇒ `StateMachine`

Put the Mocha::StateMachine into the state specified by `initial_state_name`.

Parameters:

- initial_state_name (`String`) —

  name of initial state

Returns:

- (`StateMachine`) —

  state machine, thereby allowing invocations of other Mocha::StateMachine methods to be chained.

    64
    65
    66
    67

    # File 'lib/mocha/state_machine.rb', line 64

    def starts_as(initial_state_name)
      become(initial_state_name)
      self
    end