Class: Mocha::StateMachine — Mocha 1.15.0

# 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) ⇒ StatePredicate, State

  Provides mechanisms to (a) determine whether the StateMachine is in a given state; or (b) to change the StateMachine into the given state.

- #is_not(unexpected_state_name) ⇒ StatePredicate

  Provides a mechanism to determine whether the StateMachine is not in the state specified by `unexpected_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

    58
    59
    60
    # File 'lib/mocha/state_machine.rb', line 58

    def become(next_state_name)
      @current_state = next_state_name
    end

### #is(expected_state_name) ⇒ `StatePredicate` #is(desired_state_name) ⇒ `State`

Provides mechanisms to (a) determine whether the Mocha::StateMachine is in a given state; or (b) to change the Mocha::StateMachine into the given state.

Overloads:

- #is(expected_state_name) ⇒ `StatePredicate`

  Provides a mechanism to determine whether the Mocha::StateMachine is in the state specified by `expected_state_name` at some point in the future

  Parameters:

  - expected_state_name (`String`) —

    name of expected state.

  Returns:

  - (`StatePredicate`) —

    state predicate which, when queried, will indicate whether the Mocha::StateMachine is in the state specified by `expected_state_name`

- #is(desired_state_name) ⇒ `State`

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

  Parameters:

  - desired_state_name (`String`) —

    name of desired new state.

  Returns:

  - (`State`) —

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

Parameters:

- state_name (`String`) —

  name of expected/desired state.

Returns:

- (`StatePredicate`, `State`) —

  (a) state predicate which, when queried, will indicate whether the Mocha::StateMachine is in the given state; or (b) state which, when activated, will change the Mocha::StateMachine into the given state.

    76
    77
    78
    # File 'lib/mocha/state_machine.rb', line 76

    def is(state_name)
      State.new(self, state_name, 'is') { |current, given| current == given }
    end

### #is_not(unexpected_state_name) ⇒ `StatePredicate`

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

Parameters:

- unexpected_state_name (`String`) —

  name of unexpected state.

Returns:

- (`StatePredicate`) —

  state predicate which, when queried, will indicate whether the Mocha::StateMachine is not in the state specified by `unexpected_state_name`.

    84
    85
    86
    # File 'lib/mocha/state_machine.rb', line 84

    def is_not(unexpected_state_name) # rubocop:disable Naming/PredicateName
      StatePredicate.new(self, unexpected_state_name, 'is not') { |current, given| current != given }
    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.

    50
    51
    52
    53
    # File 'lib/mocha/state_machine.rb', line 50

    def starts_as(initial_state_name)
      become(initial_state_name)
      self
    end