Class: HhMoney

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
app/my_lib/hh_money.rb

Overview

typed: ignore

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

.format_rounded(price_cents, currency, round: :up) ⇒ Object



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_centsObject



86
87
88
# File 'app/my_lib/hh_money.rb', line 86

def amount_cents
  money.cents
end

#default_format(*rules) ⇒ Object Also known as: to_s



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/my_lib/hh_money.rb', line 53

def default_format(*rules)
  default_options = case money.currency.to_s
                    when Country::THAI_CURRENCY_CODE, Country::SINGAPORE_CURRENCY_CODE, Country::MALAYSIA_CURRENCY_CODE
                      { disambiguate: true }
                    when Country::USA_CURRENCY_CODE
                      {}
                    else
                      APMErrorHandler.report('unknown money currency', currency: money.currency)
                      return ''
                    end

  merged_options = rules.first.is_a?(Hash) ? rules.first.merge(default_options) : default_options
  money.format(**merged_options)
end

#formatObject



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
  # APMErrorHandler.report('deprecated method, use default_format instead', caller: Rails.backtrace_cleaner.clean(caller))

  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

#format_currency_symbolObject



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/my_lib/hh_money.rb', line 68

def format_currency_symbol
  case money.currency.to_s
  when Country::THAI_CURRENCY_CODE, Country::USA_CURRENCY_CODE
    money.currency.symbol
  when Country::SINGAPORE_CURRENCY_CODE
    money.currency.disambiguate_symbol
  when Country::MALAYSIA_CURRENCY_CODE
    money.currency.symbol # MYR uses regular symbol "RM"
  else
    APMErrorHandler.report('unknown money currency', currency: money.currency)
    ''
  end
end

#format_rounded(round: :up) ⇒ Object



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