Class: Zold::Amount

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

Overview

Amount

Constant Summary collapse

MAX =

Maximum amount of zents

2**63
ZERO =

Just zero, for convenience.

Amount.new(zents: 0)

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Amount.



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

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

Instance Method Details

#*(other) ⇒ Object



124
125
126
127
128
129
# File 'lib/zold/amount.rb', line 124

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

#+(other) ⇒ Object



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

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

#-(other) ⇒ Object



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

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

#/(other) ⇒ Object



131
132
133
134
# File 'lib/zold/amount.rb', line 131

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

#<(other) ⇒ Object



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

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

#<=(other) ⇒ Object



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

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

#<=>(other) ⇒ Object



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

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

#==(other) ⇒ Object



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

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

#>(other) ⇒ Object



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

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

#negative?Boolean

Returns:

  • (Boolean)


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

def negative?
  @zents.negative?
end

#positive?Boolean

Returns:

  • (Boolean)


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

def positive?
  @zents.positive?
end

#to_iObject

Convert it to zents and return as an integer.



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

def to_i
  @zents
end

#to_sObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/zold/amount.rb', line 66

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

Convert to ZLD and return as a string. If you need float, you should use to_f() later.



62
63
64
# File 'lib/zold/amount.rb', line 62

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

#zero?Boolean

Returns:

  • (Boolean)


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

def zero?
  @zents.zero?
end