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.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/zold/amount.rb', line 22

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



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

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



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

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

#-(other) ⇒ Object



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

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

#/(other) ⇒ Object



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

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



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

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

#<=(other) ⇒ Object



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

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

#<=>(other) ⇒ Object



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

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

#==(other) ⇒ Object



65
66
67
68
# File 'lib/zold/amount.rb', line 65

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

#>(other) ⇒ Object



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

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

#negative?Boolean

Returns:

  • (Boolean)


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

def negative?
  @zents.negative?
end

#positive?Boolean

Returns:

  • (Boolean)


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

def positive?
  @zents.positive?
end

#to_fObject

Convert to ZLD and return as a float.



45
46
47
# File 'lib/zold/amount.rb', line 45

def to_f
  @zents.to_f / (2**FRACTION)
end

#to_iObject

Convert it to zents and return as an integer.



40
41
42
# File 'lib/zold/amount.rb', line 40

def to_i
  @zents
end

#to_sObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/zold/amount.rb', line 54

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.



50
51
52
# File 'lib/zold/amount.rb', line 50

def to_zld(digits = 2)
  format("%0.#{digits}f", to_f)
end

#zero?Boolean

Returns:

  • (Boolean)


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

def zero?
  @zents.zero?
end