class CurrencyNumberal::Currency

Attributes

code[RW]
number[RW]

Public Class Methods

new(number, code) click to toggle source

rubocop:disable Lint/MissingSuper

# File lib/currency_numberal.rb, line 21
def initialize(number, code)
  @number = number
  @code = code
end

Public Instance Methods

*(other) click to toggle source
# File lib/currency_numberal.rb, line 72
def *(other)
  raise Error, 'Invalid number' unless other.is_a?(Numeric)

  self.class.new(@number.to_f * other, @code)
end
+(other) click to toggle source
# File lib/currency_numberal.rb, line 64
def +(other)
  self.class.new(@number + other.send(@code).number, @code)
end
-(other) click to toggle source
# File lib/currency_numberal.rb, line 68
def -(other)
  self.class.new(@number - other.send(@code).number, @code)
end
/(other) click to toggle source
# File lib/currency_numberal.rb, line 78
def /(other)
  raise Error, 'Invalid number' unless other.is_a?(Numeric)

  self.class.new(@number.to_f / other, @code)
end
<=>(other) click to toggle source
# File lib/currency_numberal.rb, line 84
def <=>(other)
  base <=> other.base
end
base() click to toggle source
# File lib/currency_numberal.rb, line 43
def base
  @number.to_f / CurrencyNumberal::CURRENCIES[@code][:base]
end
coerce(other) click to toggle source
# File lib/currency_numberal.rb, line 53
def coerce(other)
  case other
  when Float
    [self.class.new(other, @code), self]
  when Integer
    [self.class.new(other, @code), self]
  else
    [self.class.new(other.to_f, @code), self]
  end
end
symbol() click to toggle source

rubocop:enable Lint/MissingSuper

# File lib/currency_numberal.rb, line 27
def symbol
  CurrencyNumberal::CURRENCIES[@code][:symbol]
end
to_f() click to toggle source
# File lib/currency_numberal.rb, line 35
def to_f
  @number.to_f
end
to_i() click to toggle source
# File lib/currency_numberal.rb, line 39
def to_i
  @number.to_i
end
to_s() click to toggle source
# File lib/currency_numberal.rb, line 31
def to_s
  "#{@number.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse} #{symbol}"
end