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.



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

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



120
121
122
123
124
125
# File 'lib/zold/amount.rb', line 120

def *(other)
  raise '* may only work with a number' unless other.is_a?(Integer) || other.is_a?(Float)
  c = (@coins * other).to_i
  raise "Overflow, can't multiply #{@coins} by #{m}" if c > Amount::MAX
  Amount.new(coins: c)
end

#+(other) ⇒ Object



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

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

#-(other) ⇒ Object



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

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

#/(other) ⇒ Object



127
128
129
130
# File 'lib/zold/amount.rb', line 127

def /(other)
  raise '/ may only work with a number' unless other.is_a?(Integer) || other.is_a?(Float)
  Amount.new(coins: (@coins / other).to_i)
end

#<(other) ⇒ Object



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

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

#<=(other) ⇒ Object



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

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

#<=>(other) ⇒ Object



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

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

#==(other) ⇒ Object



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

def ==(other)
  raise "== may only work with Amount: #{other}" unless other.is_a?(Amount)
  @coins == other.to_i
end

#>(other) ⇒ Object



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

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

#negative?Boolean

Returns:

  • (Boolean)


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

def negative?
  @coins.negative?
end

#positive?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/zold/amount.rb', line 116

def positive?
  @coins.positive?
end

#to_iObject



54
55
56
# File 'lib/zold/amount.rb', line 54

def to_i
  @coins
end

#to_sObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/zold/amount.rb', line 62

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

#to_zld(digits = 2) ⇒ Object



58
59
60
# File 'lib/zold/amount.rb', line 58

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

#zero?Boolean

Returns:

  • (Boolean)


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

def zero?
  @coins.zero?
end