Class: Mocha::StateMachine

Inherits:
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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ StateMachine

Returns a new instance of StateMachine


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

def initialize(name)
  @name = name
  @current_state = nil
end

Instance Attribute Details

#current_stateObject


56
57
58
# File 'lib/mocha/state_machine.rb', line 56

def current_state
  @current_state
end

#nameObject (readonly)


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

def name
  @name
end

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


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

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.


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

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.


91
92
93
# File 'lib/mocha/state_machine.rb', line 91

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

#mocha_inspectObject


96
97
98
99
100
101
102
# File 'lib/mocha/state_machine.rb', line 96

def mocha_inspect
  if @current_state
    "#{@name} is #{@current_state.mocha_inspect}"
  else
    "#{@name} has no current state"
  end
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:


68
69
70
71
# File 'lib/mocha/state_machine.rb', line 68

def starts_as(initial_state_name)
  become(initial_state_name)
  self
end