Class: MonkeyEngine::MonkeyService

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeMonkeyService

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?).

Parameters:

Returns:

  • (Boolean)

    true if the action is completed (Action#action_completed?), false otherwise.



123
124
125
# File 'lib/MonkeyService/monkey_service.rb', line 123

def action_eval!(action)
  @engine.action_eval! action
end

#add(monkey) ⇒ Object

Note:

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.

Parameters:

  • monkey (Monkey, #read)

    the Monkey to add.



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

Note:

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.

Returns:

  • (Boolean)

    true if any Monkeys are alive, false otherwise.



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

Note:

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

Note:

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.

Parameters:

  • monkey (Monkey, #read/#write)

    the Monkey to kill.



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

Note:

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

Note:

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.

Returns:

  • (Array)

    an Array of Monkey objects that were killed.



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

Note:

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.

Parameters:

Returns:

  • (Boolean)

    true if the action performed has been completed (Action#action_completed?), false otherwise.



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