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

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



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

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.



63
64
65
# File 'lib/Monkey/monkey.rb', line 63

def kill
  @kill_thread = true
end

#startMonkey, self

Starts this Monkey.

Returns:

Raises:



73
74
75
76
77
78
# File 'lib/Monkey/monkey.rb', line 73

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

#threadThread

The thread that this Monkey uses to perform its actions.

Returns:

  • (Thread)


84
85
86
# File 'lib/Monkey/monkey.rb', line 84

def thread
  @thread
end