Class: Mocha::StateMachine

Inherits:
Object
  • Object
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

Instance Method Details

#become(next_state_name) ⇒ Object

Put the Mocha::StateMachine into the next_state_name.

Parameters:

  • name of new state



60
61
62
# File 'lib/mocha/state_machine.rb', line 60

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:

    • name of expected state.

    Returns:

    • 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:

    • name of desired new state.

    Returns:

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

Parameters:

  • name of expected/desired state.

Returns:

  • (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.



78
79
80
# File 'lib/mocha/state_machine.rb', line 78

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:

  • name of unexpected state.

Returns:

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



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

def is_not(unexpected_state_name) # rubocop:disable Naming/PredicatePrefix
  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:

  • name of initial state

Returns:

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



52
53
54
55
# File 'lib/mocha/state_machine.rb', line 52

def starts_as(initial_state_name)
  become(initial_state_name)
  self
end