-
Notifications
You must be signed in to change notification settings - Fork 0
/
race.rb
79 lines (65 loc) · 2.39 KB
/
race.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# frozen_string_literal: true
require_relative 'character_modifier'
require_relative 'elements'
# This class models a character race. A race contains a Stats object, this
# object has the stats bonuses that should be given to a character of that
# particular race. It also says for what elements a character of that race
# receives bonus attack and AP gain.
#
# @author Sergio Bobillier <sergio.bobillier@gmail.com>
class Race < CharacterModifier
# @return [Symbol|Array<Symbol>] The element or elements for which a Character
# of this race should receive bonuses on attack and AP gain.
attr_reader :element
alias elements element
# Sets the race element (if given).
def initialize(stats, element = nil)
super(stats)
self.element = element if element
end
# Sets the race's element (or elements). A race's element determines what
# elemental bonuses a character of the race will receive (when attacking or
# during AP gain).
#
# @param element [Symbol] The race's element.
def element=(element)
unless element.is_a?(Symbol) || element.is_a?(Array) || element.nil?
raise ArgumentError, '`element` should be a Symbol or an Array of Symbols'
end
if element.is_a?(Array)
validate_elements(element)
else
validate_element(element) unless element.nil?
end
@element = element
end
alias elements= element=
private
# Validates that no element is duplicated in the given array, that the array
# is compraised only of Symbols and that each of those Symbols is actually a
# valid element.
#
# @param elements [Array<Symbol>] The elements array.
# @raise [ArgumentError] If there is a duplicated element in the array, if
# any of the elements in the array is not a Symbol or any of the elements is
# unknown.
def validate_elements(elements)
if elements.length != elements.uniq.length
raise ArgumentError, 'Elements in the array should be unique'
end
elements.each do |element|
unless element.is_a?(Symbol)
raise ArgumentError, 'All elements of the array should be Symbols'
end
validate_element(element)
end
end
# Checks that the given element is valid.
#
# @param element [Symbol] The element.
# @raise [ArgumentError] If the element is unknown.
def validate_element(element)
return if ELEMENTS.include?(element)
raise ArgumentError, "Unknown element '#{element}'"
end
end