Class: Zold::Amount

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/amount.rb

Overview

Amount

Constant Summary collapse

FRACTION =

How many zents are in one ZLD: 2^FRACTION

32
MAX =

Maximum amount of zents

2**63
ZERO =
Amount.new(coins: 0)

Instance Method Summary collapse

Constructor Details

#initialize(coins: nil, zld: nil) ⇒ Amount

Returns a new instance of Amount.


36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/zold/amount.rb', line 36

def initialize(coins: nil, zld: nil)
  if !coins.nil?
    raise "Integer is required, while #{coins.class} provided: #{coins}" unless coins.is_a?(Integer)
    @coins = coins
  elsif !zld.nil?
    raise "Float is required, while #{zld.class} provided: #{zld}" unless zld.is_a?(Float)
    @coins = (zld * 2**Amount::FRACTION).to_i
  else
    raise 'You can\'t specify both coints and zld'
  end
  raise "The amount is too big: #{@coins}" if @coins > Amount::MAX
  raise "The amount is too small: #{@coins}" if @coins < -Amount::MAX
end

Instance Method Details

#*(other) ⇒ Object


112
113
114
115
116
# File 'lib/zold/amount.rb', line 112

def *(other)
  c = (@coins * other).to_i
  raise "Overflow, can't multiply #{@coins} by #{m}" if c > Amount::MAX
  Amount.new(coins: c)
end

#+(other) ⇒ Object


94
95
96
97
# File 'lib/zold/amount.rb', line 94

def +(other)
  raise '+ may only work with Amount' unless other.is_a?(Amount)
  Amount.new(coins: @coins + other.to_i)
end

#-(other) ⇒ Object


99
100
101
102
# File 'lib/zold/amount.rb', line 99

def -(other)
  raise '- may only work with Amount' unless other.is_a?(Amount)
  Amount.new(coins: @coins - other.to_i)
end

#<(other) ⇒ Object


79
80
81
82
# File 'lib/zold/amount.rb', line 79

def <(other)
  raise '< may only work with Amount' unless other.is_a?(Amount)
  @coins < other.to_i
end

#<=(other) ⇒ Object


84
85
86
87
# File 'lib/zold/amount.rb', line 84

def <=(other)
  raise '<= may only work with Amount' unless other.is_a?(Amount)
  @coins <= other.to_i
end

#<=>(other) ⇒ Object


89
90
91
92
# File 'lib/zold/amount.rb', line 89

def <=>(other)
  raise '<= may only work with Amount' unless other.is_a?(Amount)
  @coins <=> other.to_i
end

#==(other) ⇒ Object


69
70
71
72
# File 'lib/zold/amount.rb', line 69

def ==(other)
  raise '== may only work with Amount' unless other.is_a?(Amount)
  @coins == other.to_i
end

#>(other) ⇒ Object


74
75
76
77
# File 'lib/zold/amount.rb', line 74

def >(other)
  raise '> may only work with Amount' unless other.is_a?(Amount)
  @coins > other.to_i
end

#negative?Boolean

Returns:

  • (Boolean)

108
109
110
# File 'lib/zold/amount.rb', line 108

def negative?
  @coins < 0
end

#to_iObject


52
53
54
# File 'lib/zold/amount.rb', line 52

def to_i
  @coins
end

#to_sObject


60
61
62
63
64
65
66
67
# File 'lib/zold/amount.rb', line 60

def to_s
  text = "#{to_zld}ZLD"
  if negative?
    Rainbow(text).red
  else
    Rainbow(text).green
  end
end

#to_zld(digits = 2) ⇒ Object


56
57
58
# File 'lib/zold/amount.rb', line 56

def to_zld(digits = 2)
  format("%0.#{digits}f", @coins.to_f / 2**Amount::FRACTION)
end

#zero?Boolean

Returns:

  • (Boolean)

104
105
106
# File 'lib/zold/amount.rb', line 104

def zero?
  @coins.zero?
end