Class: HhMoney
- Inherits:
-
Object
show all
- Extended by:
- Forwardable
- Defined in:
- app/my_lib/hh_money.rb
Overview
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(price_cents, price_currency, from_amount: false) ⇒ HhMoney
Returns a new instance of HhMoney.
31
32
33
|
# File 'app/my_lib/hh_money.rb', line 31
def initialize(price_cents, price_currency, from_amount: false)
@money = from_amount ? Money.from_amount(price_cents, price_currency) : Money.new(price_cents, price_currency)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
catch all missing methods and send to #money
91
92
93
94
95
96
97
|
# File 'app/my_lib/hh_money.rb', line 91
def method_missing(method, *args, &block)
if money.respond_to?(method)
money.send(method, *args, &block)
else
super
end
end
|
Class Method Details
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'app/my_lib/hh_money.rb', line 10
def format_rounded(price_cents, currency, round: :up)
if price_cents % 1 == 0
return HhMoney.new(price_cents, currency).default_format
end
x = price_cents % 100
if round == :up
price_cents = price_cents - x + 100 if x != 0
else
price_cents = price_cents - x
end
HhMoney.new(price_cents, currency).default_format
end
|
.from_amount(amount, currency) ⇒ Object
24
25
26
27
28
|
# File 'app/my_lib/hh_money.rb', line 24
def from_amount(amount, currency)
amount = amount.to_d unless amount.is_a?(Numeric)
new(amount, currency, from_amount: true)
end
|
Instance Method Details
#amount_cents ⇒ Object
86
87
88
|
# File 'app/my_lib/hh_money.rb', line 86
def amount_cents
money.cents
end
|
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'app/my_lib/hh_money.rb', line 35
def format
case money.currency.to_sym
when :THB
money.format(no_cents_if_whole: true, format: '%u%n')
when :SGD
money.format(no_cents_if_whole: true, format: 'S%u%n')
when :MYR
money.format(no_cents_if_whole: true, format: '%u%n')
when :USD
money.format(no_cents_if_whole: true)
else
APMErrorHandler.report('unknown money currency', currency: money.currency)
''
end
end
|
82
83
84
|
# File 'app/my_lib/hh_money.rb', line 82
def format_rounded(round: :up)
self.class.format_rounded(money.cents, money.currency, round: round)
end
|