Class: CoopAl::Value

Inherits:
Object
  • Object
show all
Defined in:
lib/coop_al/value.rb

Overview

Value

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amounts = {}) ⇒ Value

Returns a new instance of Value.



8
9
10
11
12
13
14
15
# File 'lib/coop_al/value.rb', line 8

def initialize(amounts = {})
  @platinum = amounts[:platinum] || 0
  @electrum = amounts[:electrum] || 0
  @gold = amounts[:gold] || 0
  @silver = amounts[:silver] || 0
  @copper = amounts[:copper] || 0
  normalize
end

Instance Attribute Details

#copperObject (readonly)

Returns the value of attribute copper.



6
7
8
# File 'lib/coop_al/value.rb', line 6

def copper
  @copper
end

#electrumObject (readonly)

Returns the value of attribute electrum.



6
7
8
# File 'lib/coop_al/value.rb', line 6

def electrum
  @electrum
end

#goldObject (readonly)

Returns the value of attribute gold.



6
7
8
# File 'lib/coop_al/value.rb', line 6

def gold
  @gold
end

#platinumObject (readonly)

Returns the value of attribute platinum.



6
7
8
# File 'lib/coop_al/value.rb', line 6

def platinum
  @platinum
end

#silverObject (readonly)

Returns the value of attribute silver.



6
7
8
# File 'lib/coop_al/value.rb', line 6

def silver
  @silver
end

Class Method Details

.copper(amount) ⇒ Object



83
84
85
# File 'lib/coop_al/value.rb', line 83

def self.copper(amount)
  Value.new(copper: amount)
end

.electrum(amount) ⇒ Object



87
88
89
# File 'lib/coop_al/value.rb', line 87

def self.electrum(amount)
  Value.new(electrum: amount)
end

.gold(amount) ⇒ Object



75
76
77
# File 'lib/coop_al/value.rb', line 75

def self.gold(amount)
  Value.new(gold: amount)
end

.platinum(amount) ⇒ Object



71
72
73
# File 'lib/coop_al/value.rb', line 71

def self.platinum(amount)
  Value.new(platinum: amount)
end

.silver(amount) ⇒ Object



79
80
81
# File 'lib/coop_al/value.rb', line 79

def self.silver(amount)
  Value.new(silver: amount)
end

Instance Method Details

#*(other) ⇒ Object



61
62
63
64
# File 'lib/coop_al/value.rb', line 61

def *(other)
  normalize_from(raw_value * other)
  self
end

#+(other) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/coop_al/value.rb', line 51

def +(other)
  @platinum += other.pp
  @electrum += other.ep
  @gold += other.gp
  @silver += other.sp
  @copper += other.cp
  normalize
  self
end

#/(other) ⇒ Object



66
67
68
69
# File 'lib/coop_al/value.rb', line 66

def /(other)
  normalize_from(raw_value / other)
  self
end

#cpObject

Returns the value of attribute copper.



21
22
23
# File 'lib/coop_al/value.rb', line 21

def copper
  @copper
end

#epObject

Returns the value of attribute electrum.



18
19
20
# File 'lib/coop_al/value.rb', line 18

def electrum
  @electrum
end

#gpObject

Returns the value of attribute gold.



19
20
21
# File 'lib/coop_al/value.rb', line 19

def gold
  @gold
end

#nonzero?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/coop_al/value.rb', line 27

def nonzero?
  !zero?
end

#normalizeObject



47
48
49
# File 'lib/coop_al/value.rb', line 47

def normalize
  normalize_from(raw_value)
end

#normalize_from(value) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/coop_al/value.rb', line 35

def normalize_from(value)
  @platinum = 0
  @electrum = 0
  @gold = value.floor
  value -= @gold
  value *= 10
  @silver = value.floor
  value -= @silver
  value *= 10
  @copper = value.floor
end

#ppObject

Returns the value of attribute platinum.



17
18
19
# File 'lib/coop_al/value.rb', line 17

def platinum
  @platinum
end

#raw_valueObject



31
32
33
# File 'lib/coop_al/value.rb', line 31

def raw_value
  @platinum * 10 + @gold + @silver / 10.0 + @copper / 100.0 + @electrum / 2.0
end

#spObject

Returns the value of attribute silver.



20
21
22
# File 'lib/coop_al/value.rb', line 20

def silver
  @silver
end

#to_aObject



91
92
93
# File 'lib/coop_al/value.rb', line 91

def to_a
  [@platinum, @electrum, @gold, @silver, @copper]
end

#to_sObject



95
96
97
98
99
100
101
102
103
# File 'lib/coop_al/value.rb', line 95

def to_s
  [
    [@platinum, 'pp'],
    [@gold, 'gp'],
    [@silver, 'sp'],
    [@copper, 'cp'],
    [@electrum, 'ep']
  ].select { |v| v[0].nonzero? }.map { |v| "#{v[0]}#{v[1]}" }.join(', ')
end

#zero?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/coop_al/value.rb', line 23

def zero?
  raw_value.zero?
end