82 lines
1.6 KiB
Ruby
82 lines
1.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class Float
|
|
def btc
|
|
StrikeMe::BTC.new((self * 100_000_000).round)
|
|
end
|
|
|
|
def sats
|
|
StrikeMe::BTC.new(round)
|
|
end
|
|
end
|
|
|
|
class Integer
|
|
def btc
|
|
StrikeMe::BTC.new(self * 100_000_000)
|
|
end
|
|
|
|
def sats
|
|
StrikeMe::BTC.new(self)
|
|
end
|
|
end
|
|
|
|
module StrikeMe
|
|
class BTC
|
|
class Error < StandardError; end
|
|
|
|
MAX_NUMBER_OF_SATS = 21_000_000 * 100_000_000
|
|
|
|
attr_reader :sats
|
|
|
|
def initialize(sats)
|
|
raise Error, 'Must be initialized using an integer representing sats' unless sats.is_a? Integer
|
|
raise Error, "Max sats is: #{MAX_NUMBER_OF_SATS}. You have: #{sats}" if sats > MAX_NUMBER_OF_SATS
|
|
raise Error, 'Must be initialized using a positive number' if sats.negative?
|
|
|
|
@sats = sats
|
|
end
|
|
|
|
def to_s
|
|
# to_f.to_s
|
|
format('%.8f', to_f)
|
|
end
|
|
|
|
def to_f
|
|
sats.to_f / 100_000_000
|
|
end
|
|
|
|
def as_json
|
|
to_s
|
|
end
|
|
|
|
def *(other)
|
|
self.class.new((sats * other).to_i)
|
|
end
|
|
|
|
def /(other)
|
|
self.class.new((sats / other).to_i)
|
|
end
|
|
|
|
def +(other)
|
|
raise Error, "Addition can only be performed using a #{self.class.name} object" unless other.is_a?(self.class)
|
|
|
|
self.class.new(sats + other.sats)
|
|
end
|
|
|
|
def -(other)
|
|
raise Error, "Addition can only be performed using a #{self.class.name} object" unless other.is_a?(self.class)
|
|
|
|
self.class.new(sats - other.sats)
|
|
end
|
|
|
|
def ==(other)
|
|
case other
|
|
when Integer then sats == other
|
|
when BTC then sats == other.sats
|
|
else
|
|
false
|
|
end
|
|
end
|
|
end
|
|
end
|