Class: TinyMoney::Money
- Inherits:
-
Object
- Object
- TinyMoney::Money
- Defined in:
- lib/tiny_money.rb
Overview
Define class responsible to represent monetary values
Class Method Summary collapse
- .dump(obj) ⇒ Object
-
.load(loaded_value) ⇒ Object
method to work with active record.
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(other) ⇒ Object
-
#initialize(this) ⇒ Money
constructor
A new instance of Money.
- #truncate(n) ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(this) ⇒ Money
Returns a new instance of Money.
16 17 18 19 |
# File 'lib/tiny_money.rb', line 16 def initialize(this) raise ArgumentError, "#{value} is not a number" unless number?(this) @this = BigDecimal(this.to_s) end |
Class Method Details
.dump(obj) ⇒ Object
28 29 30 31 |
# File 'lib/tiny_money.rb', line 28 def dump(obj) raise ArgumentError, (obj) unless obj.is_a?(self) obj.value end |
.load(loaded_value) ⇒ Object
method to work with active record. Based on www.viget.com/articles/how-i-used-activerecord-serialize-with-a-custom-data-tyApe
24 25 26 |
# File 'lib/tiny_money.rb', line 24 def load(loaded_value) new(loaded_value) end |
Instance Method Details
#*(other) ⇒ Object
52 53 54 |
# File 'lib/tiny_money.rb', line 52 def *(other) wrap_new_value(@this * other) end |
#+(other) ⇒ Object
44 45 46 |
# File 'lib/tiny_money.rb', line 44 def +(other) wrap_new_value(@this + other) end |
#-(other) ⇒ Object
48 49 50 |
# File 'lib/tiny_money.rb', line 48 def -(other) wrap_new_value(@this - other) end |
#/(other) ⇒ Object
56 57 58 |
# File 'lib/tiny_money.rb', line 56 def /(other) wrap_new_value(@this / other) end |
#truncate(n) ⇒ Object
60 61 62 |
# File 'lib/tiny_money.rb', line 60 def truncate(n) @this.truncate(n) end |
#value ⇒ Object
40 41 42 |
# File 'lib/tiny_money.rb', line 40 def value @this.to_f end |