Class: Monkey
- Inherits:
-
Object
- Object
- Monkey
- Includes:
- ProtectedConstructor
- Defined in:
- lib/Monkey/monkey.rb
Overview
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.
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.
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#monkey_symbol ⇒ Symbol
readonly
The Symbol that identifies this Monkey.
-
#thread ⇒ Thread
readonly
The thread that this Monkey uses to perform its actions.
Instance Method Summary collapse
-
#alive? ⇒ Boolean
Determines if the Monkey is alive.
-
#current_action ⇒ MonkeyAction
Retrieves the current Action that this Monkey is engaged in.
-
#kill ⇒ Object
Kills this Monkey.
-
#start ⇒ Monkey, self
Starts this Monkey.
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
27 |
# File 'lib/Monkey/monkey.rb', line 27 attr_reader :monkey_symbol, :action |
#monkey_symbol ⇒ Symbol (readonly)
Returns the Symbol that identifies this Monkey.
27 28 29 |
# File 'lib/Monkey/monkey.rb', line 27 def monkey_symbol @monkey_symbol end |
#thread ⇒ Thread (readonly)
The thread that this Monkey uses to perform its actions.
89 90 91 |
# File 'lib/Monkey/monkey.rb', line 89 def thread @thread end |
Instance Method Details
#alive? ⇒ Boolean
The Monkey is considered alive if the Monkey#thread.alive? is true.
Determines if the Monkey is alive.
44 45 46 47 48 |
# File 'lib/Monkey/monkey.rb', line 44 def alive? return false if @thread.nil? @thread.alive? end |
#current_action ⇒ MonkeyAction
Retrieves the current Action that this Monkey is engaged in.
54 55 56 57 58 |
# File 'lib/Monkey/monkey.rb', line 54 def current_action return MonkeyActionDead.new(self) unless alive? @action end |
#kill ⇒ Object
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 |
#start ⇒ Monkey, self
Starts this Monkey.
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 |