Class: Ubea::Money

Inherits:
BigDecimal
  • Object
show all
Defined in:
lib/ubea/money.rb

Constant Summary collapse

CurrencyMismatch =
Class.new(Exception)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount, currency) ⇒ Money

Returns a new instance of Money.



10
11
12
13
14
# File 'lib/ubea/money.rb', line 10

def initialize(amount, currency)
  self.currency = currency

  super(amount)
end

Instance Attribute Details

#currencyObject

Returns the value of attribute currency.



8
9
10
# File 'lib/ubea/money.rb', line 8

def currency
  @currency
end

Class Method Details

._load(args) ⇒ Object



117
118
119
# File 'lib/ubea/money.rb', line 117

def self._load(args)
  new(*args.split(':'))
end

Instance Method Details

#*(other) ⇒ Object



76
77
78
79
# File 'lib/ubea/money.rb', line 76

def *(other)
  assert_currency!(other, strict: false)
  self.class.new super, currency
end

#+(other) ⇒ Object



66
67
68
69
# File 'lib/ubea/money.rb', line 66

def +(other)
  assert_currency!(other)
  self.class.new super, currency
end

#-(other) ⇒ Object



71
72
73
74
# File 'lib/ubea/money.rb', line 71

def -(other)
  assert_currency!(other)
  self.class.new super, currency
end

#/(other) ⇒ Object



81
82
83
84
# File 'lib/ubea/money.rb', line 81

def /(other)
  assert_currency!(other, strict: false)
  self.class.new super, currency
end

#<(other) ⇒ Object



102
103
104
105
# File 'lib/ubea/money.rb', line 102

def <(other)
  assert_currency!(other)
  super
end

#<=(other) ⇒ Object



107
108
109
110
# File 'lib/ubea/money.rb', line 107

def <=(other)
  assert_currency!(other)
  super
end

#==(other) ⇒ Object



86
87
88
89
# File 'lib/ubea/money.rb', line 86

def ==(other)
  return false unless other.is_a?(self.class)
  to_s == other.to_s
end

#>(other) ⇒ Object

<=> does not work somehow???



92
93
94
95
# File 'lib/ubea/money.rb', line 92

def >(other)
  assert_currency!(other)
  super
end

#>=(other) ⇒ Object



97
98
99
100
# File 'lib/ubea/money.rb', line 97

def >=(other)
  assert_currency!(other)
  super
end

#_dump(level) ⇒ Object

Keep currency when marshaling



113
114
115
# File 'lib/ubea/money.rb', line 113

def _dump(level)
  [super.to_s.split(':').last, currency].join ':'
end

#exchange_to(new_currency) ⇒ Object



16
17
18
19
20
21
# File 'lib/ubea/money.rb', line 16

def exchange_to(new_currency)
  return self if new_currency == currency

  new_amount = CurrencyConverter.convert(self, currency, new_currency)
  self.class.new new_amount, new_currency
end

#inspectObject



62
63
64
# File 'lib/ubea/money.rb', line 62

def inspect
  "#<Ubea::Money amount=#{self}>"
end

#to_s(with_currency: true, auto_format: true, tooltip_currency: false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ubea/money.rb', line 23

def to_s(with_currency: true, auto_format: true, tooltip_currency: false)
  amount = dup
  currency = self.currency.dup

  old_amount_string = if tooltip_currency
    amount.exchange_to(tooltip_currency).to_s(auto_format: false)
  end

  if auto_format
    if currency == "BTC" && amount < 1
      amount *= 1E6
      currency = "bits"
      self.max_decimal_places = 2
    end
  end

  amount = sprintf("%.#{decimal_places}f", amount)

  left, right = amount.split('.')

  if right
    right.gsub!(/(\d{3})(?=\d)/, '\\1 ') # thousand sep
    right.gsub!(/[0 ]+$/, '') # remove superfluous traling zeros
    groups = right.split("")
    right << "0" * (3 - groups.last.size) if groups.size > 1 && groups.last # padding
  end

  amount = left.reverse.gsub(/(\d{3})(?=\d)/, '\\1 ').reverse
  amount << "." << right if right && !right.empty?

  amount << " " << currency if with_currency

  if tooltip_currency && tooltip_currency != currency
    amount = %(<span title="#{old_amount_string}" data-toggle="tooltip">#{amount}</span>)
  end

  amount
end