Class: TinyMoney::Money

Inherits:
Object
  • Object
show all
Defined in:
lib/tiny_money.rb

Overview

Define class responsible to represent monetary values

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(this) ⇒ Money

Returns a new instance of Money.

Raises:

  • (ArgumentError)


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

Raises:

  • (ArgumentError)


28
29
30
31
# File 'lib/tiny_money.rb', line 28

def dump(obj)
  raise ArgumentError, argument_error_message(obj) unless obj.is_a?(self)
  obj.value
end

.load(loaded_value) ⇒ Object



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

#valueObject



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

def value
  @this.to_f
end