Class: Trooper::Arsenal

Inherits:
Array
  • Object
show all
Defined in:
lib/trooper/arsenal.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.actionsObject

Public: Storage for the defined actions.

Examples

Arsenal.actions # => [<Action>, <Action>]

Returns the actions arsenal.



81
82
83
# File 'lib/trooper/arsenal.rb', line 81

def actions
  @actions ||= new
end

.reset!Object

Public: Clears the arsenals storage of all strategies and actions.

Examples

Arsenal.reset! # => true

Returns true.



92
93
94
95
# File 'lib/trooper/arsenal.rb', line 92

def reset!
  @strategies, @actions = nil
  true
end

.strategiesObject

Public: Storage for the defined strategies.

Examples

Arsenal.strategies # => [<Strategy>, <Strategy>]

Returns the strategies arsenal.



70
71
72
# File 'lib/trooper/arsenal.rb', line 70

def strategies
  @strategies ||= new
end

Instance Method Details

#add(weapon) ⇒ Object

Public: Add a ‘weapon’ to the arsenal.

weapon - An object that responds to a name method e.g ‘weapon.name’ .

Examples

Arsenal.actions.add(<Action>) # => <Action>

Returns the weapon passed.



29
30
31
32
33
34
35
# File 'lib/trooper/arsenal.rb', line 29

def add(weapon)
  if weapon.ok?
    remove weapon.name
    self << weapon 
  end
  weapon
end

#clear!Object

Public: Clears the arsenals storage array.

Examples

Arsenal.strategies.clear! # => []

Returns an empty array.



57
58
59
# File 'lib/trooper/arsenal.rb', line 57

def clear!
  self.clear
end

#find_by_name(name) ⇒ Object Also known as: []

Public: Find an item in the arsenal.

name - The name of the weapon object, weapon object must respond to name.

Examples

Arsenal.strategies.find_by_name(:my_stratergy) # => <Strategy>
Arsenal.strategies[:my_stratergy] # => <Strategy>

Returns the duplicated String.



15
16
17
# File 'lib/trooper/arsenal.rb', line 15

def find_by_name(name)
  detect { |weapon| weapon.name == name }
end

#remove(name) ⇒ Object

Public: Removes a ‘weapon’ from the arsenal.

name - The name of the arsenal to delete.

Examples

Arsenal.actions.remove(:my_action) # => [<Action>]

Returns self.



46
47
48
# File 'lib/trooper/arsenal.rb', line 46

def remove(name)
  self.delete_if {|w| w.name == name}
end