class CoopAl::Value

Value

Attributes

copper[R]
cp[R]
electrum[R]
ep[R]
gold[R]
gp[R]
platinum[R]
pp[R]
silver[R]
sp[R]

Public Class Methods

copper(amount) click to toggle source
# File lib/coop_al/value.rb, line 83
def self.copper(amount)
  Value.new(copper: amount)
end
electrum(amount) click to toggle source
# File lib/coop_al/value.rb, line 87
def self.electrum(amount)
  Value.new(electrum: amount)
end
gold(amount) click to toggle source
# File lib/coop_al/value.rb, line 75
def self.gold(amount)
  Value.new(gold: amount)
end
new(amounts = {}) click to toggle source
# 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
platinum(amount) click to toggle source
# File lib/coop_al/value.rb, line 71
def self.platinum(amount)
  Value.new(platinum: amount)
end
silver(amount) click to toggle source
# File lib/coop_al/value.rb, line 79
def self.silver(amount)
  Value.new(silver: amount)
end

Public Instance Methods

*(other) click to toggle source
# File lib/coop_al/value.rb, line 61
def *(other)
  normalize_from(raw_value * other)
  self
end
+(other) click to toggle source
# 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) click to toggle source
# File lib/coop_al/value.rb, line 66
def /(other)
  normalize_from(raw_value / other)
  self
end
nonzero?() click to toggle source
# File lib/coop_al/value.rb, line 27
def nonzero?
  !zero?
end
normalize() click to toggle source
# File lib/coop_al/value.rb, line 47
def normalize
  normalize_from(raw_value)
end
normalize_from(value) click to toggle source
# 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
raw_value() click to toggle source
# File lib/coop_al/value.rb, line 31
def raw_value
  @platinum * 10 + @gold + @silver / 10.0 + @copper / 100.0 + @electrum / 2.0
end
to_a() click to toggle source
# File lib/coop_al/value.rb, line 91
def to_a
  [@platinum, @electrum, @gold, @silver, @copper]
end
to_s() click to toggle source
# 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?() click to toggle source
# File lib/coop_al/value.rb, line 23
def zero?
  raw_value.zero?
end