Initial commit
This commit is contained in:
commit
4b5f7906d7
33 files changed
+933
No files matched your search
@@ -0,0 +1,82 @@
|
||||
# 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
|
||||
NUMERICAL_OPERATORS = %i[* /].freeze
|
||||
|
||||
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
|
||||
Reference in new issue
Block a user