Class: Monkey

Inherits:
Object
  • Object
show all
Includes:
ProtectedConstructor
Defined in:
lib/Monkey/monkey.rb

Overview

Note:

The Monkey class represents a virtual monkey that performs particular actions that may or may not be typical of a real monkey. Monkey actions are derived from class MonkeyAction.

Note:

The constructor for this class is protected; to instantiate a managed Monkey, use MonkeyEngine::MonkeyService#add; to instantiate an unmanaged Monkey, use MonkeyFactory::create.

Monkey class.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



27
# File 'lib/Monkey/monkey.rb', line 27

attr_reader :monkey_symbol, :action

#monkey_symbolSymbol (readonly)

Returns the Symbol that identifies this Monkey.

Returns:

  • (Symbol)

    the Symbol that identifies this Monkey.



27
28
29
# File 'lib/Monkey/monkey.rb', line 27

def monkey_symbol
  @monkey_symbol
end

#threadThread (readonly)

The thread that this Monkey uses to perform its actions.

Returns:

  • (Thread)


89
90
91
# File 'lib/Monkey/monkey.rb', line 89

def thread
  @thread
end

Instance Method Details

#alive?Boolean

Note:

The Monkey is considered alive if the Monkey#thread.alive? is true.

Determines if the Monkey is alive.

Returns:

  • (Boolean)

    true if the Monkey is alive, false otherwise.



44
45
46
47
48
# File 'lib/Monkey/monkey.rb', line 44

def alive?
  return false if @thread.nil?

  @thread.alive?
end

#current_actionMonkeyAction

Retrieves the current Action that this Monkey is engaged in.

Returns:



54
55
56
57
58
# File 'lib/Monkey/monkey.rb', line 54

def current_action
  return MonkeyActionDead.new(self) unless alive?

  @action
end

#killObject

Note:

The Monkey#monkey_do method executed continually by Monkey#thread is terminated.

Kills this Monkey.



65
66
67
# File 'lib/Monkey/monkey.rb', line 65

def kill
  @kill_thread = true
end

#startMonkey, self

Starts this Monkey.

Returns:

Raises:



75
76
77
78
79
80
81
82
83
# File 'lib/Monkey/monkey.rb', line 75

def start
  if alive?
    raise MonkeyEngine::Exceptions::InvalidOperationException,
      "The monkey [#{@monkey_symbol}] thread is already started"
  end

  initialize_thread
  self
end