Class: MonkeyEngine::MonkeyService
- Inherits:
-
Object
- Object
- MonkeyEngine::MonkeyService
- Extended by:
- Forwardable
- Includes:
- Enumerable, Observable, Singleton
- Defined in:
- lib/MonkeyService/monkey_service.rb
Overview
The monkey service.
Provides the main interface for all functionality relating to the MonkeyEngine (see MonkeyEngine) and this gem. This class wraps MonkeyManager and MonkeyEngine classes.
Instance Method Summary collapse
-
#action_eval!(action) ⇒ Boolean
Evaluates the given action, to determine whether or not the action is completed (Action#action_completed?).
-
#add(monkey) ⇒ Object
Adds the Monkey to be managed.
-
#any_alive? ⇒ Boolean
Determines if any Monkeys that are being managed by the underlining MonkeyManager are alive.
-
#initialize ⇒ MonkeyService
constructor
A new instance of MonkeyService.
-
#kill!(monkey) ⇒ Object
Kills the monkey.
-
#kill_all! ⇒ Array
Kills all monkeys managed by the underlying MonkeyManager.
-
#monkey_do(action) ⇒ Boolean
Performs the action against the Monkey associated with this action.
- #new_action(monkey) ⇒ Object
Constructor Details
#initialize ⇒ MonkeyService
Returns a new instance of MonkeyService.
26 27 28 29 |
# File 'lib/MonkeyService/monkey_service.rb', line 26 def initialize @monkey_manager = MonkeyEngine::MonkeyManager.instance @engine = MonkeyEngine::Engine.instance end |
Instance Method Details
#action_eval!(action) ⇒ Boolean
Evaluates the given action, to determine whether or not the action is completed (Action#action_completed?).
123 124 125 |
# File 'lib/MonkeyService/monkey_service.rb', line 123 def action_eval!(action) @engine.action_eval! action end |
#add(monkey) ⇒ Object
Observers are notified after this operation in the form of these update method params: ObserverKlass#update(param1, param2, param3); where param1=Time of the operation, param2=Symbol representing the operation (in this case, :add), param3=The following Hash: [monkey] where monkey=The Monkey added.
Adds the Monkey to be managed.
40 41 42 43 |
# File 'lib/MonkeyService/monkey_service.rb', line 40 def add(monkey) @monkey_manager.add(monkey).start do_notify_observers(:add, { monkey: monkey }) end |
#any_alive? ⇒ Boolean
A Monkey is considered alive if the Monkey#thread.alive? is true.
Determines if any Monkeys that are being managed by the underlining MonkeyManager are alive.
52 53 54 55 56 57 58 59 60 |
# File 'lib/MonkeyService/monkey_service.rb', line 52 def any_alive? return false if @monkey_manager.count.zero? alive_count = 0 @monkey_manager.each { |monkey| alive_count += 1 if monkey.alive? } alive_count.positive? end |
#kill!(monkey) ⇒ Object
The Monkey#monkey_do method executed continually by Monkey#thread is terminated.
Observers are notified after this operation in the form of these update method params: ObserverKlass#update(param1, param2, param3); where param1=Time of the operation, param2=Symbol representing the operation (in this case, :kill!), param3=The following Hash: [monkey] where monkey=The Monkey killed.
Kills the monkey.
74 75 76 77 |
# File 'lib/MonkeyService/monkey_service.rb', line 74 def kill!(monkey) @monkey_manager.kill!(monkey) do_notify_observers(:kill!, { monkey: monkey }) end |
#kill_all! ⇒ Array
The Monkey#monkey_do method executed continually by Monkey#thread, for each Monkey is terminated.
Observers are notified after this operation in the form of these update method params: ObserverKlass#update(param1, param2, param3); where param1=Time of the operation, param2=Symbol representing the operation (in this case, :kill_all!), param3=nil.
Kills all monkeys managed by the underlying MonkeyManager.
90 91 92 93 94 |
# File 'lib/MonkeyService/monkey_service.rb', line 90 def kill_all! monkey_array = @monkey_manager.kill_all! do_notify_observers(:kill_all!, nil) monkey_array end |
#monkey_do(action) ⇒ Boolean
Observers are notified after this operation in the form of these update method params: ObserverKlass#update(param1, param2, param3); where param1=Time of the operation, param2=Symbol representing the operation (in this case, :action_complete), param3=The following Hash: [action] where action=The action that was completed.
Performs the action against the Monkey associated with this action.
112 113 114 |
# File 'lib/MonkeyService/monkey_service.rb', line 112 def monkey_do(action) do_notify_observers(:action_completed, { action: action }) if @engine.do_action action end |
#new_action(monkey) ⇒ Object
96 97 98 |
# File 'lib/MonkeyService/monkey_service.rb', line 96 def new_action(monkey) @engine.new_action monkey end |