Class: SafeMoney
- Inherits:
-
Object
- Object
- SafeMoney
- Defined in:
- lib/safe_money.rb,
lib/safe_money/version.rb
Constant Summary collapse
- CoercionError =
Class.new(TypeError)
- ArithmeticError =
Class.new(ArgumentError)
- VERSION =
"0.0.2"
Class Method Summary collapse
- .cents(number) ⇒ Object (also: [])
- .dollars(number) ⇒ Object
Instance Method Summary collapse
- #*(number) ⇒ Object
- #+(number) ⇒ Object
- #-(number) ⇒ Object
- #-@ ⇒ Object
- #/(number) ⇒ Object (also: #div)
- #as_json(options = nil) ⇒ Object
- #eql?(other) ⇒ Boolean
- #equal?(other) ⇒ Boolean
-
#initialize(cents) ⇒ SafeMoney
constructor
A new instance of SafeMoney.
- #inspect ⇒ Object (also: #to_s, #to_str)
- #negative? ⇒ Boolean
- #positive? ⇒ Boolean
- #to_dollars ⇒ Object
- #to_i ⇒ Object
- #to_param ⇒ Object
Constructor Details
#initialize(cents) ⇒ SafeMoney
Returns a new instance of SafeMoney.
38 39 40 41 42 |
# File 'lib/safe_money.rb', line 38 def initialize cents raise ArgumentError, 'cents must be an Integer' unless cents.is_a?(Integer) @cents = cents freeze end |
Class Method Details
.cents(number) ⇒ Object Also known as: []
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/safe_money.rb', line 10 def cents number return number if number.is_a?(self) raise ArgumentError, 'cents must be Numeric' unless number.is_a?(Numeric) cents = if number.is_a?(Float) raise CoercionError, "#{number} cannot safely be converted into cents" unless number.to_i == number number.to_i else number end new cents end |
.dollars(number) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/safe_money.rb', line 24 def dollars number return number if number.is_a?(self) raise ArgumentError, 'dollars must be Numeric' unless number.is_a?(Numeric) if number != number.round(2) raise CoercionError, "#{number} cannot safely be converted into cents" end cents = (number * 100.00).round new cents end |
Instance Method Details
#*(number) ⇒ Object
82 83 84 85 86 |
# File 'lib/safe_money.rb', line 82 def * number self.class.cents( to_f * number ) rescue CoercionError raise ArithmeticError, "#{self} cannot be safely multiplied by #{number}" end |
#+(number) ⇒ Object
70 71 72 73 74 |
# File 'lib/safe_money.rb', line 70 def + number self.class.cents( to_i + number ) rescue CoercionError raise ArithmeticError, "#{number} cannot be safely added to #{self}" end |
#-(number) ⇒ Object
76 77 78 79 80 |
# File 'lib/safe_money.rb', line 76 def - number self.class.cents( to_i - number ) rescue CoercionError raise ArithmeticError, "#{number} cannot be safely subtracted from #{self}" end |
#-@ ⇒ Object
96 97 98 |
# File 'lib/safe_money.rb', line 96 def -@ self.class.cents(-to_i) end |
#/(number) ⇒ Object Also known as: div
88 89 90 91 92 |
# File 'lib/safe_money.rb', line 88 def / number self.class.cents( to_f / number ) rescue CoercionError raise ArithmeticError, "#{self} cannot be safely divided by #{number}" end |
#as_json(options = nil) ⇒ Object
115 116 117 |
# File 'lib/safe_money.rb', line 115 def as_json =nil to_i end |
#eql?(other) ⇒ Boolean
56 57 58 59 |
# File 'lib/safe_money.rb', line 56 def eql? other other = other.to_i if self.class === other to_i.eql?(other) end |
#equal?(other) ⇒ Boolean
61 62 63 64 |
# File 'lib/safe_money.rb', line 61 def equal? other other = other.to_i if self.class === other to_i.equal?(other) end |
#inspect ⇒ Object Also known as: to_s, to_str
108 109 110 111 |
# File 'lib/safe_money.rb', line 108 def inspect dollars = to_dollars "#{'-' if dollars < 0}$#{sprintf('%.2f', dollars.abs)}" end |
#negative? ⇒ Boolean
104 105 106 |
# File 'lib/safe_money.rb', line 104 def negative? self < 0 end |
#positive? ⇒ Boolean
100 101 102 |
# File 'lib/safe_money.rb', line 100 def positive? self > 0 end |
#to_dollars ⇒ Object
66 67 68 |
# File 'lib/safe_money.rb', line 66 def to_dollars (@cents * 0.01).round(2) end |
#to_i ⇒ Object
44 45 46 |
# File 'lib/safe_money.rb', line 44 def to_i @cents end |
#to_param ⇒ Object
119 120 121 |
# File 'lib/safe_money.rb', line 119 def to_param to_i.to_s end |