lib/MonkeyManager/monkey_manager.rb in MonkeyEngine-1.1.0 vs lib/MonkeyManager/monkey_manager.rb in MonkeyEngine-2.0.0
- old
+ new
@@ -1,10 +1,11 @@
+# frozen_string_literal: true
+
require 'singleton'
require 'forwardable'
module MonkeyEngine
-
# The MonkeyManager.
#
# This class provides the functionality needed to manage Monkeys (See Monkey, MonkeyService)
#
class MonkeyManager
@@ -16,27 +17,27 @@
include Singleton
# The constructor
#
def initialize
- @monkeys = Array.new
+ @monkeys = []
end
- public
-
# Adds the Monkey to the list of Monkeys to be managed.
#
# @param (Monkey, #read) monkey The Monkey to add.
#
# @return [Monkey] the Monkey added.
#
# @raise [MonkeyEngine::Exceptions::InvalidArgumentTypeException] if parameter monkey is not a Monkey object.
# @raise [MonkeyEngine::Exceptions::UniqueObjectException] if monkey already exists.
#
def add(monkey)
- raise MonkeyEngine::Exceptions::InvalidArgumentTypeException.new "Parameter 'monkey' is not Monkey object" unless monkey.is_a? Monkey
- raise MonkeyEngine::Exceptions::UniqueObjectException.new "Monkeys must be unique" if exists? monkey
+ unless monkey.is_a? Monkey
+ raise MonkeyEngine::Exceptions::InvalidArgumentTypeException, "Parameter 'monkey' is not Monkey object"
+ end
+ raise MonkeyEngine::Exceptions::UniqueObjectException, 'Monkeys must be unique' if exists? monkey
@monkeys.push(monkey)
monkey
end
@@ -64,11 +65,11 @@
#
# @return [Boolean] true if the Monkey is currently active (Monkey#.alive?), false otherwise.
#
def alive?(monkey)
monkey = get(monkey)
- monkey.alive? unless monkey.nil?
+ monkey&.alive?
end
# Returns the Monkey indicated by *monkey*.
#
# @param [Monkey, Symbol, String, #read] monkey The Monkey to return.
@@ -86,11 +87,12 @@
# TODO: This seems inefficient.
return @monkeys.select { |m| m.monkey_symbol == monkey }.first if monkey.is_a? Symbol
return @monkeys.select { |m| m.monkey_symbol == monkey.to_sym }.first if monkey.is_a? String
return @monkeys.select { |m| m.monkey_symbol == monkey.monkey_symbol }.first if monkey.is_a? Monkey
- raise MonkeyEngine::Exceptions::InvalidArgumentTypeException.new "Parameter 'monkey' is not a Symbol, String or Monkey object"
+ raise MonkeyEngine::Exceptions::InvalidArgumentTypeException,
+ "Parameter 'monkey' is not a Symbol, String or Monkey object"
end
# Returns a duplicate Array of Monkey objects managed by this MonkeyManager.
#
# @return [Array] An Array of Monkey objects managed by this MonkeyManager.
@@ -118,11 +120,11 @@
# wait indefinitely.
#
# @return [Array, nil] Returns an [Array] of Monkey objects, or, nil, if no Monkey objects are being managed by
# this MonkeyManager.
#
- def join_all(limit=nil)
+ def join_all(limit = nil)
limit = 0 unless limit.nil?
@monkeys.each { |monkey| monkey.join limit } unless @monkeys.empty?
end
# Kills the monkey.
@@ -150,13 +152,13 @@
# terminated.
#
# @return [Array] an Array of Monkey objects that were killed.
#
def kill_all!
- @monkeys.each { |monkey| monkey.kill } unless @monkeys.empty?
+ @monkeys.each(&:kill) unless @monkeys.empty?
monkeys = @monkeys.dup
@monkeys.clear
monkeys
end
end
-end
\ No newline at end of file
+end