Class: Zold::Amount
- Inherits:
-
Object
- Object
- Zold::Amount
- 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
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(other) ⇒ Object
- #<(other) ⇒ Object
- #<=(other) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #>(other) ⇒ Object
-
#initialize(coins: 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(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
118 119 120 121 122 123 |
# File 'lib/zold/amount.rb', line 118 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
96 97 98 99 |
# File 'lib/zold/amount.rb', line 96 def +(other) raise '+ may only work with Amount' unless other.is_a?(Amount) Amount.new(coins: @coins + other.to_i) end |
#-(other) ⇒ Object
101 102 103 104 |
# File 'lib/zold/amount.rb', line 101 def -(other) raise '- may only work with Amount' unless other.is_a?(Amount) Amount.new(coins: @coins - other.to_i) end |
#/(other) ⇒ Object
125 126 127 128 |
# File 'lib/zold/amount.rb', line 125 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
81 82 83 84 |
# File 'lib/zold/amount.rb', line 81 def <(other) raise '< may only work with Amount' unless other.is_a?(Amount) @coins < other.to_i end |
#<=(other) ⇒ Object
86 87 88 89 |
# File 'lib/zold/amount.rb', line 86 def <=(other) raise '<= may only work with Amount' unless other.is_a?(Amount) @coins <= other.to_i end |
#<=>(other) ⇒ Object
91 92 93 94 |
# File 'lib/zold/amount.rb', line 91 def <=>(other) raise '<= may only work with Amount' unless other.is_a?(Amount) @coins <=> other.to_i end |
#==(other) ⇒ Object
71 72 73 74 |
# File 'lib/zold/amount.rb', line 71 def ==(other) raise "== may only work with Amount: #{other}" unless other.is_a?(Amount) @coins == other.to_i end |
#>(other) ⇒ Object
76 77 78 79 |
# File 'lib/zold/amount.rb', line 76 def >(other) raise '> may only work with Amount' unless other.is_a?(Amount) @coins > other.to_i end |
#negative? ⇒ Boolean
110 111 112 |
# File 'lib/zold/amount.rb', line 110 def negative? @coins.negative? end |
#positive? ⇒ Boolean
114 115 116 |
# File 'lib/zold/amount.rb', line 114 def positive? @coins.positive? end |
#to_i ⇒ Object
54 55 56 |
# File 'lib/zold/amount.rb', line 54 def to_i @coins end |
#to_s ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/zold/amount.rb', line 62 def to_s text = "#{to_zld}ZLD" if negative? Rainbow(text).red else Rainbow(text).green 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
106 107 108 |
# File 'lib/zold/amount.rb', line 106 def zero? @coins.zero? end |