Class: Zold::Amount
- Inherits:
-
Object
- Object
- Zold::Amount
- Defined in:
- lib/zold/amount.rb
Overview
Amount
Constant Summary collapse
- MAX =
Maximum amount of zents
2**63
- ZERO =
Amount.new(zents: 0)
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(other) ⇒ Object
- #<(other) ⇒ Object
- #<=(other) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #>(other) ⇒ Object
-
#initialize(zents: nil, zld: nil) ⇒ Amount
constructor
A new instance of Amount.
- #negative? ⇒ Boolean
- #positive? ⇒ Boolean
- #to_i ⇒ Object
- #to_s ⇒ Object
- #to_zld(digits = 2) ⇒ Object
- #zero? ⇒ Boolean
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
121 122 123 124 125 126 |
# File 'lib/zold/amount.rb', line 121 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
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(zents: @zents + other.to_i) end |
#-(other) ⇒ Object
104 105 106 107 |
# File 'lib/zold/amount.rb', line 104 def -(other) raise '- may only work with Amount' unless other.is_a?(Amount) Amount.new(zents: @zents - other.to_i) end |
#/(other) ⇒ Object
128 129 130 131 |
# File 'lib/zold/amount.rb', line 128 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
84 85 86 87 |
# File 'lib/zold/amount.rb', line 84 def <(other) raise '< may only work with Amount' unless other.is_a?(Amount) @zents < 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) @zents <= other.to_i 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) @zents <=> 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: #{other}" unless other.is_a?(Amount) @zents == 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) @zents > other.to_i end |
#negative? ⇒ Boolean
113 114 115 |
# File 'lib/zold/amount.rb', line 113 def negative? @zents.negative? end |
#positive? ⇒ Boolean
117 118 119 |
# File 'lib/zold/amount.rb', line 117 def positive? @zents.positive? end |
#to_i ⇒ Object
55 56 57 |
# File 'lib/zold/amount.rb', line 55 def to_i @zents end |
#to_s ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/zold/amount.rb', line 63 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
59 60 61 |
# File 'lib/zold/amount.rb', line 59 def to_zld(digits = 2) format("%0.#{digits}f", @zents.to_f / 2**FRACTION) end |
#zero? ⇒ Boolean
109 110 111 |
# File 'lib/zold/amount.rb', line 109 def zero? @zents.zero? end |