Sha256: c4fa645ab42e85c62ee400577d57060cd1c85400ed7c127c8d72c9c5905c6ec8

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

require "koin/version"
require 'bigdecimal'

class Koin < BigDecimal
  attr_accessor :currency

  def initialize(value, currency = nil)
    @currency = currency&.upcase

    if value.is_a?(Float)
      super(value, 16)
    elsif value.is_a?(Koin)
      @currency ||= value.currency
      super(value)
    elsif value.is_a?(String) || value.is_a?(Integer) || value.is_a?(BigDecimal) || value.is_a?(Numeric)
      super(value)
    else
      raise NotImplementedError("Invalid `value` type: #{value.class}.")
    end
  end

  # TODO: Make sure comparisons actually work.
  # def <=>(other)
  #   raise ArgumentError.new("`currency` must be identical to compare two Koin objects. Got `#{self.currency}` and `#{other.currency}`") \
  #     unless self.currency == other.currency

  #   super
  # end
  # Koin.new(1, 'eth') == Koin.new(1)
  # Koin.new(1, 'eth') == Koin.new(1, 'eth')
  # Koin.new(1, 'btc') == Koin.new(1, 'eth')
  # Koin.new(1) == Koin.new(1)
  # Koin.new(1) == Koin.new(2)

  def to_s(*args)
    "#{super} #{currency}".strip
  end
  alias_method :inspect, :to_s
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
koin-0.5.2 lib/koin.rb